Even if in the last years Linux became more accessible even to new entries, with even more sophisticated and Windows-like graphical interfaces and desktop environments like Gnome, KDE, xfce, Unity and so on, the core of operations is still run via command line.
Once you start approaching the command line world, the first basic operations that you have to face involve file and directory manipulation.
You will be asked to create, modify, copy, move files and directories, along with some basic editing of configuration file.
Every linux shell is shipped with basic linux files commands and features, that include file manipulation (see my post about linux shells for more information) and the commands we are going to see now cover them all.
What linux files commands are we going to see?
The commands we are going to explore are:
Move files/directories – mv
The mv command is used to either move files or folders or rename them.
The syntax for renaming a file or directory is:
While if you want to move a file or a directory from a path to another you can use:
Copy files/directories – cp
The cp command is used to make a copy of an existing file and allows the user to change its name when copying it.
The syntax for the command is:
Similarly to the mv command, cp can be used to copy a file into another directory:
In a different way from the move command, copying a directory and its content is achieved appending an option to the command:
Create a directory – mkdir
Assume that you need to create a directory in the path /usr/local, mkdir is the command that fits your needs.
It can create directories both using an absolute path or relative path.
The syntax is:
This syntax works if the directory you are creating is part of a path that already exists (i.e. /usr/local) but assume that you want to create a directory inside another directory and both don’t exist, you need to use a flag, ‘-p’, that makes sure that directories that are not found in the specified path are created at the same time.
The above command will create both nonexistentdir and mybrandnewdir.
Remove files/directories – rm
The rm command is used to remove files or directories and their content, and when used it asks for confirmation before applying the operation.
The syntax for deleting files is:
While for directories, like the cp command, it needs a flag to apply the operation recursively on the directory and its content:
If you feel brave enough, you can avoid getting asked if you are sure to remove what you want to remove by using another flag that tells the command to force the operation:
Be very careful when using rm with the “-f” flag, as there are not turn back once you hit the command.
Here you go! How you have some basic understandings about how files and directories can be manipulated. Be sure to follow the next tutorials to learn more about more advanced features!