Table of Contents

How to remove Files and Directories in Linux

 rm --help
Usage: rm [OPTION]... FILE..

To remove a file name just do

rm filename

Above command will remove the filename without asking unless you have the following alias set

alias rm='rm -i'

If you have the above alias, then it will ask you for the permissions as shown below.

rm filename
rm: remove regular file ‘filename’?

You can use -f switch to override the above behavior then, it will not ask for permissions even though you have alias set with -i option.

 rm -f fielname

You can also remove more than one files by using following commands...

rm filename1 filename2 filename3

To remove all files with extension pdf

rm *.pdf

To remove the empty directory use

rm -d dirname 

or use

rmdir dirname

If the directory is not empty; use rm -r

rm -r dirname

Above command will remove all the sub directories recursively.

If you dont specify the -f switch, it will prompt for permissions before deleting as shown below...

rm -r /tmp/*
zsh: sure you want to delete all the files in /tmp [yn]?

You can type y and Enter to delete. To suppress the above prompt. We can use -f swith as shown below.

rm -rf /tmp/

To remove multiple directories by name, we can do following...

rm -r dirname1 dirname2 dirname3

Related Posts