在* nix中清空目录最安全的方法是什么?

时间:2022-10-22 09:01:59

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?

清空当前目录的内容是否有更安全的方法?

12 个解决方案

#1


7  

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

最安全的方法是在按Enter键之前先坐在你的手上。

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

除此之外,你可以创建一个像这样的别名(对于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.

这将显示您的目录,等待输入按,然后使用正确的标记删除您指定的内容。您可以按^ C而不是Enter来取消。

#2


7  

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.

这是一种更安全的方法:首先使用ls列出将受影响的文件,然后使用命令行历史记录或历史记录替换将ls更改为rm,并在确信将对其进行操作后再次执行命令。

#3


5  

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

如果您想要非常安全,可以创建一个简单的别名或shell脚本,如:

mv $1 ~/.recycle/

mv $ 1~ / .recycle /

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

这只会将你的东西移动到.recycle文件夹(你好,Windows!)。

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

然后设置一个cron作业来对该文件夹中超过一周的东西做rm -rf。

#4


5  

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 :)

如果您不习惯在文件名中嵌入换行符,则可以省略-print0和-0参数。但我会使用它们,以防万一:)

#5


3  

Go one level up and type in the directory name

向上一级并输入目录名称

rm -rf <dir>/*

#6


3  

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.

我很少附加到我想要删除内容的目录,但必须保留目录本身。

#7


2  

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

当使用rm -rf时,我几乎总是使用完全限定的路径。

#8


1  

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命令。在Debian / Ubuntu / etc中,它可以从包trash-cli安装。它适用于文件和目录(因为它实际上是移动文件,而不是立即删除它)。

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

trash实现了freedesktop.org垃圾桶规范,与GNOME和KDE垃圾桶兼容。

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

可以使用同一包中的restore-trash或通过常用GUI取消删除文件。

#9


0  

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

你总是可以打开-i,这会在每个文件上提示你,但这对于大型目录来说真的很耗时。

I always do a pwd first.

我总是先做一个pwd。

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.

我甚至会创建一个别名,以便强制提示我的用户。我认为红帽默认是这样做的。

#10


0  

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

您可以删除`f'开关,它应该提示您输入每个文件,以确保您确实要删除它。

#11


0  

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.

如果您想要做的就是吹走整个目录,那么该操作总会存在一定程度的危险。如果你真的想确定你做的是正确的事情,你可以随时对某个地方进行移动操作,比如/ tmp,等待一段时间,确保一切正常,“删除”到位。然后进入/ tmp目录并仅使用相对路径进行强制和递归删除操作。另外,在移动中重命名为“delete-directoryname”以使其更容易出错。

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

例如,我想删除/ opt /文件夹,所以我这样做:

mv /opt/folder /tmp/delete-folder

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/

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.

执行rm -rf最重要的提示是始终使用相对路径。这使您无需在完成输入之前键入/。

#12


0  

There's a reason I have [tcsh]:

我有[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...

它是我第一次意外地在*和.o之间放置一个空格而创建的。我只想说,发生的事情不是我预期会发生的......

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!

但事情本来可能更糟。早在90年代初,我的一个朋友就有了一个〜/ etc目录。他想删除它。不幸的是他键入了rm -rf / etc.不幸的是,他以root身份登录。他度过了糟糕的一天!


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

要变得邪恶:触摸 - '-rf *'

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

为安全起见,请使用' - '和-i。或者一次正确创建一个别名!

#1


7  

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

最安全的方法是在按Enter键之前先坐在你的手上。

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

除此之外,你可以创建一个像这样的别名(对于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.

这将显示您的目录,等待输入按,然后使用正确的标记删除您指定的内容。您可以按^ C而不是Enter来取消。

#2


7  

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.

这是一种更安全的方法:首先使用ls列出将受影响的文件,然后使用命令行历史记录或历史记录替换将ls更改为rm,并在确信将对其进行操作后再次执行命令。

#3


5  

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

如果您想要非常安全,可以创建一个简单的别名或shell脚本,如:

mv $1 ~/.recycle/

mv $ 1~ / .recycle /

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

这只会将你的东西移动到.recycle文件夹(你好,Windows!)。

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

然后设置一个cron作业来对该文件夹中超过一周的东西做rm -rf。

#4


5  

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 :)

如果您不习惯在文件名中嵌入换行符,则可以省略-print0和-0参数。但我会使用它们,以防万一:)

#5


3  

Go one level up and type in the directory name

向上一级并输入目录名称

rm -rf <dir>/*

#6


3  

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.

我很少附加到我想要删除内容的目录,但必须保留目录本身。

#7


2  

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

当使用rm -rf时,我几乎总是使用完全限定的路径。

#8


1  

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命令。在Debian / Ubuntu / etc中,它可以从包trash-cli安装。它适用于文件和目录(因为它实际上是移动文件,而不是立即删除它)。

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

trash实现了freedesktop.org垃圾桶规范,与GNOME和KDE垃圾桶兼容。

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

可以使用同一包中的restore-trash或通过常用GUI取消删除文件。

#9


0  

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

你总是可以打开-i,这会在每个文件上提示你,但这对于大型目录来说真的很耗时。

I always do a pwd first.

我总是先做一个pwd。

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.

我甚至会创建一个别名,以便强制提示我的用户。我认为红帽默认是这样做的。

#10


0  

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

您可以删除`f'开关,它应该提示您输入每个文件,以确保您确实要删除它。

#11


0  

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.

如果您想要做的就是吹走整个目录,那么该操作总会存在一定程度的危险。如果你真的想确定你做的是正确的事情,你可以随时对某个地方进行移动操作,比如/ tmp,等待一段时间,确保一切正常,“删除”到位。然后进入/ tmp目录并仅使用相对路径进行强制和递归删除操作。另外,在移动中重命名为“delete-directoryname”以使其更容易出错。

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

例如,我想删除/ opt /文件夹,所以我这样做:

mv /opt/folder /tmp/delete-folder

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/

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.

执行rm -rf最重要的提示是始终使用相对路径。这使您无需在完成输入之前键入/。

#12


0  

There's a reason I have [tcsh]:

我有[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...

它是我第一次意外地在*和.o之间放置一个空格而创建的。我只想说,发生的事情不是我预期会发生的......

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!

但事情本来可能更糟。早在90年代初,我的一个朋友就有了一个〜/ etc目录。他想删除它。不幸的是他键入了rm -rf / etc.不幸的是,他以root身份登录。他度过了糟糕的一天!


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

要变得邪恶:触摸 - '-rf *'

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

为安全起见,请使用' - '和-i。或者一次正确创建一个别名!