使用 find - 删除除任何文件/目录之外的所有文件/目录(在 Linux 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5659516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using find - Deleting all files/directories (in Linux ) except any one
提问by kingsmasher1
If we want to delete all files and directories we use, rm -rf *
.
如果我们想删除我们使用的所有文件和目录,rm -rf *
.
But what if i want all files and directories be deleted at a shot, except one particular file?
但是如果我想一次性删除所有文件和目录,除了一个特定文件怎么办?
Is there any command for that? rm -rf *
gives the ease of deletion at one shot, but deletes even my favourite file/directory.
有什么命令吗?rm -rf *
可以轻松删除一次,但甚至删除我最喜欢的文件/目录。
Thanks in advance
提前致谢
采纳答案by thkala
find
can be a very good friend:
find
可以是很好的朋友:
$ ls
a/ b/ c/
$ find * -maxdepth 0 -name 'b' -prune -o -exec rm -rf '{}' ';'
$ ls
b/
$
Explanation:
解释:
find * -maxdepth 0
: select everything selected by*
without descending into any directories-name 'b' -prune
: do not bother (-prune
) with anything that matches the condition-name 'b'
-o -exec rm -rf '{}' ';'
: callrm -rf
for everything else
find * -maxdepth 0
: 选择所有被选中的东西,*
不降到任何目录-name 'b' -prune
: 不要打扰 (-prune
) 任何符合条件的东西-name 'b'
-o -exec rm -rf '{}' ';'
: 要求rm -rf
其他一切
By the way, another, possibly simpler, way would be to move or rename your favouritedirectory so that it is not in the way:
顺便说一句,另一种可能更简单的方法是移动或重命名您最喜欢的目录,使其不碍事:
$ ls
a/ b/ c/
$ mv b .b
$ ls
a/ c/
$ rm -rf *
$ mv .b b
$ ls
b/
回答by Anji
you need to use regular expression for this. Write a regular expression which selects all other files except the one you need.
您需要为此使用正则表达式。编写一个正则表达式,选择除您需要的文件之外的所有其他文件。
回答by maverik
You can type it right in the command-line or use this keystroke in the script
您可以直接在命令行中键入它或在脚本中使用此按键
files=`ls -l | grep -v "my_favorite_dir"`; for file in $files; do rm -rvf $file; done
P.S. I suggest -i
switch for rm
to prevent delition of important data.
PS 我建议-i
switchrm
以防止删除重要数据。
P.P.S You can write the small script based on this solution and place it to the /usr/bin
(e.g. /usr/bin/rmf
). Now you can use it as and ordinary app:
PPS 您可以根据此解决方案编写小脚本并将其放置到/usr/bin
(例如/usr/bin/rmf
)。现在您可以将其用作普通应用程序:
rmf my_favorite_dir
The script looks like (just a sketch):
脚本看起来像(只是一个草图):
#!/bin/sh
if [[ -z ]]; then
files=`ls -l`
else
files=`ls -l | grep -v `
fi;
for file in $files; do
rm -rvi $file
done;
回答by Roland Illig
I don't know of such a program, but I have wanted it in the past for some times. The basic syntax would be:
我不知道有这样的程序,但我过去想要它有一段时间了。基本语法是:
IFS='
' for f in $(except "*.c" "*.h" -- *); do
printf '%s\n' "$f"
done
The program I have in mind has three modes:
我想到的程序有三种模式:
- exact matching (with the option
-e
) glob
matching (default, like shown in the above example)- regex matching (with the option
-r
)
- 精确匹配(使用选项
-e
) glob
匹配(默认,如上例所示)- 正则表达式匹配(带有选项
-r
)
It takes the patterns to be excluded from the command line, followed by the separator --
, followed by the file names. Alternatively, the file names might be read from stdin
(if the option -s
is given), each on a line.
它需要从命令行中排除的模式,然后是分隔符--
,然后是文件名。或者,可以从stdin
(如果-s
给出选项)读取文件名,每个文件名都在一行上。
Such a program should not be hard to write, in either C or the Shell Command Language. And it makes a good excercise for learning the Unix basics. When you do it as a shell program, you have to watch for filenames containing whitespace and other special characters, of course.
这样的程序应该不难用 C 或 Shell 命令语言编写。它是学习 Unix 基础知识的一个很好的练习。当然,当您作为 shell 程序执行此操作时,您必须注意包含空格和其他特殊字符的文件名。
回答by Noufal Ibrahim
If it's just one file, one simple way is to move that file to /tmp
or something, rm -Rf
the directory and then move it back. You could alias this as a simple command.
如果它只是一个文件,一个简单的方法是将那个文件/tmp
或东西,rm -Rf
目录,然后将其移回。您可以将其别名为一个简单的命令。
The other option is to do a find
and then grep
out what you don't want (using -v
or directly using one of find
s predicates) and then rm
ing the remaining files.
另一种选择是先做 afind
然后grep
去掉你不想要的(使用-v
或直接使用find
s 谓词之一),然后rm
ing 剩余的文件。
For a single file, I'd do the former. For anything more, I'd write something custom similar to what thkala said.
对于单个文件,我会做前者。对于更多内容,我会写一些类似于 thkala 所说的自定义内容。
回答by Inkane
At least in zsh
至少在 zsh 中
rm -rf ^filename
could be an option, if you only want to preserve one single file.
如果您只想保留一个文件,可能是一种选择。
回答by chiborg
In bash you have the !()
glob operator, which inverts the matched pattern. So to delete everything except the file my_file_name.txt
, try this:
在 bash 中,您有!()
glob 运算符,它反转匹配的模式。因此,要删除除 file 之外的所有内容my_file_name.txt
,请尝试以下操作:
shopt -s extglob
rm -f !(my_file_name.txt)
See this article for more details: http://karper.wordpress.com/2010/11/17/deleting-all-files-in-a-directory-with-exceptions/
有关更多详细信息,请参阅本文:http: //karper.wordpress.com/2010/11/17/deleting-all-files-in-a-directory-with-exceptions/
回答by tonybaldwin
I see a lot of longwinded means here, that work, but with a/ b/ c/ d/ e/
我在这里看到很多冗长的意思,那是有效的,但是用 a/ b/ c/ d/ e/
rm -rf *.* !(b*)
this removes everything except directory b/ and its contents (assuming your file is in b/. Then just cd b/ and
这将删除除目录 b/ 及其内容之外的所有内容(假设您的文件在 b/ 中。然后只需 cd b/ 和
rm -rf *.* !(filename)
to remove everything else, but the file (named "filename") that you want to keep.
删除其他所有内容,但要保留的文件(名为“文件名”)除外。
回答by Thyag
Short answer
简答
ls | grep -v "z.txt" | xargs rm
Details:
详情:
The thought process for the above command is :
上述命令的思考过程是:
- List all files (ls)
- Ignore one file named "z.txt" (grep -v "z.txt")
- Delete the listed files other than z.txt (xargs rm)
- 列出所有文件 (ls)
- 忽略名为“z.txt”的文件(grep -v "z.txt")
- 删除除 z.txt (xargs rm) 以外的列出文件
Example
例子
Create 5 files as shown below:
创建5个文件,如下图:
echo "a.txt b.txt c.txt d.txt z.txt" | xargs touch
List all files except z.txt
列出除 z.txt 之外的所有文件
ls|grep -v "z.txt"
a.txt
b.txt
c.txt
d.txt
We can now delete(rm) the listed files by using the xargsutility :
我们现在可以使用xargs实用程序删除(rm)列出的文件:
ls|grep -v "z.txt"|xargs rm
回答by wildplasser
mv subdir/preciousfile ./
rm -rf subdir
mkdir subdir
mv preciousfile subdir/
This looks tedious, but it is rather safe
这看起来很乏味,但它相当安全
- avoids complex logic
- never use
rm -rf *
, its results depend on your current directory (which could be/
;-) - never use a globbing
*
: its expansion is limited by ARGV_MAX. - allows you to check the error after each command, and maybe avoid the disaster caused by the next command.
- avoids nasty problems caused by space or NL in the filenames.
- 避免复杂的逻辑
- 从不使用
rm -rf *
,其结果取决于您当前的目录(可能是/
;-) - 永远不要使用
*
通配符:它的扩展受到 ARGV_MAX 的限制。 - 允许您在每个命令后检查错误,并可能避免下一个命令造成的灾难。
- 避免由文件名中的空格或 NL 引起的令人讨厌的问题。