Delete files or directories on linux

This is a simple guide to file deletion in Linux using command line

I will try to put all command recipes I've ever needed, and whenever I use something other than commands mentioned here to delete some files, I will find a place for it in this article.

Commands we will use

rm
remove files or directories.
ls
list directory content.
find
find files and execute commands on them.
xargs
use standard input to execute command lines.
grep
print lines (from a file or stdout) matching a pattern.
mkdir
create new directories.
touch
update access and modification time of files (also used to create empty files).

Please note that all commands was tested only on bash (the default Linux shell) as some features may not be available (or be written differently) in other shells.

Permissions needed

To delete a file you (current user) only need to have write and execute permissions on the parent directory of that file (you do not need any permissions on the file itself).

for example if you need to delete a file /home/user1/shares/filename.ext then you need to have write and execute permissions on /home/user1/shares and you do not need any permissions on filename.ext itself.

Please note that if you do not have read permission on the parent directory, you won't be able to list files there (using ls or find for example), you will still be able to delete a file if you know its name or using wild-card, but please notice that any command you find here that contains "ls" or "find" commands will need you to have read permission on the parent directory of the file you want to delete.

Also note that for a non empty directory to be deleted it has to either be writable and executable by the current user, empty directories will be deleted if current user have write and execute permissions on the parent directory. (this goes recursively, you can use ls -lR to check permissions of all files, directories and directory contents under current directory)

Try first

The rm command is one dangerous fellow, try to be extremely cautious when dealing with it especially with the root user, so I recommend you try the command you are gonna use before using it, here is an example for a fast test setup :

$ mkdir testdir; cd testdir
$ touch one two three four; mkdir five six

You can repeat the second line whenever you need to re-establish this test file structure, now you can start testing on these files and directories you just created (still you need to be cautious though. Generally, avoid playing with rm command with root user permissions in hand)

Normal deletion

$ rm filename.ext

Delete the file named "filename.ext" in the current directory.

$ rm -f filename.ext

This will force deletion, no prompt (confirmation), it means don't bother me if i do not have write permission on the file or even if the file does not exist.

$ rm -v file1.new file2.old

Delete two files under current directory, -v means tell me what you are doing (verbose)

$ rm -i file.txt movie.avi image.jpg

Delete three files in the current directory, -i means prompt(confirm) before each file.

Delete Directories (Folders)

$ rm -r dirname

Delete the directory "dirname" (that resides under current directory) and all its contents recursively. some notes about this command line :

  • The file "dirname" doesn't have to be a directory but if it is it will be dealt with the right way
  • This needs you to have the proper permissions over all non-empty sub directories of "dirname"
$ rm -rvfi dirname

Here using these options makes sense (-v outputs file names and if they where deleted or not, -f will force deletion as some files may be problematic, and -i ask us before every file is deleted whether we are really willing to delete it or not)

Delete list of files

Delete a list of files matching a wild-card pattern

$ rm -f ./*

Delete all files in current directory (Be extremely careful when working as root)

$ rm -f ./.codefile.sw*

Delete all swap files(.swp, swo, ..etc) of the file named codefile (usually used by vi/vim)

Delete list of files from another file

$ rm -vfr `cat rmfile`

Delete files listed in the file "rmfile" (-v will tell you what is being done, -f will assume yes to all confirmations and ignore non existent files, and -r will deal with directories recursively)

Delete all files except one

$ rm -rf $(ls | grep -vx filename)

Delete all files and directories under the current directory except the one named "filename".

$ rm -rf $(ls 2>/dev/null | grep -vx filename)

If you expect ls to return errors sometimes (so we suppress stderr).

$ rm -rf $(ls -A 2>/dev/null | grep -vx filename)

Also delete hidden files and directories.

$ find ! -name filename -print | xargs rm -fr

Delete all files and directories under the current directory except the one named "filename", this will also delete hidden files and directories.

$ find ! -name filename -delete

Same but without using xargs, this is better in normal cases.

$ find ! -name filename -type f -delete

Delete only regular files under the current directory except the one named "filename".

$ find ! -name dirname -type d -delete

Delete only directories under the current directory except the one named "dirname".

Delete based on name

Delete files with name matching wild-card pattern

This is the same method used in deleting a file list using a wild card pattern.

$ rm -f go*

Delete files starting with "go" under current directory.

$ rm -f *go

Delete files ending with "go" under current directory.

$ rm -f .*.sw*

Delete all swap files under current directory.

Delete files with names matching regular expression

$ rm -fr `ls | grep -E \(go\)\(-\([[:digit:]]\)\{1,2\}\)\{1,3\}`

Delete files and directories matching regular expressions (like: go-10-1-0 , go-81-90-7 or go-1-12-11) under current directory.

$ find ./ -regextype posix-egrep -regex \(./go\)\(-\([[:digit:]]\)\{1,2\}\)\{1,3\} -delete

Delete files and directories matching regular expressions (like: ./go-10-1-0 , ./go-81-90-7 or ./go-1-12-11).

Delete files based on modification, access, or change time

Delete files last modified in the last 15 minutes

$ find -mmin -15 -type f -delete

Delete regular files under current directory that was last modified in the last 15 minutes (remove -type f to delete all files and directories)

Delete files last accessed more than 30 minutes ago

$ find -amin +30 -delete

Delete files and directories (including hidden ones) under current directory that was last accessed before 30 minutes (or more).

Delete files with status (content, name, permissions, or ownership) last changed before specific time

$ find -cmin 5 -delete

Delete files and directories (including hidden ones) under current directory with status last changed exactly before 5 minutes.

Delete files that was last modified in a specific range of time

$ find -mmin -90 -mmin +60 -delete

Delete files and directories (including hidden ones) under current directory that was modified before 60 minutes but not before more than 90 minutes(if it is now 16:00 this means : delete files modified between 15:00 and 14:30).

Delete based on inode index

$ find -inum 1347507 -delete

Delete the file with this inode index.

Delete files that has specific content

Delete files that has content matching a regular expression

$ rm -vf `grep -Elr \([[:digit:]]\{3\}\ \)\{2\} .`

Delete files that contains a text matching the above regular expressions (will match two consequent three digit number separated by a single space), this will also search sub directories and hidden files, and hidden sub directories.

Archive
Web
Linux