Code
[user@sahara ~]$ cd
[user@sahara ~]$
Working Directory Structure
├── home
│ ├── lecture1
│ │ ├── messages
│ │ │ ├── en-us.txt
│ │ │ ├── es-mx.txt
│ │ │ ├── zh-cn.txt
│ │ │ ├── zh-tw.txt
│ │ ├── Hello.class
│ │ ├── Hello.java
│ │ ├── README
Why this output?
The
cd
command change the directory of the current directory to the directory that we specify. Since no directory is specify here, thecd
command doesn’t do anything and return nothing.
Error? If so, why it’s an error
No, this is not an error.
Code
[user@sahara ~]$ ls
lecture1
Working Directory Structure
├── home
│ ├── lecture1
│ │ ├── messages
│ │ │ ├── en-us.txt
│ │ │ ├── es-mx.txt
│ │ │ ├── zh-cn.txt
│ │ │ ├── zh-tw.txt
│ │ ├── Hello.class
│ │ ├── Hello.java
│ │ ├── README
Why this output?
The
ls
command, short for list, help you list out all the files and directories inside the directory that I’m in, which is/home
Error? If so, why it’s an error
No, this is not an error.
Code
[user@sahara ~]$ cat
Working Directory Structure
├── home
│ ├── lecture1
│ │ ├── messages
│ │ │ ├── en-us.txt
│ │ │ ├── es-mx.txt
│ │ │ ├── zh-cn.txt
│ │ │ ├── zh-tw.txt
│ │ ├── Hello.class
│ │ ├── Hello.java
│ │ ├── README
Why this output?
After entering
cat
command, the program didn’t output anything and stop working because the command line is waiting for the user to input something after the command ofcat
, which the command line will stay there until user enter something or force stop the command with ctrl-c.
Error? If so, why it’s an error
Yes, this is an error because the command line stop doing anything and just waiting for user to input something to continue working because arugment is required to run this command.
Code
[user@sahara ~/lecture1]$ cd messages/
[user@sahara ~/lecture1/messages]$
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
The
cd
command is exectued with a path to a directory as an argument. Sincecd
stands for change directory, the current directory of the terminal is changed. The directory we use as an argument ismessages/
, which will take the current directory from/home/lecture1
to/home/lecture1/messages
.
Error? If so, why it’s an error
No, this is not an error.
Code
[user@sahara ~/lecture1]$ ls messages/
en-us.txt es-mx.txt zh-cn.txt zh-tw.txt
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
The
ls
command prints out all the files and directory inside the folder we specify. Since an argument of directorymessages/
is added after thels
command, the files printed out is the files and directories inside the directorymessages/
which isen-us.txt es-mx.txt zh-cn.txt zh-tw.txt
.
Error? If so, why it’s an error
No, this is not an error.
Code
[user@sahara ~/lecture1]$ cat messages/
cat: messages/: Is a directory
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
The
cat
command is used to print out the file in the command line, since we specify a directory, the output from the command line iscat: messages/: Is a directory
which tell the user thatmessages/
is a directory instead of file that can be print out.
Error? If so, why it’s an error
Yes, there is an error caused by giving the
cat
command a directory as the first argument, which thecat
doesn’t accept directory as input so output acat: messages/:Is a directory
error in the command line.
Code
[user@sahara ~/lecture1]$ cd Hello.java
bash: cd: Hello.java: Not a directory
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
Since the
cd
command is used to change the current working directory to another directory, after running the command with a file specify as the first argument, the command line return abash: cd: Hello.java: Not a directory
which said that you cannot change the directory to a file.
Error? If so, why it’s an error
Yes, this is an error because we pass a argument specify a file to the change directory command, which
cd
doesn’t take file as the argument input and returnbash: cd: Hello.java: Not a directory
error information to the user.
Code
[user@sahara ~/lecture1]$ ls Hello.java
Hello.java
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
The
ls
command list out the files and directories of the input relative/absolute path in the first argument. Since we passed a specific fileHello.java
, the only thing printed out isHello.java
since the file is specify and the only file or directory under that path isHello.java
.
Error? If so, why it’s an error
No, this is not an error.
Code
[user@sahara ~/lecture1]$ cat Hello.java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class Hello {
public static void main(String[] args) throws IOException {
String content = Files.readString(Path.of(args[0]), StandardCharsets.UTF_8);
System.out.println(content);
}
Working Directory Structure
├── lecture1
│ ├── messages
│ │ ├── en-us.txt
│ │ ├── es-mx.txt
│ │ ├── zh-cn.txt
│ │ ├── zh-tw.txt
│ ├── Hello.class
│ ├── Hello.java
│ ├── README
Why this output?
The
cat
command printed out the file specify in the first argument. In this case,Hello.java
specified, so the output displayed in the command line is the java program in plain text.
Error? If so, why it’s an error
No, this is not an error.