Hot answers tagged

384 votes

Delete files older than X days +

Be careful with special file names (spaces, quotes) when piping to rm. There is a safe alternative - the -delete option: find /path/to/directory/ -mindepth 1 -mtime +5 -delete That's it, no separate ...
basic6's user avatar
  • 5,995
151 votes

Have rm not report when a file is missing?

Use the -f option. It will silently ignore nonexistent files. From man rm: -f, --force ignore nonexistent files and arguments, never prompt [The "never prompt" part means that (a) -f ...
John1024's user avatar
  • 72.9k
122 votes

Why does the following script delete itself?

The kernel interprets the line starting with #! and uses it to run the script, passing in the script's name; so this ends up running /bin/rm scriptname which deletes the script. (As Stéphane ...
Stephen Kitt's user avatar
106 votes

Deleting billions of files from a directory while seeing the progress as well

You can use rm -v to have rm print one line per file deleted. This way you can see that rm is indeed working to delete files. But if you have billions of files then all you will see is that rm is ...
Lesmana's user avatar
  • 26.6k
104 votes

Why is rm allowed to delete a file under ownership of a different user?

The reason why this is permitted is related to what removing a file actually does. Conceptually, rm's job is to remove a name entry from a directory. The fact that the file may then become unreachable ...
Celada's user avatar
  • 42.7k
103 votes

How to get rid of "No match found" when running "rm *"

This behaviour is controlled by several of Zsh's globbing options. By default, if a command line contains a globbing expression which doesn't match anything, Zsh will print the error message you're ...
Stephen Kitt's user avatar
90 votes

Why can't I remove the '.' directory?

Removing the current directory does not affect the file system integrity or its logical organization. Preventing . removal is done to follow the POSIX standard which states in the rmdir(2) manual page:...
jlliagre's user avatar
  • 60k
78 votes

How can I harden bash scripts against causing harm when changed in the future?

set -u or set -o nounset This would make the current shell treat expansions of unset variables as an error: $ unset build $ set -u $ rm -rf "$build"/* bash: build: unbound variable set -u and set ...
Kusalananda on strike's user avatar
75 votes

Why is the wild card character * so different between the zip and rm commands?

You've explained the situation very well. The final piece to the puzzle is that unzip can handle wildcards itself: ARGUMENTS file[.zip] ... Wildcard ...
Jeff Schaller's user avatar
  • 66k
69 votes

I just deleted everything in my home directory. How? And why are some files still there?

Ouch. You aren't the first victim. What went wrong? Starting in your home directory, e.g. /home/felix, or even in /home/felix/src or /home/felix/Downloads/src. cd ../Dependencies/cpython Failed ...
Gilles 'SO- stop being evil''s user avatar
68 votes

Why can't I delete this file as root?

From root user check attributes of files # lsattr if you notice i (immutable) or a (append-only), remove those attributes: # man chattr # chattr -i [filename] # chattr -a [filename]
Invoker's user avatar
  • 1,363
68 votes

How do I pipe ls to grep, and delete the files filtered by grep?

This almost made me wince. You might want to stop pointing that shotgun at your foot. Basically any kind of parsing of ls is going to be more complicated and error-prone than established methods like ...
l0b0's user avatar
  • 50k
65 votes

What is the difference between 'rm' and 'unlink'?

POSIX specifies that the unlink utility calls the C library unlink function and nothing else. It takes no option. If you pass a valid path name to something which isn't a directory, and if you have ...
Kaz's user avatar
  • 7,176
55 votes

How do I pipe ls to grep, and delete the files filtered by grep?

You had the right idea, just missed some details. Since you're dealing with a list coming to STDIN and rm expects parameters, you need to use xargs. Thus: ls | grep chrome | xargs rm Should give ...
John's user avatar
  • 16.5k
54 votes

How can I delete a file named ">"?

Any of these should work: sudo rm \> sudo rm '>' sudo rm ">" sudo find . -name '>' -delete sudo find . -name '>' -exec rm {} + Note that the last two commands, those using find, will ...
terdon on strike's user avatar
50 votes

Deleting specific files based on filename from terminal

You don't need a loop or extra commands where you have Bash Shell Brace Expansion. rm -f rho_{0..200000..5000}.txt Explanation: {start..end..step}. The -f to ignore prompt on non-existent files. P....
αғsнιη's user avatar
  • 40.7k
50 votes

Accidentally ran sudo rm /* on my Arch Linux installation

Arch Linux has four symbolic links in /: bin -> usr/bin lib -> usr/lib lib64 -> usr/lib sbin -> usr/bin You should be able to recreate them (using a Live-USB or an emergency shell) or by ...
stefan0xC's user avatar
  • 1,498
47 votes

How to quote arguments with xargs

I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it. find . -size +1M | xargs -I {} rm "{}"
dee-see's user avatar
  • 579
47 votes

Why does rm manual say that we can run it without any argument, when this is not true?

The standard synopsis for the rm utility is specified in the POSIX standard1&2 as rm [-iRr] file... rm -f [-iRr] [file...] In its first form, it does require at least one file operand, but in ...
Kusalananda on strike's user avatar
46 votes

How to unlink (remove) the special hardlink "." created for a folder?

It is technically possible to delete ., at least on EXT4 filesystems. If you create a filesystem image in test.img, mount it and create a test folder, then unmount it again, you can edit it using ...
Stephen Kitt's user avatar
43 votes

Cannot remove file: "Structure needs cleaning"

That is strongly indicative of file-system corruption. You should unmount, make a sector-level backup of your disk, and then run e2fsck to see what is up. If there is major corruption, you may later ...
DepressedDaniel's user avatar
40 votes

How do I pipe ls to grep, and delete the files filtered by grep?

You can also use the find command with a wildcard: find . -maxdepth 1 -name '*chrome*' -delete Note that the "-maxdepth" argument ensures that find only works in the current directory, and doesn't ...
chander's user avatar
  • 590
39 votes

What is :>filename.txt Doing?

As you have discovered, this just empties the file contents (it truncates the file); that is different from rm as rm would actually remove the file altogether. Additionally, :>file.txt will ...
jesse_b's user avatar
  • 35.4k
38 votes

Is it possible to determine the progress of an rm command?

from man rm use the -v option: -v, --verbose explain what is being done
xyx's user avatar
  • 707
35 votes

rm -rf all files and all hidden files without . & .. error

Just realised this is the most convenient way in most Linux distros: ls -A1 | xargs rm -rf where -A = list everything except . and .. -1 = put every item in one line
godzillante's user avatar
35 votes

Is `yes | rm -r` safer than `rm -rf`?

Short answer No. However, I really like your creativity of piping commands together. Still, yes|rm -r is a nice example of UUOC (useless use of cat) - an acronym (or better jargon) for command line ...
Lutz Willek's user avatar
35 votes

Why doesn't rmdir work recursively?

Unlinking directories was originally a privileged operation: It is also illegal to unlink a directory (except for the super-user). So rmdir was implemented as a small binary which only removed ...
Stephen Kitt's user avatar
33 votes

Remove files newer than X day and hour

You might want to use find -newermt. Make sure to review files to be removed first: find /usr/include -type l -newermt "Jun 2 22:27" Use -delete to perform actual removes. find /usr/include -type ...
yaegashi's user avatar
  • 12k
32 votes

Since when do the POSIX and GNU rm not delete /?

You can find the HTML version of all the editions of POSIX 2008 online: original: TC1 (2013 edition) http://pubs....
Stéphane Chazelas's user avatar
31 votes

Deleting files with spaces in their names

You can use standard globbing on the rm command: rm -- *\ * This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- ...
Stephen Kitt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible