Exploring The File System
All files in the Linux OS are organized into directories. For Windows users, this is akin to organizing files into “folders”. An important point to note is that everything in Linux can be classified as a file or directory.
By the end of this section, the reader will be able to:
Navigate directories
Create/delete directories and files
How To Create/Delete Files And Directories
mkdir
command
The mkdir
allows us to create directories. This simplest way to use it is to type mkdir
followed by the name of the new directory into the terminal and hit enter. This will create the new directory in your present working directory.
mkdir newdirectoryname
Let’s create a new directory called “NewDirectory”.
Mkdir Screen:
We have successfully created our a new directory called “NewDirectory” in our current working directory.
You can also create multiple directories inside your current directory by entering this into your terminal:
mkdir newdirectoryname1 newdirectoryname2 newdirectoryname3
Go ahead and try this on your own. Enter the ls
command and you should be able to see all three newly created directories in your current directory.
We can also use mkdir
to create a directory in a directory other than the one we are currently in. Simply type mkdir
followed by the path/newdirectoryname. In our example, we will create a new directory named “NewDirectory” inside the /tmp/usr/ sub-directory, from our current working directory.
mkdir path/newdirectoryname
In the screenshot above, we use the cd
, pwd
and ls
functions to show the contents of the /tmp/usr/ subdirectory before and after executing the mkdir
command from the / directory. We successfully created the “NewDirectory” in the /tmp/usr sub directory.
You can also create multiple directories inside your another directory by entering this into your terminal:
mkdir path/newdirectoryname1 path/newdirectoryname2 path/newdirectoryname3
Go ahead and try this on your own. Enter the ls
command and you should be able to see all three newly created directories in your current directory.
Now that we have created all these new directories, it seems fitting that we learn how to delete them.
rmdir
command
The rmdir
command allows us to delete a directory. It is used in an almost identical way to the mkdir
command. For that reason, we will forgo most explanation and simply demonstrate an example of its usage in the screen shot below. Readers are encouraged to try on their own.
rmdir DirectoryName
or
rmdir path/directoryname
Now that we have learned how to create and delete directories, we will learn how to do the same for files.
touch
command
The touch
command is the easiest way to create a new file in linux. It is used in a similar fashion as mkdir
and rmdir
commands, including creating multiple files at once.
touch newFileName
or
touch path/newFileName
Refer to the screenshot below for an example of its usage.
Using touch
allows us to create a new file, but what if we want to create a new file and place content into it right away. For that, we will introduce the cat
command.
cat
command
cat
is a powerful command that allows us to write as well as display contents to the file.
Firstly to create a file type this into your command line and hit enter (make sure newfile does not already exist in that folder otherwise you will overwrite the file):
cat newfile
Now you will notice that you have not returned to the command line, instead the terminal is expecting more input. This is where you input the contents of the file. Once you are finished writing the contents of the file you can return to the command line using ctrl + d.
Similarly you can do the same for files that are in other directories by using the command:
cat path/newfile
To display the contents of a file we can use the cat
command as such:
cat filename
or
cat path/filename
In the example below we will create a file named HelloWorld.txt in a directory other than our current one, write Hello World! to the file and display its contents from the terminal.
For users who would like to explore file editting in more detail, click here.
Finally we will learn how to delete files.
rm
command
The rm
command is used to delete files in the same way rmdir
deletes directories.
rm
filename
or
rm path/filename
As our example we will delete the HelloWorld.txt
we created above.
This concludes our introduction of Linux Filesystem. At this point, the reader should feel comfortable with navigating in Linux. In the following articles/sections we will look at some more advanced topics such as redirection and shell scripting.
In the next section, we’ll be going over the bread and butter of the command line: redirecting inputs and outputs of commands.