Tag Info

New answers tagged

0

The answer is not by default. Let me add that the risk can be more subtle than your bare-bones example. Take this example: [user@server ~]$ sudo rm -rf directory_to_delete /* The accidental space between the directory_to_delete and the /* means that it's going to operate on both the directory_to_delete and /*, the root folder, as the root user. And ...


1

I teach everyone about the !$ = last argument in the last command trick: % ls job[XYZ].* jobX.out1 jobX.out2 [rest of the matches] % rm !$ This allows an inspection of the wildcard expansion, and then use of the exact same glob pattern without the possibility of inserting a space before the *. Also I suggest people never cut and paste a globbing ...


6

If I am in a situation where deleting the wrong files is a really big deal, one of the things I've done is make a trashcan folder, like mkdir trashcan, and then I have a script rmTrashcan which has a rm -rf trashcan/* or rm -rf * or similar, written very carefully and checked several times. That way, if I make a mistake, the mistake is on a mv command, not ...


0

As I suggested in a different question, using a DEBUG trap that checks BASH_COMMAND will do the trick: check_for_wildcard_rm() { local rm_glob_re='^rm[[:space:]](.*[[:space:]])?[*]([[:space:]]|$)' if [[ $BASH_COMMAND =~ $rm_glob_re ]]; then echo "Suppressing rm with a bare *" >&2 return 1 fi return 0 } shopt -s extdebug trap ...


2

rm has -i and -I flags to confirm before every removal. In the past, some distributions have turned them on by default. This is a terrible idea. Give the user too many confirmation dialogs for normal operations and they'll begin to habitually confirm them. This just shifts the requirement to "be careful" (always a red flag) to a new and more annoying ...


4

Instead of bothering with two commands (ls and rm), I think a simpler and easier way is to use find: find . -maxdepth 1 -name "string*" -delete If you accidentally type "string *" it will delete files named string chars and string letters (which is what you wanted anyway), but it won't grab every file like * in a shell will. Also, you can leave off the ...


33

A DEBUG trap could be written to cancel commands that look suspicious. The following, or code similar to it, can be added to your ~/.bashrc: shopt -s extdebug checkcommand() { if [[ $BASH_COMMAND = 'rm -r'*' *' ]]; then echo "Suppressing rm -r command ending in a wildcard" >&2 return 1 fi # check for other commands here, if you like ...


0

Everyone seems to be saying, "Be more careful", "Don't make an alias/confirmation prompt because you'll become habituated to not paying proper attention to it". Cool, and everything. I mean, I don't think you should make an alias for rm (nor rmrf, since you could easily screw that up and type the real command). But why can't you make an alias/script and ...


48

There is no way to a totally bulletproof a system. And adding "Are you sure?" prompts to things is both counterproductive and leads to "Of course I'm sure." kneejerk reactions. A trick I picked up from a book in years past is to first do ls -R blah* then, do rm -fr blah* if and only if the listing that ls turned up hit what I wanted hit. It's easy ...


2

Try composing your initial rm command without the -f flag, but with -i instead, such that rm will prompt you for each file it intends to delete. For small recursive deletions, you can hold down the y key, once you're sure the command has been typed correctly. For large deletions, you can abort the operation, and use commandline history to carefully change ...


3

Is there a simple way to smartly protect against an accidental trailing or leading wildcard? Not really. It’s proposed in another answer you could create a custom command to add a prompt before executing a task. But the problem with this custom command is it must be consciously installed on systems you are working on. And even if it is installed, the ...


8

Can you train yourself to use, say, rmrf in place of rm -rf? If so, this bash function will provide you a chance to see what would actually happen before confirming the command: rmrf() { echo rm -rf "$@"; read -p "Proceed (y/N)? "; [ "${REPLY,,}" = y ] && rm -rf "$@"; } To make this function permanent, add this line to the ~/.bashrc file on ...


0

The use of wildcards, as suggested in another answer, is often the faster way to select a subset of files from your directory, but not always it is possible to use it (e.g. in the previous case at least you have the extension suffix in understandable characters). rm -i *ng # means remove all files that finish for ng Another way can be to use the inode ...



Top 50 recent answers are included