Linux Gate #4: Everything you need to know about Linux directories!
Directories are at the heart of Linux
Hello there! This article is the fifth article of our Linux series called: the Linux Gates.
In this series of articles, we want to provide you with exactly what you'll need for you to start your way through Linux commands.
Our aim in Outofbox-GATE is to help adapt Linux commands for everyone to help gain work efficiency in your daily job.
We think that Linux is an incredible tool not only for programmers but for everyone, and we excel at helping you see the interest of it in your work.
In the previous article Linux Gate #3 : The thing at the heart of Linux, we introduced you to files and how to create them. In the present article we are going to see how to create, modify, and remove directories.
in this article, we are going to see:
Why directories are important?
How to create a directory in Linux?
How to modify directory's name?
How to move file(s) inside a given directory
What is a directory?
In a simple words, a directory is a folder that contains multiple files. It's a files container.
In order to organize your work, you will usually need multiple separate directories or multiple directories inside one parent directory.
From the previous article Linux Gate #3 : The thing at the heart of Linux, we've talked about the path concept. Well, a path is nothing else but a series of directory's names one inside another.
Why directories are important?
Directories can help you organize a lot of files. They help as well getting to a desired file quickly through the path concept.
In Linux, directories are the most basic objects that nothing can work without them.
If you have in mind a program that you used in the past and found amazing, well if you break this program down, you'll find out that's it's mainly composed of files and directories.
It's amazing how these two entities : file and a directory are however simple and intuitive; constitutes the heart of our 21th century's technology.
You are probably wondering that it should takes much more than just files and directories to get something interesting and you are right.
The final ingredient missing here is the coding language.
But that's for another time ...
How to create a directory in Linux?
In order to create a directory, you need to type-in the command "mkdir" + the name of your directory:
The directory "newdirectory" is now created. You can check this out by using the "ls" command we've seen in Linux Gate #2 : Where to begin ?!:
Notice that Linux will create you desired directory in your current location in the path.
You can also notice that Linux comes with a set of built-in directories that can accomplish some basic functions.
Those directories usually starts with "/" symbol like /home /etc /mnt /opt /proc ...
But why is that? well if you think of Linux itself as a program, then you should know that it requires directories, files, and a language to be created!
Now, the "mkdir" command allows to create as many directories as you want. For example, if you want to create five directories numbered from 1 to 5. you can do it by:
The directories created are empty of course and ready to be used.
How to modify directory's name?
In order to change the name of a created directory we will need the command "mv" which stands for move.
When we write:
Linux simply change the name of the origin directory called "newdirectory" to call it "simpledirectory". Simple as that.
Now, the "mv" command works the same way with files:
The file "newfile" has now the name "simplefile".
The content of both modified file and directory, are not modified, but only their names are.
How to remove a directory?
To remove a directory, you need to use the command "rmdir". This command can remove only empty directories.
In order to remove a directory in general, whether empty or not, we recommend to use the command "rm".
Now this command allows only to remove file or files. But when we add the option "-r" to it, the command "rm -r" can remove directory or directories:
How to move file(s) inside a given directory?
So now that we know how to create files and directories, we'll often need to move things around from a directory into another one.
Here as well, the command "mv" is a must have. Say for example we want to move a file inside a given directory, then all we need to do is:
In fact, with this command, you can move a file into any path you want:
You can also move a directory (with all its contents: files and sub-directories) inside another directory using exactly the same previous command but with -r option.
The option -r is a very used option to transform commands that operates on files only and get them to work on directories as well.
This option is absolutely something to remember for future usages.
Where to go next?
There you go! you've learned how to create a directory, how to rename a directory and how to remove a directory.
In the next post, we are going to get in depth on how working with files and directories in order to make simple programs.
Stay tuned for that !
Meanwhile, don't hesitate to follow our Blog in order to get notified on future Posts and Podcasts.
You can also follow our LinkedIn page where we propose useful tips and trick about Linux :
OutOfBox-GATE Linux LinkedIn Page
And until the next Post, SHEERS !
Comments
Post a Comment