All commands in this lab will be run from the /home
directory. The file structure for this is shown in the image below.
- No arguments
[user@sahara ~]$ cd
[user@sahara ~]$
- Before the
cd
command was run, the working directory was/home
. - Since an argument was not given, the
cd
command changed into the root directory, which was/home
. Since we were already in the/home
directory, nothing happened. - There is no output, so no error occurs.
- Path to a directory
[user@sahara ~]$ cd lecture1
[user@sahara ~/lecture1]$
- Before the
cd
command was run, the working directory was/home
. - The only directory contained directly inside of
/home
waslecture1
. Whenlecture1
is passed as the argument for thecd
command, the working address changes to/home/lecture1
. - No error occurs when this command is run.
- Path to a file
[user@sahara ~]$ cd lecture1/Hello.java
bash: cd: lecture1/Hello.java: Not a directory
[user@sahara ~]$
- Before the
cd
command was run, the working directory was/home
. - There is no file contained directly inside of
/home
, so I used the path toHello.java
, which waslecture1/Hello.java
. This did not work because thecd
command can only receive directories as an input, not file names. - This output shows an error because I cannot
cd
intoHello.java
, as it is a file.
- No arguments
[user@sahara ~]$ ls
lecture1
[user@sahara ~]$
- The
ls
command is run from the/home
directory. - The
ls
command shows an overview of all the files and directories directly inside the working directory. No input is given, so the command works from the/home
directory. Since the only item directly inside of/home
islecture1
, the ls command outputslecture1
. - No error occurs with this command.
- Path to a directory
[user@sahara ~]$ ls lecture1
Hello.class Hello.java messages README
[user@sahara ~]$
- The
ls
command is run from the/home
directory. - Since
ls
lists all the files within the given directory, this command lists all the directories and files in/lecture1
because that is the input given. - No error occurs on this command.
- Path to a file
[user@sahara ~]$ ls lecture1/Hello.java
lecture1/Hello.java
[user@sahara ~]$
- The
ls
command is run from the/home
directory. - There are no files directly within
/home
, so I uselecture1/Hello.java
to access theHello.java
file. This outputs the path forHello.java
. - No error occurs here, but this command is ideally used to access files inside directories (not for files).
- No arguments
[user@sahara ~]$ cat f f
- The
cat
command is run from the/home
directory. - When
cat
is run with no input, the terminal freezes and runs infinitely. This is becausecat
provides a preview of files and directories, but nothing was given as an argument. When I typed something in the terminal while the command was running, the terminal provided an output of the copy of whatever I typed. - An error occurs because no input is given, so
cat
cannot provide a preview of any file or directory.
- Path to a directory
[user@sahara ~]$ cat lecture1 cat: lecture1: Is a directory [user@sahara ~]$
- The
cat
command is run from the/home
directory. - The input given is
lecture1
, which is a directory located inside/home
. Therefore, runningcat
provides the information forlecture1
by saying that it is a directory. - An error occurs because
cat
is meant to provide the data stored in a file. Since a directory is given instead,cat
cannot provide a file preview.
- Path to a file
[user@sahara ~]$ cat lecture1/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) t hrows IOException { String content = Files.readString(Path .of(args[0]), StandardCharsets.UTF_8); System.out.println(content); } } [user@sahara ~]$
- The
cat
command is run from the/home
directory. - No file is located directly inside
/home
, so the path provided is/lecture1/Hello.java
. This command outputs the code written inHello.java
. - No error occurs on this command.