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

I am afraid that one day, I am going to put a space or skip something in the command that I am currently using:

rm -rf ./*

Is there a safer way to empty the current contents of a directory?

+4
source share
12 answers

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

Aside, you could create an alias like this (for Bash)

alias rm = "pwd; read; rm"

This will show you your directory, wait for the input to click, and then delete what you specified with the appropriate flags. You can cancel by pressing ^ C instead of Enter.

+7
source

: ls, , , , ls rm , , .

+7

, script, :

mv $1 ~/.recycle/

.recycle(, Windows!).

cron, rm -rf , .

+5

, :

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

. , . /.

, , ,

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

. :

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

:

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

, -print0 -0. , :)

+5

rm -rf <dir>/*
+3

:

rm -fr .

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

, , .

+3

rm -rf .

+2

trash. Debian/Ubuntu/etc. trash-cli. , ( , ).

trash freedesktop.org, GNOME KDE.

restore-trash .

+1

-i, , .

pwd.

, . Red Hat , .

0

`f ', , .

0

, , . , , - , /tmp, , , "" . /tmp . , "delete-directoryname", .

, /opt/folder, :

mv/opt/folder/tmp/delete-folder

.... , , - , , , ....

cd/tmp

PWD

rm -rf delete-folder/

rm -rf - . a/ , .

0

, [tcsh]:

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

, * .o. , , ...

But it could have been worse. Back in the early 90s, my friend had a ~ / etc directory . He wanted to remove it. Unfortunately, he got the rm -rf / etc . Unfortunately, it was registered as root. He had a bad day!


To be evil: Touch - '-rf *'

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

0
source

All Articles