Lab Objective:
Learn how to create, delete, and list files.
Lab Purpose:
In this lab, you will learn how to create, delete, and list files using the Bash shell.
Lab Tool:
Ubuntu 18.04 (or another distro of your choice).
Lab Topology:
A single Linux machine, or virtual machine
Lab Walkthrough:
Task 1:
Open the Terminal application, then enter: touch foo
You have now created a file, whose (empty) contents you can print with cat foo
You can list more details about the new file with ls -lh foo
Task 2:
Now create another empty file: touch .bar
You can use the ls command by itself to list all files in the current directory (which right now should be your home directory). But what happens when you do this now? Where is .bar?
.bar is a hidden file! Use ls -a, and it will show up.
Task 3:
Now let’s clean up. Enter rm foo .bar to remove both files you created.
Task 4:
Enter: mkdir -p foo/bar
You have now created not one, but two directories, one inside the other. Now run the following:
cd foo
ls
Just like with files, you can also create hidden directories:
mkdir .baz
ls
ls -alh
Your current directory (which you changed with cd) is foo, which you can confirm with pwd.
Task 5:
ls -alh lists bar and .baz, as expected, but it also lists. and .. – these represent the listed and parent directories, respectively. In other words, . is foo, and .. is your home directory, also known as ~
Now run:
cd .
pwd
pushd ..
pwd
popd
pwd
cd ~
pwd
How do those commands affect your navigation through the directory structure?
Task 6:
Now run:
cd ./foo
touch ~/foo/.baz/quux
ls -aR ~/foo
The ~ expands to /home/user (where “user” is whatever your username is). Thus, while the first command above uses a relative pathname, where the result depends on the current directory, the latter two commands use an absolute pathname. The results of those commands will be the same regardless of your directory location (unless, of course, you switch users).
Task 7:
Finally, let’s clean up:
cd ~
rm -r foo
Notes:
When you typed ls -a, you probably noticed some other strange dotted entries, like. and .. – these are not files, but directories, and will be covered in the next lab.
A “trick” you may run into is a file that begins with -, tricking Bash into thinking you are providing a flag to rm rather than the file to be removed. Can you figure out how to remove such a file?