Now that we have learned how to work with files, let’s learn some command to work with directories.
Changing Directory
Remember how in the previous guide we created a file inside Documents directory? How do we go into the Documents directory and ls
the files inside? This is perhaps one of the most used commands in a terminal beside ls
. Now as we move into another directory, let me tell you another secret tricks of a pro developer. Type cd Doc
into your command line, and then hit the Tab [↹] key
$ cd Doc⇥
This feature is known as auto-complete, and it works to auto-complete commands and possible directory and file that works with the command. Hit Enter and then type ls
. You will see the content of Documents directory.
In order to move back up into home directory, you can use the ..
directory path as cd
argument, like:
$ cd ..
Then try to ls
again to see you have returned to Home directory. There is also a useful command for showing the path of our current directory, which is pwd
or print working directory. Type this command to find out where you are. Remember your output will be different from mine, unless your username is also “nathan.”
$ pwd
/home/nathan
Creating, Copying and Moving Directory
We have done this in the previous section using mkdir
command, so of course you already know how to create a directory. The only thing left for me to tell you is that both mkdir
and touch
command support creating multiple files and directories at one time. You can type them like this:
$ mkdir a_directory another_directory third_directory
and you will make three directories in your current directory. Next, let’s cd
into the Documents directory and make more files.
$ cd Documents && touch begin.txt start.txt end.txt
Another useful operator that will be used a lot in the world of command line is the &&
and operator. This operator means you can join two different command into one line, which is just what we did.
Now that we have multiple files in Documents directory, let’s play around with copying and moving directories. For an example:
$ cp -r ../a_directory .
$ ls
a_directory
the -r
means recursively, and what this mean in human language is “copy the a_directory and all files inside that directory into the current directory.”
Next, let’s do moving directory. Go into the home folder and move another_directory into Documents.
$ mv another_directory Documents/another_directory_new_name
If you don’t want to rename another_directory, then you can omit the another_directory_new_name part from the command. You don’t need the -r
argument for moving directory.
Deleting Directory
Now deleting directory runs very quirky in the command line world, because the command rmdir
can only delete empty directories, so if you try to delete Documents folder like:
$ rmdir Documents
rmdir: failed to remove 'Documents': Directory not empty
That’s why developers rarely use the rmdir
command, and instead use a modified version of rm
command. Try this command for a change:
$ rm -r Documents
As with copying, -r
argument means remove Documents directory along with all of its content without exceptions. Now sometimes you might also need to add -f
argument to the command in case -r doesn’t work. But you want to be careful with it, as -f
stands for force, and it makes your command able to remove some files that are stubborn.
$ rm -rf Documents