How To Remove Files in Linux with RM and SHRED

Table of Contents

All those files we create day in and out have to get cleared up somehow, which is why we have rm and shred. rm lets us remove files or directories in super simple way. shred goes further than rm by overwriting the file contents, with the option to also remove the file. Learning about these two commands and how to employ them is essential for anyone who uses a Linux system. Whether you’re already a data removal expert or not, we’ll provide you a great overview of both commands.

rm basics

rm is incredibly straight forward, and the syntax is super simple!

				
					rm file-to-delete.txt
				
			
Subsequent files & directories can be chained and separated via spaces.
				
					rm foo.txt bar.txt 
				
			
or you can batch remove files using a wildcard, like so:
				
					rm *.txt
				
			
Very clear and straight forward, but what if we wanted to remove directories?

Advanced rm usage

By default rm doesn’t remove directories the same way it would do a singular file. Go ahead try it.

				
					mkdir testdir
rm testdir 
//rm: cannot remove 'testdir': Is a directory
				
			

Bummer, but this is fixed by passing the -r option to rm. This option will remove the directory and its contents recursively. Now if you wanted to play it safe when deleting, you’d also pass the -i option or --interactive along with -r to receive a prompt before deleting each item.

				
					mkdir one two three four five six seven eight nine ten

#prompt for every removal
rm one two -ri
#rm: remove directory 'one'? 
#rm: remove directory 'two'? 

#prompt once
rm three four -r --interactive=once
#rm: remove 2 arguments recursively? 

#never prompt
rm five six -r --interactive=never
#or 
rm seven eight -rf

#remove empty dirs
rm * -d
				
			

There you have a handful of ways of remove directories from your system. One more way of removing files using the prompt, is -I which asks once before removing more than 3 files or recursively.

				
					touch one two three four five six seven eight nine ten
rm * -I
#$: sure you want to delete all 10 files in ~ [yn]? y
#rm: remove 10 arguments? y
				
			

Below you can find a summary table of the above commands with a couple of extras. Note that if you use rm to remove a file it is possible to perform data recovery on that file/directory. Thus if you are really trying for security we would use shred.

Option Action
-f, —force
Ignore nonexistent files and arguments, never prompt
-i
Prompt before every removal
-I
Prompt once before removing more than three files, or when removing recursively
--interactive[=WHEN]
Prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
-r, -R, —recursive
Remove directories and their contents recursively
-d, --dir
Remove empty directories
-v, --verbose
Explains what is being done
--help
Display help text

shred

Unlike rm, shred does not remove files primarily, it overwrites their contents making it unrecoverable, with file removal being optional. Fortunately, like rm, the syntax for shred isn’t complex.

Basic shred

As stated earlier, by default shred does not remove the file, instead it will overwrite the contents of the file with garbage data rendering the data contained within destroyed. An example of this can be seen below:
				
					touch test && echo "test content" > test
cat test
#test content
shred test 
cat test
#�B����D�ڑ��
#�n^U�7��J,��@��Xl!z���/���3�@GV3>�"R�9ȭ,3�5����"�����&^�N�}S�(FN�K�޻l���7�2�c �2��i���C1�2Sѭݻ
#...
				
			
Here we can see after the shred is performed that test file is corrupted beyond recognition.

Advanced shred

To also delete the file all we need to do is pass the -u option that tells shred to delete the file after shredding.
				
					touch testtwo
shred testtwo -u
				
			

For the more paranoid among us, you can also increase the number of overwrites performed using the -n 4 option, which tells shred to overwrite the file 4 times, this can be increased to any number of times. Another option that modifies the overwrite behavior is -s K which shreds the files contents by byte, and can be specified in K(ilo), M(ega), and G(iga) sizes.

				
					touch testthree
shred testthree -n 4 -s K -u -z
				
			

Finally -z tells shred to add a final overwrite of zeros instead of the garbage data we saw in the first example, which is a telltale sign that the file has been shredded. I’ve provided another summary table below covering the options we covered today as a handy reference.

Option Action
-f, --force
change permissions to allow writing if necessary
-n, --iterations=N
overwrite N times instead of the default (3)
-s, --size=N
shred this many bytes (suffixes like K, M, G accepted)
-u
De-allocate and remove file after overwriting
-v, --verbose
Just like rm’s verbose, this explains what is being done
-z, --zero
add a final overwrite with zeros to hide shredding
--help
like rm, displays help text

Conclusion

That was rm and shred two essential data removal commands to add to the terminal toolbox! Just one word of precaution – take care with rm and shred as they can result in accidental data loss if you don’t know what you are pointing at so double check your file paths! Please note that the tables I’ve provided are just overviews, use the proper --help argument on either rm and shred to see more obscure options. Thanks for reading and If this post was helpful don’t forget to subscribe to stay up to date with our posts!

Meet the Author

boxes-header
Boyd Gordon
GNOME Boxes with KVM

Ever need a VM ASAP without having to find the ISO file yourself? Today we will explore how spin up a VM at the push

View Post

Leave a Reply