Linux Commands Series 3 : Directories and Files 1


One big part of working with Linux and the terminal is the manipulation of files and directories. Today I will be presenting some basic commands for working with them. so without further ado, let's start :

1. Create a file : You can create a file using many commands, today we will see a simple command, named touch

touch file1.txt

will create the file file1, we can also use this command to create many files at once :

touch tutorial1.txt tutorial2.txt tutorial3.txt

which will create all the 3 files at once.

touch command creates new files

the two uses of touch are showcased in the terminal screenshot above.

2. Create a directory : In order to create a directory you can use the command mkdir, which stands for make directory :

mkdir directory1

mkdir command

after executing mkdir directory1, you can notice that a new directory called directory1 appears when we execute the ls command

mkdir command can also be used to create many directories at once at the current directory, let's look at this example :

mkdir to create many directories

after executing mkdir project1 project2 project3, the directories have been created, which is shown by the ls command

mkdir command can also create nested directories that did not exist before using the option -p. For example :

mkdir -p myTutorial/Ubuntu/Shell

will create Shell directory within Ubuntu directory within myTutorial directory, all the three did not exist before in the example :

mkdir for creating nested directory with -p option

the 3 directories myTutorial,Ubuntu,Shell (which did not exist before as shown by ls), have been created one within an other till Shell directory, which is empty.

3. Remove a directory : you can remove a directory using the command rmdir which means 'remove directory', rmdir will only remove the directory if it is empty, otherwise it will warn you with an alert

rmdir alert for removing non empty directory
rmdir removes empty directory

the first time we tried to delete directory1, we could not because it was non empty, containing the directory subdir1, so we went inside directory1, removed subdir1 with rmdir subdir1, then went back with cd .., then executed rmdir directory1, which executed successfully.

4. remove a directory : there is an other solution if you want to remove a directory with all its subdirectories and files (please use it with extreme caution !!!), the command is rm -Rf directory :

Let's take an example :

rm command for removing directories with -rf options

TutorialDirectories contains two text files and a subdirectory called turotialSubdir, going to the parent directory (which is Linux in this case) then running rm -Rf TurotialDirectories wipes out the directory and all of its contents, as is shown by ls command after, that's why you should use it with extreme caution. if you want to be more careful and be prompted for every removal, use the option -i, as shown below :

remove directory in interactive mode

the command line is prompting us for the removal of every element in every subdirectory, we can respond by 'y' which is yes, or 'n' which is no

5. Delete a file : rm can also be used to delete a file, let's showcase it with an example : we will create a file using touch, ls the directory to see the file has been created, then execute the rm command then ls to see if the file has been deleted or not :

rm command to remove file

project2 directory was empty, we created testfile1.txt, then removed it with rm testfile1.txt, the ls command shows that the file testfile1.txt has really been removed.

rm can also delete many files at once :

rm file1 file2 file3

rm command to remove many files at once

file1.txt, file2.txt and file3.txt have been successfully removed with only one rm command

In the next tutorial, we will go more in depth about manipulating the contents of files and directories, with more new commands !

Happy Learning !