New answers tagged

43

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. See How programs get run for details of how this works in Linux; the comments on that article give details for other platforms. #! is called a shebang, you'll find lots of ...


0

You can use -f as it will, according to the man page: Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. also remember The -f option overrides any previous -i options. rm -f ...


2

As an alternative, you can use the find command with the -delete option. This will (obviously) only delete the files it finds so it never warns if none are. An example might be: find /var/log/triffids/ -name "*.log" -mtime 30 -delete which would search the /var/log/triffids/ directory and delete all files with the .log suffix which have not been modified ...


2

This is a hack, but you could always touch the file about to be removed. If it doesn't exist, it will be created so rm can remove it.


0

The filesystem in /Volumes that you mention is the installation virtual disk (dmg). Hence why it is read only. Umount/Eject it and it will be gone. You can do it via the graphical interface or via: sudo diskutil umount '/Volumes/Grammarian PRO2 X' Be also careful what you do with the /Volumes directory, because it is the root of all your mounted ...


7

If your requirement is that rm does not complain about missing files, but that you also want any other output from rm, my suggestion would be to first test for the target file's existence, and only call rm (without the -f flag) if the file actually exists. # rest of script # ... [ -e "$file" ] && rm "$file" # ... # rest of script Calling rm -f on ...


6

You stated that you still want to see other output from the rm command when you run it, but using the -f option will suppress those. You will need to filter STDERR for what you want, which is a little tricky, given you want everything else to show. You will want to use a command like this: rm -f filename 3>&1 1>&2 2>&3 3>&- | ...


0

There are two main ways: If you add the -f option, it will not show the "doesn't exist" error and two: (works for almost any command) instead of just doing "thecommand" do "thecommand 2>/dev/null" where "thecommand" is the command you're doing.


3

Check the --force (-f) option of rm: -f, --force ignore nonexistent files and arguments, never prompt


21

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 overrides any previously specified -i or -I option, and (b) write-protected files will be deleted without asking.] Example Without -f, rm will complain about ...


1

With rm, -f overrides -i if it comes later on the command line. Whichever option comes last on the command line will have effect, so you can override an alias rm='rm -i' just by using rm -f, which will expand to rm -i -f. e.g. $ mkdir rmtest $ cd rmtest $ touch a b c d e f $ alias rm='rm -i' $ rm * rm: remove regular empty file 'a'? n rm: remove regular ...


1

You can suppress the alias by escaping or quoting the command name, e.g., \rm foo "rm" foo Further reading: Bash Shell: Ignore Aliases and Functions When Running A Command


1

Given a path to the file ./some/where/thatcertainfile, stripping off the final /thatcertainfile gives you a path to the directory. Launch a shell to be able to use string manipulation on the path. find . -name thatcertainfile -exec sh -c 'rm -r "${0%/*}"' {} \; Alternatively, use zsh. To transform a path into the name of the containing directory, use the ...


0

Try this command: rm -rf $(find . -name thatcertainfile -execdir pwd \;) It should say to the rm -rf that what it had to remove is the output of your command. For example, if your command's output was /home/guest/Documents the command I showed would translate on rm -rf /home/guest/Documents.


0

I know this has been answered already, but I always use mc - Midnight Commander to delete awkward files that I am too afraid in attempting to delete. It is a "GUI" like interface where you simply highlight the file you want to delete, and delete it.


2

There wouldn't be any overwrite issues with rm but whatever the scenario, if you abort during the process, it will blindly stop where it was when it got interrupted. There is unfortunately no way of knowing where it stopped unless you previously made a listing of what should be removed.


3

In addition to frostschutz's double quotes method, and Andy's simple quote one, there are also the shorter: rm -r \~ and the relative path one: rm -rf ./~


5

In theory yes. In practice usually also yes. If you're calling a shell script or alias that does something weird, then maybe no. You could use echo to see what a particular command would be expanded to by the shell: $ echo rm -R ~ rm -R /home/frostschutz $ echo rm -R "~" rm -R ~ Note that echo removes the "" so you should not copy-paste what it prints. ...


5

As Kalvin Lee mentioned, you can cd to the directory and remove its contents, then use rmdir to remove the directory. I recommend this over the rm -R approach because you're less likely to fat-finger the command and blow away your home directory. Generally, you can put things that you don't want the shell to expand in single quotes. This will remove an ...


1

As long as you get the script right and you don't run it in a weird environment, everything should be fine. But if something breaks the expected output of date, all bets are off. For example, if you run this snippet with IFS=-, then you'll be running something like rm /media/… 04 26* i.e. delete files beginning with 26 in the current directory. Of course ...


0

Modern find (via POSIX, so the not-GNU find on OpenBSD does support this) can do the necessary work without a call to xargs via the -exec command {} + form. find ... -exec rm -rf '{}' \+


3

If you're using -print0 you should use the -0 flag to xargs so it will read the names correctly. find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -print0 | xargs -0 rm -rf alternately, if you have GNU find you could use the -delete flag, though it won't work if the directories are not empty find ~/.jenkins/jobs/subco/workspace/myproject/ ...


1

Since there is always a space in your filenames, I'd include that in your command: rm "$(date +%F --date "Yesterday") "* # Removes old clips That should be an easy way to prevent deleting all files in the directory, as even if date doesn't return anything, it would only delete files starting with a space character (which hopefully do not exist). However ...


2

Two things jump out: You have no checking for failure of the substitution There is a race condition if the date changes between uses of the date command. You could solve them both like this: #/bin/bash # Exit if any command fails set -e dir='/media/jmartin/Cams/video' day=$(date +%F --date Yesterday) # Conbine files from the past 24 hours into a ...


5

The $(date +%F --date "Yesterday") technically isn't a variable, it's a command substitution, but that is tangential to your question. This construct could prove to be problematic if for some reason the date command wasn't in your $PATH, and thusreturned nothing - at which point it would delete everything in /video/. If you instead take that command ...


0

rm -rf directory or rm -rf * of course is the fastest method unless your local rm implementation is broken. Using find gives no advantages. Whether this is fast or slow mainly depends on the filesystem and OS implementation. So the question seems to be inappropriate. UFS and ZFS on Solaris are known to be very fast with this kind of task as both ...


0

The correct command to disable the double verification seems to be setopt rm_star_silent. To enable the double verification do setopt no_rm_star_silent. For more detailed information look at the man page for zshoptions man zshoptionsor http://linux.die.net/man/1/zshoptions


1

Is this GNU only? No. This rm behavior dates back around 40 years and has been standardized. What's the rationale for this behavior? From the 1st edition man page for rm: Removal of a file requires write permission in its directory, but neither read nor write permission on the file itself. BUGS rm probably should ask whether a ...


2

Since you asked three separate questions, I'll answer them separately. Your first question: Is this GNU only? I'm not sure that it's GNU only. It seems to be a feature of GNU Coreutils' rm that is also found in at least some other rm documentation. For example, according to some old documentation at opengroup.org for POSIX rm: If file is not ...


2

plain user You might wish to protect a file from your own deletion/modification. A quick way is to chmod 000 foo (or chmod -w foo). However, this is of no use if you choose to delete it unwillingly. So rm command will inform you (as non root), if this is the case, that's why there is a confirmation request. root when running as root, an specially during ...


0

Possible problem with locked files in the .Trash directory? See this Apple support post, which suggests running chflags -R nouchg ... on the trash directory before attempting rm -rf.


3

You should use find for this. You can safely test with the following command: find . \( -name frontend -prune \) -o -type f \( -name \*.o -o -name '*.cm[oixa]' -o -name \*.cmxa -o -name \*.annot \) -print Once you're happy with the list of files that gets printed, run the real command to delete the lot of them: find . \( -name frontend -prune \) -o ...


-2

For file rm -f fileName rm -rf Directory or rm -rf FolderName



Top 50 recent answers are included