You are on page 1of 14

Operating Systems

(CC418)
LAB 4 – FILES AND DIRECTORIES

Eng. Nour Eldehy


Creating files and directories
• We learned before how to view contents of directories with “ls” as we learned how to navigate
between them with “cd”.
• In order to create a new file we use the command “touch” followed by one or more file names.
Using extensions while creating files is optional and is out of the scope of the command.
• In this example, we create a new file (file1), then create two more files (file2.txt and file3):
Creating files and directories
• Creating new directories follows the same logic except that the command used is “mkdir”
which stands for “Make Directory”.
• In this example, we create a new directory (dir1):

• But what happens when we try to create a directory called “my_folder.mp3”? Should this
work?!
Creating files and directories
• The reason this worked is the known Unix principle “Everything is a file”. This means that the
file is the building block of everything. For example, the sound you hear from your speakers is
actually some binary data being continuously written to the file representing your connected
speakers. If you remember the directory “/dev”, everything that belongs to your keyboard,
mouse and other external devices can be found here.
• For example, every single mouse movement should be stored in a file called “psaux” in the
“/dev” directory. If everything is a file, we should be able to view the contents of that file with
“cat” just like any file. Try typing “sudo cat /dev/psaux” and notice what happens when you
move your mouse. (You can stop this with pressing CTRL+C).
• In the same sense, we were able to create the directory “my_folder.mp3” because a directory is
just a file that contains more files inside it. Unix/Linux doesn’t care about extensions. Extensions
are just a way for us humans to differentiate between files and “file containers”. When we see a
“.mp3” we expect to find a sound file. That’s why it is still not recommended to create
directories with extensions.
Deleting files and directories
• Deleting files can be achieved by using the command “rm” which stands for “Remove”.
• In this example we delete “file5”:

• Remember that we can use wildcards with other commands than “ls” too. Here we delete all
files that start with the character “f”:

• Remember to be extremely careful when using wildcards while deleting things! For example, if
you were inside a large directory and typed “rm *” instead of “rm f*”, then every single file
would be deleted!
Deleting files and directories
• Similarly, we can delete directories with “rmdir” that stands for “Remove Directory”. However,
this command can only delete empty directories.
• In this example, we make sure that “dir1” is empty then go back and delete it:

• If we tried to delete a directory that has files inside, we would have gotten an error message.
Then how do we delete full directories? We do this by using “rm” we previously used for files
but we also add to it the flag “-r” which means “recursively”, which means that we tell the
command to delete this file and the file inside it, and the file inside it...and so on.
Copying/Moving files and directories
• Copying and Moving files follow the same logic except that moving files doesn’t leave the
original file in its place.
• We copy files and directories with the command “cp” we follow it by the path of the file we
want to copy then the destination path. In this example we copy the file inside “dir1” and paste
it to dir1’s parent folder:

• In order to copy directories, the same logic applies except that we have to add the “-r” flag. For
example, “cp -r dir1 ..” makes a copy of “dir1” in its parent folder.
Copying/Moving files and directories
• Similarly, we move files with “mv”. In this example, we move the directory “dir1” to the music
folder:

• Notice that we don’t need to use any flags to move directories (unlike cp).
• If we think about it, renaming a file or directory is just moving it to the same place but with a
different name. In this example we rename file1 to file2:
Text editors
• We can edit text in Ubuntu in different ways. Some text editors have a GUI while others run
straight from the terminal. Vim is the most popular terminal text editor but it has a very high
learning curve that is out of this lab’s scope. A less popular but easier terminal text editor is
Nano that we will use along with Ubuntu’s default GUI text editor Gedit.
• We can run Nano by either typing the command “nano” alone or followed by the file’s name
even if it doesn’t exist yet “nano file1”.
• Save your work by pressing (CTRL+O).
• Exit with (CTRL+X).
Text editors
• Similarly, we can use Gedit by typing “gedit” or by following it with a file’s name.

• Control will return to the terminal as soon as you exit Gedit from its window or by pressing
(CTRL+C) in the terminal (Don’t forget to save!).
Other utilities
• First let’s create a new file and fill it with text then save and exit.

• Remember that everything is a file and that Linux doesn’t care about extensions. We didn’t
even have to write “.txt” while it is advised to do so. But how do we know the type of the file’s
contents anyway? We use the “file” command. Here we try it on the “list” file and learn that it
contains ASCII text:
Other utilities
• We can view the “list” file’s contents with “cat”:

• But what if the file has way more than 10 lines and we just want to view the first n or last n
lines? We use the commands “head” and “tail” with the flag “-n” followed by the number of
required lines. Note that without the “-n” flag, the default is 10.
Useful Resources
• More on Unix’s “Everything is a file”:
• https://www.tecmint.com/explanation-of-everything-is-a-file-and-types-of-files-in-linux
• https://www.reddit.com/r/linux4noobs/comments/3ewhba/when_it_is_said_in_linux_everything_is_a
_file

• Learning Vim through an interactive tutorial:


• https://www.openvim.com/tutorial.html

• Learning Vim through a game:


• https://vim-adventures.com

• Those who want to prepare for the next lab should read about the following topics:
• File ownership and permissions.
• Super users and sudo.
Thank You!

You might also like