155
votes
Accepted
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 ...
122
votes
Accepted
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 ...
106
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 ...
106
votes
Accepted
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 ...
104
votes
Accepted
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 ...
90
votes
Accepted
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:...
78
votes
Accepted
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 ...
75
votes
Accepted
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:
http://www.info-zip.org/mans/unzip.html
ARGUMENTS
file[.zip]
...
Wildcard ...
69
votes
Accepted
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 ...
68
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 ...
68
votes
Accepted
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 ...
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 ...
51
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 ...
50
votes
Accepted
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....
48
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 "{}"
47
votes
Accepted
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 ...
46
votes
Accepted
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 ...
44
votes
Accepted
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 ...
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 ...
39
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
39
votes
Accepted
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 ...
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
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 ...
35
votes
Accepted
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 ...
32
votes
Accepted
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: http://pubs.opengroup.org/onlinepubs/9699919799.2008edition/utilities/rm.html
TC1 (2013 edition) http://pubs....
31
votes
Is it possible to determine the progress of an rm command?
I see the question is old. I want to share what it works for me to maybe help some else.
I get the progress bar using pv command line Pipe Viewer
This is the command
rm -rv DIR_OR_FILE_NAME | pv -l ...
30
votes
Accepted
Why doesn't find . -delete delete current directory?
The members of findutils aware of it, it's for compatible with *BSD:
One of the reasons that we skip deletion of "." is for compatibility
with *BSD, where this action originated.
The NEWS in ...
30
votes
Accepted
Why doesn't find/rm -iname '*phpmyadmin' delete phpMyAdmin-Version-XYZ.zip?
The problem is that you are matching a file that ends in phpmyadmin (case-insensitively) by using the pattern *phpmyadmin. To get any file that contains the string phpmyadmin (case-insensitively), use ...
29
votes
Deleting billions of files from a directory while seeing the progress as well
Check out lesmana's answer, it's much better than mine — especially the last pv example, which won't take much longer than the original silent rm if you specify /dev/null instead of logfile.
Assuming ...
29
votes
Accepted
How to force creation of a symbolic link?
In GNU's ln, there is ln -n, which would allow re-pointing a symlink:
$ mkdir dir1 dir2
$ ln -s dir1 sym
# dir1/
# dir2/
# sym -> dir1/
$ ln -nsf dir2 sym
# dir1/
# dir2/
# sym -> dir2/
BSD ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rm × 834files × 147
bash × 132
linux × 131
find × 122
directory × 102
shell × 80
permissions × 64
shell-script × 60
command-line × 58
filenames × 37
wildcards × 34
delete × 33
filesystems × 31
macos × 31
data-recovery × 22
symlink × 21
ubuntu × 19
date × 19
grep × 18
recursive × 18
ls × 14
mv × 14
xargs × 13
deleted-files × 13