The work in Linux operating systems consists of a large percentage in the use of files and directories, it is important to understand that the use of directories is practical to manage in a much more organized way all the information that we must work there, otherwise it would be a Small chaos access information when we need it..
However, although the use of directories is ideal for better control, there will always be the option that some directories or files should be deleted from the system because they are no longer necessary and if we leave them there they can occupy space and affect the organization tasks. Therefore, it is ideal to learn to erase this type of objects in Linux and thus add more knowledge to our daily tasks.
1. RM and RMDIR commands to delete files in Linux
Before going into detail about how to delete files or folders in Linux, we must understand the use of commands for that action, this is achieved with the rm and rmdir commands.
What is the rm command?
The command rm (remove) is ideal to delete files and directories in Linux, when using rm we will delete the files that we indicate but by default, rm does not delete directories in the system.
The usage syntax of this command is:
rm [OPTION] ... ARCHIVE ...
Rm command options
The available options for this command are:
- -f, --force: this option ignores nonexistent files or never asks before executing the task.
- -i: throws a warning before each file to be deleted.
- -I: this option asks once before deleting more than three files, or when recursively deleting the files.
- --interactive [= WHEN]: this option ask according to WHEN: never, once (-I), or always (-i).
- --one-file-system: this line is used when we are going to delete a hierarchy recursively, so it ignores any directory that is found in a file system other than the one of the executed argument.
- --no-preserve-root: this option does not protect root '/'
- --preserve-root: this option does not remove root '/'
- r, -R, - recursive: when using this option it is necessary to delete the directories as well as their content recursively.
- -v, --verbose: this line indicates what action we are taking.
- -help: display the command help.
- --version: display the used version of the rm command.
What is the rmdir command?
The command rmdir (remove directory), as the name implies, has been developed to remove a directory in Linux. The rmdir command deletes directories if they are empty.
Its usage syntax is:
rmdir (OPTION) ... DIRECTORY
Rmdir command options
The options for use with this command are:
- --ignore-fail-in-non-empty: this option ignores the failures in order to delete a directory since it is not empty.
- -p, --parents: The use of this parameter will delete the selected directory and then try to delete each component of the directory.
- -v, --verbose: this option will display a message for each directory that has been deleted.
- --help: display the help of rmdir.
- --version: Shows the version of the rmdir command used
With this in mind we will now see how to get the most out of deleting files or directories with rm and rmdir..
2. How to delete files with the rm command in Linux
Step 1
The most essential step to delete a file with this command is to go to the directory or path where the file is located and use the following line:
rm "File"
Step 2
To explain it, we will go to the desktop and use the ls command to list the files, then we will delete the file6 and then again use ls to see that it has been deleted:
rm file6
Step 3
In case the file is not in the current working directory, we must indicate the path to the location of the file, for example, we will delete an image from the Documents directory:
Step 4
As we have mentioned, it will be possible to delete more than one file simultaneously, in this case we will delete the elements file6 and file7, so we execute:
rm file6 file7
Step 5
Now, if in a directory we have numerous files of the same extension, TechnoWikis advises resorting to wildcards which allow us to select groups of files to delete at the same time, in this case we will use the asterisk * sign for which it represents multiple characters, so, in this case, if we want to delete all the files whose extension is .png we will execute:
rm * .png
Step 6
We can use ls to verify that they have been deleted:
Step 2
We have seen how all the files with that extension have been deleted completely. With the following command we can delete all the files that consist of a single character extension:
rm *.?
Note
If any of the files to be deleted is protected against writing, we will observe a warning to request its deletion, it is enough to confirm it to apply it.
Note
When executing the deletion of files there will always be the fear of deleting a file that should not be deleted but still, TechnoWikis will dispel this fear. For this we must use the option -i (interactive), with it the confirmation of each file will be requested, this will prevent us from deleting something that we should not.
Otherwise, if we want to force the deletion without asking, we will use the option -f (force), this option does not require confirmation even if the files are write-protected.
3. How to delete directories with the rm command in Linux
As we indicated at the beginning of this tutorial that the default rm command only deletes files but not directories, but this does not mean that it is not possible, if we want to delete an empty directory, we will use the -d (directory) option. The use of wildcards (* /?) in the directory names is valid.
Step 1
Then, to delete an empty directory we will execute the following:
rm -d directory
Step 2
We list the directories and after the removal we verify that it has been deleted:
Step 3
When we indicate more than one directory name, all empty directories selected will be deleted.
In case we want to delete one or more directories that are not empty, we must resort to the option -r (recursive)., This option will remove the directories and all the files and subdirectories that are hosted in that directory:
rm -r directory
Step 4
To verify this, first we will use the -dy parameter and we will receive the message that this directory can not be deleted because it is not empty, then we execute rm with the -ry parameter and we will see that it will be erased:
Step 5
Now, in case the directory is write protected, Linux will send a warning to confirm the elimination of this, to eliminate directories that are not empty and to suppress these indications, we must combine the options -r (recursive) and -f ( force):
rm -rf directory
Step 6
We can see how this command is effective and direct, so to avoid headaches it is ideal to understand the way the directory or directories are structured in Linux, for this it is ideal to use the tree command which we can install by executing the following line:
sudo apt install tree
Step 7
Once installed, we can go to the desired route and from there execute "tree" to display the structure of that directory. There we can see in detail that it hosts each directory without committing irrecoverable errors.
4. How to delete directories with the rmdir command in Linux
Step 1
Now, an exclusive command to erase directories is rmdir, to erase an empty directory simply execute the following:
rmdir directory
Step 2
If you want to delete multiple directories, simply enter them in the desired order:
rmdir directory1 directory2 directory3
Step 3
When trying to delete a folder that is not empty, rmdir will display the following message, this is ideal since we receive a warning before deletion, we should thank for this:
Step 4
Note that the error is generated and the deletion process automatically stops, so if we have multiple directories to eliminate the work would be stopped there, to avoid this we can use the option --ignore-fail-on-non-empty which omits the error and continues with the empty directories.
Another option to use is to use the -p (parent) option which deletes a directory and in the process removes its parent directories, the rmdir command starts with the destination directory and then goes back to the parent directory. In this case we can execute:
rmdir -p TechnoWikis1 / TestsTechnoWikis
Thanks to the rm and rmdir commands we will have the ability to delete files and directories in an integral way in any Linux distribution..