Hates_
Hates_

Reputation: 68681

What is the safest way to empty a directory in *nix?

I'm scared that one day, I'm going to put a space or miss out something in the command I currently use:

rm -rf ./*

Is there a safer way of emptying the current directory's contents?

Upvotes: 8

Views: 3199

Answers (13)

Aidy
Aidy

Reputation: 331

Here are the alias I am using on macOS. It would ask for every rm command before executing.

# ~/.profile

function saferm() {
    echo rm "$@" 
    echo "" 
    read -p "* execute rm (y/n)? :  " yesorno

    if [ $yesorno == "y" ]; then
         /bin/rm "$@"
    fi
}

alias srm=saferm
alias rm=srm

Upvotes: 0

Mechanical snail
Mechanical snail

Reputation: 30637

Use the trash command. In Debian/Ubuntu/etc., it can be installed from the package trash-cli. It works on both files and directories (since it's really moving the file, rather than immediately deleting it).

trash implements the freedesktop.org trash specification, compatible with the GNOME and KDE trash.

Files can be undeleted using restore-trash from the same package, or through the usual GUI.

Upvotes: 1

Mr.Ree
Mr.Ree

Reputation: 8418

There's a reason I have [tcsh]:

alias clean  '\rm -i -- "#"* *~'
alias rmo    'rm -- *.o'

They were created the first time I accidentally put a space between the * and the .o. Suffice to say, what happened wasn't what I expected to happen...

But things could have been worse. Back in the early '90s, a friend of mine had a ~/etc directory. He wanted to delete it. Unfortunately he typed rm -rf /etc. Unfortunately, he was logged in as root. He had a bad day!


To be evil: touch -- '-rf *'

To be safe, use '--' and -i. Or get it right once and create an alias!

Upvotes: 0

Scott Alan Miller
Scott Alan Miller

Reputation: 969

If what you want to do is to blow away an entire directory there is always some level of danger associated with that operation. If you really want to be sure that you are doing the right thing you could always do a move operation to some place like /tmp, wait for some amount of time to make sure that everything is okay with the "deletion" in place. Then go into the /tmp directory and ONLY use relative paths for a forced and recursive remove operation. Additional, in the move do a rename to "delete-directoryname" to make it easier not to make a mistake.

For example I want to delete /opt/folder so I do:

mv /opt/folder /tmp/delete-folder

.... wait to be sure everything is okay - maybe a minute, maybe a week ....

cd /tmp

pwd

rm -rf delete-folder/

The most important tip for doing an rm -rf is to always use relative paths. This keeps you from ever having typed a / before having completed your typing.

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506925

I think this is a reasonable way:

find . -maxdepth 1 \! -name . -print0 | xargs -0 rm -rf

and it will also take care of hidden files and directories. The slash isn't required after the dot and this then will also eliminate the possible accident of typing . /.

Now if you are worried what it will delete, just change it into

find . -maxdepth 1 \! -name . -print  | less

And look at the list. Now you can put it into a function:

function enum_files { find . -maxdepth 1 \! -name . "$@"; }

And now your remove is safe:

enum_files | less                     # view the files
enum_files -print0 | xargs -0 rm -rf  # remove the files

If you are not in the habit of having embedded new-lines in filenames, you can omit the -print0 and -0 parameters. But i would use them, just in case :)

Upvotes: 5

Tim
Tim

Reputation: 813

You could drop the `f' switch and it should prompt you for each file to make sure you really want to remove it.

Upvotes: 0

Robert Gamble
Robert Gamble

Reputation: 109022

Here is a safer way: use ls first to list the files that will be affected, then use command-line history or history substitution to change the ls to rm and execute the command again after you are convinced the correct files will be operated on.

Upvotes: 7

Wedge
Wedge

Reputation: 19805

When using rm -rf I almost always use the fully qualified path.

Upvotes: 2

kris
kris

Reputation: 23592

If you want to be really safe, you could create a simple alias or shell script like:

mv $1 ~/.recycle/

This would just move your stuff to a .recycle folder (hello, Windows!).

Then set up a cron job to do rm -rf on stuff in that folder that is older than a week.

Upvotes: 5

Jonathan Leffler
Jonathan Leffler

Reputation: 753675

I use one of:

rm -fr .

cd ..; rm -fr name-of-subdirectory

I'm seldom sufficiently attached to a directory that I want to get rid of the contents but must keep the directory itself.

Upvotes: 3

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340211

The safest way is to sit on your hands before pressing Enter.

That aside, you could create an alias like this one (for Bash)

alias rm="pwd;read;rm"

That will show you your directory, wait for an enter press and then remove what you specified with the proper flags. You can cancel by pressing ^C instead of Enter.

Upvotes: 7

Pyrolistical
Pyrolistical

Reputation: 28062

Go one level up and type in the directory name

rm -rf <dir>/*

Upvotes: 3

Chris Kloberdanz
Chris Kloberdanz

Reputation: 4536

You could always turn on -i which would prompt you on every file, but that would be really time consuming for large directories.

I always do a pwd first.

I'll even go as far as to create an alias so that it forces the prompt for my users. Red Hat does that by default, I think.

Upvotes: 0

Related Questions