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.
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.
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)
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)
$ 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.
$ rm -r dirname
Delete the directory "dirname" (that resides under current directory) and all its contents recursively. some notes about this command line :
$ 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)
$ 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)
$ 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)
$ 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".
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.
$ 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).
$ 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)
$ find -amin +30 -delete
Delete files and directories (including hidden ones) under current directory that was last accessed before 30 minutes (or more).
$ find -cmin 5 -delete
Delete files and directories (including hidden ones) under current directory with status last changed exactly before 5 minutes.
$ 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).
$ find -inum 1347507 -delete
Delete the file with this inode index.
$ 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.