how to delete files in linux from command line

The most commonly used command in Linux to remove or delete files is rm. The rm command can be used to remove files as well as directories. However, by default the deletion of directories is not enabled.

The rm command removes each of the specified files. You can specify a list of files as arguments to the command. The files can be specified using either the absolute path or a relative path to the file location. You can also use wildcards to match files, as with most other Linux commands.

You will need to have appropriate permissions for the files that you want to delete. This means you will need write permissions to the parent directory/the folder where file exist. Just the read or execute permissions are not enough for the delete operation. You will see a permission denied message, if you do not have sufficient privileges.

If you do not have write permissions for the file, but have write permissions for the parent folder then rm will prompt you before removing the write protected files.

There are several useful command line options that can be used with the rm command. We will enumerate just a few and popular ones

-v or –verbose : prints out what is being done
-i or –interactive : prompt before the removal of each file
-f or –force : never prompt and ignore files that does not exist

Using rm command

In order to delete a single in the current working directory, you can use the file name as the argument to the rm command.

bash$ rm file1

You can specify multiple file names separated by a space as the argument in order to remove multiple files. You can provide either the absolute file path or a path relative to the current directory.

bash$ rm -v /path/to/file1 /path/to/file2 ./relative/path/tofile3

The above command will remove the three files that are specified in the command line argument. If you want to delete more files than you want to manually type out, then you can use wild cards to match and filter files.

Using wildcards

Wildcards or regular expressions can be used as command line argument to match multiple files. This is especially useful when you have a large number of files or when you don’t care so much about the file names.

bash$ rm -v /path/to/dir/*

The above command will delete all files in the folder /path/to/dir/, but will not remove any sub-directories or its contents. The above “*” matches all files at the location. For example, You can modify it to img-*.jpg to match all files with the file name that starts with img- and ends with an extension of .jpg.

You can also use the shell expansion features to match file names. For example, to match files that have sequential numbering, as in the case images downloaded from camera you can use the following command, which uses the brace expansion …

bash$ rm /path/to/imgdir/myimage-{25..300}.jpg

Using interactive mode

rm command does not verify the intention to delete, before it actually delete files. This means that it is very easy to accidentally delete files due to typos or bad wild cards etc etc. Although there is no fool proof of avoiding this, you can safe guard yourself by creating a shell alias for the rm command.

bash$ alias rm='rm -iv'

You can add this alias to the profile or shell configuration file (ie. .cshrc or .bashrc etc) so that the alias is set every time the shell loads. You can always override the alias by using -f command line option when you are really sure and want to avoid repeated confirmations.

Using indirection

Many times it is quite possible that you want to remove a large set of files, in various locations with very different file names that is difficult to match all of them using a regular expression. For example, the file set could be the output of another process or program. In that case, you can save this list of files in a text file and use that as an input to the rm command using indirection.

Let’s say you have a text file, named filenames.txt which contains the file paths as shown below. Each of the file is entered as a new line in the file. (be sure it does include the correct path, and not / or . or anything erroneous and stupid).

bash$ cat filenames.txt

You can now provide the file, filenames.txt in the command line and each of the files in the text file will be deleted.

bash$ rm < filenames.txt

Using find

The find command is a better option in finding and filtering files based on several different parameters or metadata. This makes it ideal, when you want to delete files based on some metadata other than the file name itself.

You can search and find files, based on any criteria you want and then use the rm command or the built in delete option to remove those files. In order to delete all files that has a file name starting with myImg, you can use the following…

bash$ find /path/to/folder -iname "myImg*" -exec rm -fv {} \ ;

If you have a very large number of files, then the above method can get quite slow. You can instead use a faster method, again using the find and rm commands. In order to delete files (and only files) that has been modified in the last 10 mins.,

bash$ find /path/to/folder -type f -mmin -10 -exec rm -fv {} +

Another option you have is to pipe the output of find to the rm command instead of using the -exec option of find. To delete all files that has a .log extension and has not been modified in the last 60 mins,

bash$ find /path/to/folder -type f -mmin +60 -iname "*.log" -print0 | xargs rm -fv

Yet another option is to use the built in command line delete option in find itself. So, if you want to delete all files that are larger than 30MB in size, you can do

bash$ find /path/to/folder -type f -size 30M -delete

All of the above methods will work mostly the same. Which of these methods you want to use will depend on the number of files you want to delete and how fast you want to do them.