Amazingly, even some very experienced Linux users quite superficially familiar with deleting parts of the file system in GNU/Linux. This is due to the high popularity of graphical environments and, as a consequence, “samoperedelny” under them, even those users who seemingly sits on a UNIX-like OS, even with the great-great-grandfather.
However, I am still going to remind you — and also yourself — how to safely delete files and directories in GNU/Linux using the command line.
In modern GNU/Linux distributions for the deletion of files and directories from the command line interface (CLI) answer of the command “rm” and “rmdir“.
As for GNU/Linux delete all files?
Let’s for example look at how to delete the file. Of course, first it must create.
Open your favorite terminal emulator and execute:
This will create a file “newfile.txt” in your home directory (by default, any new terminal session is opened in the user’s home directory, it is started).
The file is created!
Now, let’s remove it! To do this, use the command:
The file was deleted!
If you want to remove a file “newfile.txt“when a terminal session is opened in a directory other than your home, you need to specify the full path to our file. For example, like this:
Please note! The symbol “~” in UNIX-like systems — to which, of course, applies to GNU Linux often means the home directory of the active user. If we need to remove this same file, but on behalf of another user (for example, in the session of the terminal from the root), the path will look like this: “/home/username/newfile.txt“where “username“is the name of the appropriate user in the system.
If the file you want to delete write-protected before deletion you will be asked to confirm the action.
Confirm the deletion of the protected file
The command “rm” could be useful when you need to delete multiple files at once. Because the old file you just deleted, let’s create in our home directory several new test subjects:
cd ~ && touch test1.txt test2.txt test3.txt
The command will create in your home directory files “test1.txt“, “test2.txt” and “test3.txt” respectively.
Here they are, our files…
Exactly the same logic and in the mass removal: after the command “rm” to specify not one, but several files that we are going to destroy. For example:
rm test1.txt test2.txt test3.txt
This will remove all three files that you created.
The command “rm“, like most console utilities GNU/Linux supports the use of option (options, key — a lot of names). For example, in the previous example, every time we asked to confirm the deletion of each of these three files, you need to use one of these parameters:
rm-i test1.txt test2.txt test3.txt
Then the system, removing each file, it will ask again whether we want it to do, as if they were write-protected.
Then I make it so that the system does not ask us to delete is protected from modification files. For the experiment, create in your home directory a file “nodelete.txt” and at once protect it from the record. Open a terminal session and run:
touch nodelete.txt && sudo chmod 444 nodelete.txt
The first command will create a file, and the second the separator limit the right to use only the reading (by the way, more about the possibility of a great team,”chmod“, designed to change access rights to files and folders, you can read here).
If we try to delete a file without any options, we will ask again. However, with the right parameter (“-f“) this will not happen and the file will be deleted immediately.
The vicissitudes file “nodelete.txt”
By the way, if you want to remove, for example, all files with the file extension “.txt” in a directory — Paradice in it and run the command:
Erases only those files that have the appropriate extension. This method is sometimes called the removal of the mask or deletion with regular expressions.
This can be very poleznyak in GNU/Linux delete directory?
To remove directories, as already mentioned, use the command “rmdir“. At the same time, delete the directory — both empty and with any content — can “rm” with the correct parameter. Moreover, “rm” looks even more practical than “rmdir“, but for the sake of justice I will show you both ways.
So, first let’s create three directories:
cd ~ && mkdir Hello1 Hello2 Hello3
The first command will change the working directory of the terminal session on the home and the second will create a directory “Hello1“, “Hello2” and “Hello3“.
My home directory with a newly created “Hello1”, “Hello2”, “Hello3”
Offer for purity of experiment, something to put in one of them. For example,”Hello2“:
Now in the directory “Hello2” file exists “somefile.txt“.
Check whether we are still in the home directory:
The last command put into the screen address of the working directory
If for some reason there is, go into it (typing “cd” without any additional arguments or with “~“) and delete the first created directory using “rmdir“:
The directory was empty, so removed, nothing pereproshit. But in the case “Hello2” have a problem: there is file! The command “rmdir” will not be able to perform the tasks:
Not delete as directory is not empty
In this case, it is useful to operate the utility “rm” with the parameter “-r“. It can remove the directory, and several together as in the case of files and directories with the content for her — not a hindrance. Let’s remove the “Hello2“by resorting to these tools:
Well, at the end of the lesson I propose to create protected file in the directory “Hello3“, and then try to remove it.
This will help to reinforce knowledge and to understand the work used within the article tools, if someone still managed not to do:
touch Hello3/test.txt && sudo chmod 444 Hello3/test.txt
As you can see, the file is created and the changes it is protected
Making sure that the working directory of the session’s terminal is still home, try the following command to remove the directory “Hello3” with file “test.txt“we read only:
System, asking for confirmation, will remove the “Hello3” along with the contents. If you do not want to confirm the action, you can combine the familiar parameters:
rm -r-f Hello3
Directory easily removed, not even “tvknob”Finally…
GNU/Linux is a very smart operating system. She actively uses the ideas of a multi-user environment and separation of rights, typical of UNIX, so you will never be able to remove someone else’s file. The exception is the use of administrator rights (in the form of the utility “sudo“or directly as root): then you will be able to do whatever they want.
On my working computer has two user — bohdan (I) and dara (my bride). In the screenshot below you can see how I opened a terminal session with their own user switched to user your girl using the “su” and introducing her password (Oh, I hope she won’t!). Then I moved to her home directory — open a terminal session was still in my home directory, as you can see, and made sure in the success of the previous command with “pwd“. Next, I created a file “file.txt” and then, resorting to “sudo“limit the access to it. Remember, all of this happened under the user dara.
Take care of your passwords!
Next step I brought to the screen the contents of a directory with some parameters, but at the same time makes the exact search using “grep“in order to show you just soderzhimogo home directory my girl. The file was created, as you can see, right he — read-only, and for all.
After playing, I walked out of the session user dara command “exit” and then appeared again in the command prompt session own user — bohdan. Already from its name I was convinced that I’m me (command “whoami“), then tried to delete file “file.txt“and as you can see, the parameters “-r” and “f“. Naturally, the system immediately issued a denial of access: do not touch someone else! I gave up and repeated the previous command, but using “sudo“, that is using administrative privileges.
The last command — again, displaying the contents of home directory my girlfriend, using “grep“. Was looking for the same “file.txt“. Not found.
So always keep your passwords, because, knowing them, the system is very easy to hurt. If something happened, seemingly irreparable, be sure to read about how to recover deleted files in GNU/Linux.
Image: screenshots of the author from open sources