bash 从目录中删除除某些文件之外的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4325216/
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
Remove all files except some from a directory
提问by masjoko
When using sudo rm -r
, how can I delete all files, with the exception of the following:
使用时sudo rm -r
,如何删除所有文件,但以下文件除外:
textfile.txt
backup.tar.gz
script.php
database.sql
info.txt
回答by awi
find [path] -type f -not -name 'textfile.txt' -not -name 'backup.tar.gz' -delete
If you don't specify -type f
find will also list directories, which you may not want.
如果您不指定-type f
find 还将列出您可能不想要的目录。
Or a more general solution using the very useful combination find | xargs
:
或者使用非常有用的组合的更通用的解决方案find | xargs
:
find [path] -type f -not -name 'EXPR' -print0 | xargs -0 rm --
for example, delete all non txt-files in the current directory:
例如,删除当前目录中的所有非 txt 文件:
find . -type f -not -name '*txt' -print0 | xargs -0 rm --
The print0
and -0
combination is needed if there are spaces in any of the filenames that should be deleted.
该print0
和-0
是否有任何应该被删除的文件名的空间需要组合。
回答by pl1nk
rm !(textfile.txt|backup.tar.gz|script.php|database.sql|info.txt)
The extglob (Extended Pattern Matching) needs to be enabled in BASH (if it's not enabled):
需要在 BASH 中启用 extglob(扩展模式匹配)(如果未启用):
shopt -s extglob
回答by darioo
find . | grep -v "excluded files criteria" | xargs rm
find . | grep -v "excluded files criteria" | xargs rm
This will list all files in current directory, then list all those that don't match your criteria (beware of it matching directory names) and then remove them.
这将列出当前目录中的所有文件,然后列出所有与您的条件不匹配的文件(注意它匹配目录名称),然后将其删除。
Update: based on your edit, if you really want to delete everything from current directory except files you listed, this can be used:
更新:根据您的编辑,如果您真的想删除当前目录中除列出的文件之外的所有内容,可以使用:
mkdir /tmp_backup && mv textfile.txt backup.tar.gz script.php database.sql info.txt /tmp_backup/ && rm -r && mv /tmp_backup/* . && rmdir /tmp_backup
It will create a backup directory /tmp_backup
(you've got root privileges, right?), move files you listed to that directory, delete recursively everything in current directory (you know that you're in the right directory, do you?), move back to current directory everything from /tmp_backup
and finally, delete /tmp_backup
.
它将创建一个备份目录/tmp_backup
(您有 root 权限,对吗?),将您列出的文件移动到该目录,递归删除当前目录中的所有内容(您知道您在正确的目录中,是吗?),移动回到当前目录中的所有内容/tmp_backup
,最后,删除/tmp_backup
.
I choose the backup directory to be in root, because if you're trying to delete everything recursively from root, your system will have big problems.
我选择备份目录在 root 中,因为如果您尝试从 root 递归删除所有内容,您的系统将出现大问题。
Surely there are more elegant ways to do this, but this one is pretty straightforward.
当然有更优雅的方法可以做到这一点,但这个方法非常简单。
回答by Paused until further notice.
Assuming that files with those names exist in multiple places in the directory tree and you want to preserve all of them:
假设具有这些名称的文件存在于目录树中的多个位置,并且您希望保留所有这些文件:
find . -type f ! -regex ".*/\(textfile.txt\|backup.tar.gz\|script.php\|database.sql\|info.txt\)" -delete
回答by theharshest
You can use GLOBIGNORE environment variable in Bash.
您可以在 Bash 中使用 GLOBIGNORE 环境变量。
Suppose you want to delete all files except php and sql, then you can do the following -
假设你要删除除php和sql之外的所有文件,那么你可以做以下操作——
export GLOBIGNORE=*.php:*.sql
rm *
export GLOBIGNORE=
Setting GLOBIGNORE like this ignores php and sql from wildcards used like "ls *" or "rm *". So, using "rm *" after setting the variable will delete only txt and tar.gz file.
像这样设置 GLOBIGNORE 会忽略来自“ls *”或“rm *”等通配符的 php 和 sql。因此,在设置变量后使用“rm *”将仅删除 txt 和 tar.gz 文件。
回答by Nick Tsai
I prefer to use sub query list:
我更喜欢使用子查询列表:
rm -r `ls | grep -v "textfile.txt\|backup.tar.gz\|script.php\|database.sql\|info.txt"`
-v, --invert-match select non-matching lines
\|
Separator
-v, --invert-match 选择不匹配的行
\|
分隔器
回答by gniourf_gniourf
Since nobody mentioned it:
由于没有人提到它:
- copy the files you don't want to delete in a safe place
- delete all the files
- move the copied files back in place
- 将您不想删除的文件复制到安全的地方
- 删除所有文件
- 将复制的文件移回原位
回答by mishunika
You can write a for loop for this... %)
您可以为此编写一个 for 循环... %)
for x in *
do
if [ "$x" != "exclude_criteria" ]
then
rm -f $x;
fi
done;
回答by lhuber
A little late for the OP, but hopefully useful for anyone who gets here much later by google...
OP 有点晚了,但希望对那些后来通过谷歌到达这里的人有用......
I found the answer by @awi and comment on -delete by @Jamie Bullock really useful. A simple utility so you can do this in different directories ignoring different file names/types each time with minimal typing:
我发现@awi 的答案和@Jamie Bullock 对 -delete 的评论非常有用。一个简单的实用程序,因此您可以在不同的目录中执行此操作,每次都以最少的输入忽略不同的文件名/类型:
rm_except (or whatever you want to name it)
rm_except (或任何你想命名的)
#!/bin/bash
ignore=""
for fignore in "$@"; do
ignore=${ignore}"-not -name ${fignore} "
done
find . -type f $ignore -delete
e.g. to delete everything except for text files and foo.bar:
例如,删除除文本文件和 foo.bar 之外的所有内容:
rm_except *.txt foo.bar
Similar to @mishunika, but without the if clause.
类似于@mishunika,但没有 if 子句。