Now that you know how to create files and directories, how to copy, move and rename them, it's time 1
to see how to remove them. 2
In Linux to remove files or directories from the command line use the rm command. Be extra careful when 3
removing files or directories, because once the file is deleted with the rm command it cannot be 4
easily recovered. 5
There isn't such a concept as the recycle bin or trash for the files that were removed from the command 6
line. 7
To remove one or more files, give them as arguments to the rm command. 8
I'll remove this file abc.txt and a.txt from dir2. 9
So rm the first file, I'm using a correct relative path, dir10 abc.txt and the second file, dir10 10
dir2 a.txt Always use the tab key for the completion. 11
I'm doing it myself as well. 12
Note that the files were removed immediately without prompting. 13
To prompt for confirmation when removing a file use the -i option; it is the same option we've seen 14
at cp and mv comments and comes from interactive. 15
It will cause rm to ask for confirmation. 16
If confirmation is given, the file is removed; and If it’s not given the file removal is skipped. 17
Let's try to remove b.txt fromdir2 using the -i option: rm -i and the file 18
dir10/dir2/b.txt 19
And it's asking for my confirmation and I want to remove it. 20
To show more information when removing a file add the -v option, which comes from verbose: rm 21
-v/dir10/b.txt I am removing this one and it has displayed a message about what he did. 22
Let's see how to remove directories; if you try to remove a directory as you would a file, you'll get an 23
error. 24
rm dir10/ 25
"cannot remove dir10; it's a directory". To remove the directorie, use the -r or -- 26
recursive option to make removal recursive. 27
So rm-r 28
dir10 29
Sorry, -r not -f 30
If you try to remove a right protected file with rm by default, it will prompt for your confirmation. 31
As root I'm creating, a new file in the current working directory. 32
This file belongs to root but the student user is allowed to remove it because it's in its home directory. 33
rm a.txt and it's prompting for my confirmation: " remove right protected regular empty file: yes 34
or no", and it was removed. 35
Just imagine that you have to remove hundreds of files from a directory and you have to confirm each removal. 36
It's impractical! To force removal and not receive a prompt pass the -f option. 37
-f stands for force, like rm -rf Music/ 38
and Pictures/ I'm removing 2 directories that are in my current working directory and they were removed. 39
Sometimes it's common to combine our r and f together to remove both files and directories without 40
a confirmation, like I did in the last example. 41
I'd like to say a little bit more about this command. If you are run it as root and make a mistake 42
then your files are gone. 43
You cannot recover them. 44
As I said before, there isn't such a thing as a recycle bin, a trash or the undo remove command; to stay 45
in the safe zone 46
my advice for you is: pay special attention when removing files as root and always, but always, use tab 47
for auto completion. 48
Let me show you an example of why you should do that. 49
Let's suppose that as root you want to remove an ordinary file from the user's home directory. 50
I'm creating a file and I want to remove it 51
as root. 52
sudo rm-rf and the path to the file 53
So /home/student.txt I'm using an absolute path. Maybe the root user is not in the user's 54
home directories, 55
so in /home/student. 56
Of course, I have used the tab key for auto completion. 57
But suppose you say that it's not necessary to use tab for auto completion because you can write quite 58
fast anyway; and by mistake, you write the white space after /home like this. 59
There is such a probability, especially if you do it often enough. What will happen is that the rm command 60
will consider that it has two arguments: /home, the first directory and then another file called a.txt 61
which resides in a directory called student and will try to remove them both. 62
Such a mistake is disastrous because it will remove the entire /home directory. 63
This one ! Such a mistake could be avoided by always using the tab key. 64
If you by mistake type a whitespace somewhere in the path, then tab will not complete the path. 65
I'm pressing tab and it's not completing the path. 66
There is a mistake. 67
My second advice is to pay special attention when using pattern matches and wild cards like asterisks, 68
dots or question marks. 69
This is also applicable to other possible destructive commands, not only rm. 70
Before running a command like rm *.txt execute echo and the pattern, to see exactly what are the 71
files that will be matched. Sometimes these patterns expand in names you don't expect to. So 72
before executing this command 73
execute echo and the pattern 74
to see which files will be matched. 75
And it's OK, there is a file called a.txt, a single file. 76
At the end, I'll tell you something really important: the rm command does not actually delete a file. 77
Instead, it unlinks it, meaning the data is still on disk. 78
The space on disk occupied by the deleted file is marked as being free and can be used by other file 79
or process. Now depending on different factors that are professional solutions that make the recovery 80
of the deleted files possible. 81
This is available to any other operating system, including Windows, Mac or Android. 82
So if you remove a file, you can't be 100 percent sure that no one will ever recover it. 83
This is, for example, just a free tool that can recover deleted files; you can write on Windows. 84
What you can do is use the shred command instead. shred overwrites a file to hide its contents many times 85
before removing. Let's see 86
an example. I'm copying the password file from etc to the current directory. 87
shred -vu -n 100 and the file Great! 88
-v means verbose or showing the process -u means removing the files after overwriting. 89
and -n and an integer number N, overwrites the file N times instead of the default, which is 3. 90
The file was overwritten with random contents before removing. In my opinion, even though shred is 91
good enough in the secure removal of files, if you want to keep your confidential data secure, you 92
have to encrypt it. 93
You cannot rely on simply deleting a file and suppose that no one could ever recover and access it.