Remove files in Linux

ethanrodrigo

Ethan Rodrigo

Posted on August 2, 2022

Remove files in Linux

Hello folks! An alien here with another Linux blog.

image.png

Today let's talk about deleting files. No seriously, It may seem little bit trivial, yet I'm going to dive deep. Will you join with me?

img.gif

HEADS-UP: Following commands are for deleting files from the terminal, means it's not going to store in the trash. Therefore execute them on some unwanted files or just create a Virtual Machine.

rm

Our first command is the great old classic rm. But did you know there's more to the rm command.

The rm command doesn't let you delete directories (let me call it dirs now on) without specifying. The -d flag specifies delete empty dirs. Similarly you can use rmdir command to delete an empty dir(and it's the only use of rmdir). However to delete a dir that has content needs a specific flag which is -r or -R.

image.png

Not sure about what's being deleted? This can be occur while deleting dirs. You can use -i flag to prompt before deleting each and every file.

image.png

Sometimes while deleting, specially a file that's belong to the root, can prompt you for a confirmation as you're using -i flag. It's bothering, isn't it? Just use -f and it will delete the dir(s)/file(s) without asking. Still remember to use -r if it's a dir.

image.png

If you're familiar with my articles you may have seen my conclusion encourages you to run sudo rm -rf /* --no-preserve-root. It's a really good command that teaches you a lot of things, not only about Linux but also about life, suchlike don't trust bloggers on the internet.

Well... what does --no-preserve-root means? It tells the rm command no to treat the / (root) dir as a special one and let the rm deletes what it contains. But if you do not use --no-preserve-root it's not going to delete the / as it calls --preserve-root which is the converse by default. image.png

Yet, there's something called globbing in Linux, which prevents --preserve-root in rm. Frankly you don't have to use --no-preserve-root if you use globbing.

rm -rf /* means delete all the contents in the / dir, besides it uses globbing (the * character). Therefore it doesn't use --preserve-root, while rm -rf / means delete the /, which uses --preserve-root. Because of that if you do sudo rm -rf /* you don't have to use --no-preserve-root.

unlink

Speaking of deleting files in Linux we've also got unlink. unlink is not only a command but also a function in C (I know that PHP has that also, yet we don't do that here).

According to the GNU man page

You can delete a file with unlink...Deletion actually deletes a file name. If this is the file’s only name, then the file is deleted as well. If the file has other remaining names, it remains accessible under those names.

Let me explain it. Consider the meaning of the word unlink. Yes, it means removing a link from something that has a link. In Linux you can have multiple names for a single file. The creation of multiple names for a single file is called linking (Here I'm referring to Hard Links). This doesn't copy the file, it simply makes a new name (an alias) to the file.

In the following example I create a file in a dir named Test0 and create a link to that file from another dir called Test1. The file here in the dir Test1 is not really a file, but a link to the file hello in the dir Test0 image.png

When you do unlink (or even rm) on a file name which is a link to another file locates elsewhere will be removed, not the file. However if the file doesn't have a link, the file will be deleted, which is equal to the rm without frills.

find

Before seeing deleting files with our next command, find, let's have a look at what it really is.

find command

Here's what find is according to the Wikipedia;

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

As the name suggest the command find is used to find a file/dir. Additionally we can perform actions (another commands) on those found file/dirs.

Syntax;

find [the path you want to look to into] -options [what to find]
Enter fullscreen mode Exit fullscreen mode
  • The path - where the find command needs to exactly look into (use . to refer the current working directory)

  • What to find - the file/dir name or a regex pattern to get multiple results

Let me give you a brief idea about the find command with some examples.

Let's create 100 files that has some random values and name with another random value.

image.png

Now if I want to filter the files that has 10 in the name, I can do something as follow

find . -name "*10*"
Enter fullscreen mode Exit fullscreen mode

And this time I want only the directories to be filtered,

find . -type d -name "*10*"
Enter fullscreen mode Exit fullscreen mode

image.png

More about the find command can be found here.

delete with find

As this article is not about finding files, but deleting them, I'm stopping here with what else find can do. Instead let's see how the find command can be used to delete files.

NOTE: It's fine if you're seeking for one file and delete them. But be careful if you're using find with a regex pattern, as it's going to delete all the filtered files.

There are three ways to delete with the find command.

1. with -delete

Below I delete files and dirs that has 13 in the filename.

image.png

And order matters. I use -delete before the file name in the following example, which literally means find files in the current working dir and delete them, then filter the files that has 23 in the name.

image.png

2. with -exec The -exec flag of the find command is used to execute an action (a command) on the results. Therefore it's followed by another command, which in this case rm.

The syntax for rm with find's -exec flag as follows;

find [the path] -options [what to find] -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode
  • {} - refers to the current file that is being processed
  • \ - escape character for ;, it also can be quoted with double quotations (";")
  • ; - to indicate the end of the command

image.png

3. with xargs As for the Wikipedia;

xargs (short for "eXtended ARGumentS" [1]) is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

The command xargs just takes output from a command and pass it into another as arguments.

For the deletion with find, we can pipe it with xargs as following syntax;

find [the path] -options [what to find] | xargs -I {} rm "{}"
Enter fullscreen mode Exit fullscreen mode
  • -I - replace {} with the find's results.
  • rm "{}" - execute rm on filtered files.

image.png

shred

Now I give you a bomb. A bomb that can blast your Linux system or make your day even worse than what it is so far.

bomb.gif

This one is for destroying the file, or a hard drive. The shred command can be used when you need to destroy a file before deleting, so nobody can find what was there.

Try shredding a file and you will get something as following.

image.png

What the shred command really does is, it overwrites the content of a file. To make it even harder to recover with expensive recovering software, shred overwrites the file multiple times.

Not only files but hard drives also can be overwritten by the shred command. To overwrite a hard drive, just pass the path of the hard drive to the shred command. For example, execute following in order to overwrite the sda1 partition.

shred /dev/sda1
Enter fullscreen mode Exit fullscreen mode

According to the man page, shred overwrites the file three times. To change that you can use -n flag to repeat the overwriting process for n times. See how shred without frills works and with the -n flag. (The flag -v stands for verbose)

image.png

In addition to the above there, are few more flags, such as -f to change the permissions to allow writing if necessary. Here is the man page.

Conclusion

In this article we discussed about removing files and dirs from a Linux system. However what if you deleted something accidentally? What if you want something that you didn't want and deleted?. The answer will be in a next article. If you haven't subscribed to the newsletter, DO IT IMMEDIATELY! Then you would find my latest article on your inbox.

Thank you for reading! Now go and execute sudo rm -rf / --no-preserve-root and make tux happy. Until next time.

If you find this useful let's connect on Twitter, Instagram, dev.to and Hashnode.

💖 💪 🙅 🚩
ethanrodrigo
Ethan Rodrigo

Posted on August 2, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related