Checkpoint 3.2.1. Exercise: Try shell commands for navigation.
Try each of the following commands on your own machine.
echo
command which simply echos or displays the argument(s) to the terminal. Try the following with or without the quotes:echo "I love open source!"
echo
command could do, it would be a fairly pointless command. The echo
command’s real power is in displaying the contents of variables and files as we will soon see.whoami
pearcej
.Echo
, ECHO
, WhoAmI
, and WHOAMI
might not be recognized as commands, so you are in a case-sensitive environment and accidentally type one of these, you will elicit a response like:Command 'WhoAmI' not found
Command 'Echo' not found, did you mean: command 'echo'
echo
command that we already saw. Try the following:echo $USER
whoami
command! Note that you must precede the variable name with a dollar sign $ whenever you reference the value it contains. Isn’t it interesting to peek inside the environment variable in that way? echo
command is to echo the values of variables, which the shell can expand. Let’s see another example:echo $SHELL
/bin/bash
myfolder
located in the root directory, its path would be /myfolder
. If myfolder
then contains a file named hello.txt
, its path would be /myfolder/hello.txt
. If /myfolder
contains a directory named /mydocuments
, its path would be /myfolder/mydocuments
. And so on." /root
folder which is found on some Linux-based systems and which is the home directory of the root superuser.pwd
command stands for print working directory and as the name says it prints the path of the directory you are working in. Try typing:pwd
/home/pearcej
pearcej
which itself is a inside of a directory called home
which is itself inside of the root directory, /
. Note that the position with respect to the forward slash /
tells us the direction of the relationship – further to the right is further inside. To use the tree terminology, a directory that has a file or second directory directly inside of it is called a parent of the file or second directory that is inside, while the file or second directory is called the child of the parent directory. Hence, pearcej
is a child of home
and home
is a child of the root directory /
in our example.cd
command which stands for change directory followed by the /
:cd /
pwd
, you should see /
echoed back.ls
to get a listing of the files and subdirectories in the current working directory.ls
ls
, the listing command. Try typing the following where the -r
option stands for reverse
, and it reverses the sorting order:ls -r
-l
option stands for long listing format, meaning that instead of output containing only the names of files and directories, the ls
command will produce additional information, assuming you have at least one file or directory.ls -l
drwxr-xr-x 3 root root 4096 Jun 19 10:55 home lrwxrwxrwx 1 root root 7 Mar 24 2022 bin -> usr/bin -rw-rw-r-- 1 pearcej friends 22 Sep 15 2022 hello.txt
-l
flag changed how the listing is displayed.ls -l
command.cd ~ ls -l
drwxr-xr-x 3 pearcej pearcej 4096 Jun 19 10:55 home lrwxrwxrwx 1 root pearcej 7 Mar 24 2022 bin -> usr/bin -rw-rw-r-- 1 pearcej friends 22 Apr 15 2023 hello.txt
d
tells us it is a directory, l
indicates it is a symbolic link, and -
indicates it is a regular file. (Note that a symbolic link, aka symlink or soft link is a special type of file that acts as a shortcut by pointing to some other file or directory.) The next nine characters display various permissions for the file. The first set of three characters (characters 2-4) are the user’s (i.e. the file owner’s) permissions, the next three are the group’s permissions, and the last three are the permissions for all others. (The user’s name is in the third column and the group name is in the fourth column.) In each of these sets of three characters, the three characters refer to read (r
), write (w
), and execute (x
) respectively. If you see a letter then that permission is allowed, and if you see a ’-’, the permission is disallowed. For example in the last of the three lines shown above, we see that the file hello.txt
has permissions of -rw-rw-r--
. This means this is a file that can be read and written (changed) but not executed by the owner pearcej
and also by any user who is a member of the friends
group, however other users can only read the file.chmod
command. We will explore this in a bit.--help
option on the command or you can use the manual which is accessed using the man
command. For example, with the ls
command, you can use either one of the following, noting that they work a bit differently because the manual may use paging. If it does, to go to the next page use the space bar.ls --help man ls
--help
and man
are not consistently both available, it is worth knowing both. That way, if one doesn’t work, you can try the other.ls
, you may see that the options -a
and --all
both list all files including that begin with a ’.’, which are the hidden files and directories. Besides brevity, one advantage the single minus sign often offers, is that to run multiple options at the same time, all you often need to do is concatenate them. Note that this is also program-specific; not all programs will allow this, but it is convenient when it is offered! For example, ls -lra
returns the result with the -l
, -r
, and -a
option flags all activated. Give it a try!ls -lra
cd ~
mkdir
command. For example, let’s say you want to make a new directory with the name newdir, you can type the following:mkdir newdir
ls -l
. The output should include a line that ends in `newdir`. Try it!drwxr-xr-x 2 pearcej pearcej 4096 Nov 15 17:00 newdir
cd newdir pwd
ls
command in an empty directory, but try it with the following options:ls -la
drwxr-xr-x 2 pearcej pearcej 4096 Nov 16 17:00 . drwxr-xr-x 13 pearcej pearcej 4096 Nov 16 17:00 ..
ls ..
, will list all the files and directories in the parent directory relative to where you are so you don’t have to change directories to get the listing from another directory. Try it!newdir
directory and you want to make a sibling directory called newdir2
, you can type the following:mkdir ../newdir2 ls -l ..
newdir2
as a child of our current parent directory. This makes newdir2
a sibling of the original newdir
directory because both have the same parent directory. The second line produces a long listing of all of the files and directories in the directory that is one level up relative to the current working directory. You should see both of the directories that you made listed as subdirectories.rmdir
followed by the name of the directory you want to remove. Note that there are other ways to remove directories, but remove directory is useful because it refuses to delete a directory that is not empty. Give it a try!cd
command is to use cd -
which will take you to your previous working directory. This is handy if you need to move between two distant locations. Try it followed by pwd
a couple of times.cd - pwd
cd
to descend into one of your new directories if you are not already in one so that you are in an empty directory. Then let’s try the following commands:touch newfile1.txt echo 'I love open source!' > newfile2.txt echo 'I really love open source!' >> newfile3.txt ls
touch
command simply makes an empty file if no file by that name already exists in the directory. It is a niche use case for the command, but people use it this way with regularity. The intended purpose of the touch
changes the last accessed/modified date. Understanding the result of the first line we typed is pretty straightforward. The second and third lines that we typed require a bit more explanation. As we learned above, the echo
command simply outputs (or echoes) the argument(s) to the terminal, but here the echo
command has been used in combination with output redirection into a file.>
or >>
. Recall that the echo
command normally prints to the screen. What we did above in creating new files with the echo
command was to use output redirection to redirect the output of the command into a file of the specified name. So, the command echo 'I love open source!' > newfile2.txt
redirected the output of the echo
command into the newfile2.txt file. Both >
and >>
will create a new file with the provided name if one does not already exist. Note that the >
is the output redirection operator used for overwriting a file that might or might not already exist, while the >>
is an output operator that appends the output to an existing file or creates a new one if one does not already exist. So, >
should be used judiciously! cat
to see the file contents. Type the following one at a time:cat newfile1.txt cat newfile2.txt cat newfile3.txt
cat
. The cat
command, which stands for concatenate, is used to concatenate standard input (typically the keyboard) or file(s) to the standard output. Like the name suggests, you can also use the cat
command to concatenate files. For example, if you try:cat newfile2.txt newfile3.txt > newfile1.txt cat newfile1.txt
cat
command in combination with output redirection to create a multi-line file. Try typing the following lines to create a new multi-line file named newfile_multi.txt:cat > newfile_multi.txt these are multiple lines
Control+D
on your keyboard which will cause the new file to be closed. You should now have a new file called newfile_multi.txt.<
or <<
allows you to redirect the input of a command. So, if we want the response from a command to be written to a file instead of to the terminal, we can use output redirection, but if we want the input to a command to come from a file instead of from the keyboard, we use input redirection. wc
, which stands for word count. This a command that as the name suggests can be used for counting. However, it does more than count words! It actually provides the line count, the word count, and the character count in the file(s) specified in the file arguments. By default, it displays all this in four-columnar output with the file name in the final column.wc < newfile2.txt
1 4 20 newfile2.txt
because newfile2.txt contains ’I love open source!’ which is 1 line, 4 words, and 20 characters. If you only want to count the number of words, you could use the -w
flag to display only the word count as follows:wc -w < newfile2.txt
ls
command into a temporary file, and then use input redirection with wc -w
to get the word count of this file. (This is one way to count the number of files in your current directory.) Let’s try the following:ls > temp.txt wc -w < temp.txt
ls | wc -w
cat >> helloworld.sh
will tell the shell to open a new file named helloworld.sh
and to expect multiple lines of input from the keyboard. Type the following lines:cat >> helloworld.sh # This is a comment in a script typically used to explain the purpose echo 'Hello World!'
cat >> helloworld.sh
command sequence by pressing Ctrl+D
on your keyboard. This will cause the file named helloworld.sh
to be completed and closed.helloworld.sh
. However, we would like to make it executable so we can run it as a script. To make it executable, we use the chmod
command. The chmod
stands for change file mode bits. To add execution, we will need the +x
option, which stands for ’add execution’ and will change it to being executable. So, run the following in your shell to make it an executable file for all users:chmod +x helloworld.sh
./helloworld.sh
read
command which reads text from the keyboard. It is frequently used to make scripts interactive. However, we need a variable to store the result. In the shell, some variables, like environment variables, always exist and you can always access them. However, you can also create your own variables. As we have seen, you can use echo to ask the shell to provide the value of a variable, but you must precede the variable name with the dollar sign ($). Let’s see how all this works by trying the following, finishing by pressing Control+D
:cat >> hellouser.sh # Says hello to the user by name. echo 'What is your first name?' # In the next line, USERNAME is dynamically created as a new variable read USERNAME echo "Hello " $USERNAME
chmod +x hellouser.sh ./hellouser.sh
mv
, cp
, and rm
respectively. Let’s see how these work. First, list your files. You should have newfile1.txt, newfile2.txt, and newfile3.txt in your directory from the previous exercise.mkdir subdir mv newfile3.txt subdir/newfile3.txt ls ls subdir
ls subdir
provides a listing of the subdirectory named subdir. You should see that newfile3.txt is now not in the current directory, but it is instead in the subdirectory subdir.mv
command to rename files as follows:mv newfile1.txt newfile4.txt ls
mv
command is that if the destination filename already exists, it might get overwritten. For this reason, you might want to use the -i
flag which stands for interactive. Try it.mv -i newfile1.txt newfile2.txt
cp
command works as you might expect:cp newfile2.txt newfile2_cp.txt cat newfile2_cp.txt
mv
command, the cp
command will overwrite the destination file if it already exists. For this reason, you might want to use the -i
flag which stands for interactive here too. Try it.cp -i newfile4.txt newfile2_cp.txt
-i
flag seems a wise safeguard.rm
command is a useful, but another dangerous command. Let’s try it:rm newfile2_cp.txt
cp
, and mv
, you are probably wise to use the -i
option.rm -i newfile4.txt
man bash
for hints on how to search your shell history, re-execute commands, and much more.history
command is executed:1 git init 2 git add main.c 3 git commit -m "Initial commit" 4 git remote add origin https://github.com/username/repo.git 5 git push -u origin master 6 history
rm -i newfile?.txt
rm: remove regular file 'newfile2.txt'? rm: remove regular file 'newfile4.txt'?
rm -i newfile*.txt
rm: remove regular file 'newfile2.txt'? rm: remove regular file 'newfile4.txt'? rm: remove regular file 'newfile2_cp.txt'?
apple orange banana grape
kiwi
to fruits.txt. Then, display the contents of fruits.txt in the terminal. Next, count the number of fruits in fruits.txt and display the total count. Finally, overwrite the content of data.txt with the content of fruits.txt and rename data.txt to a new file named fruits2
.