如何使用Find-command删除列出的文件以外的所有内容?

时间:2021-06-29 19:30:04

I have a list of hidden files in a file "list_files" that should not be removed in the current directory. How can remove everything except them with a Find-command? I tried, but it clearly does not work:

我有一个文件“list_files”中的隐藏文件列表,不应该在当前目录中删除。如何使用Find-command删除除它们之外的所有内容?我试过了,但它显然不起作用:

find . -iname ".*" \! -iname 'list_files'

3 个解决方案

#1


Removing files should always be done safely.

应始终安全地删除文件。

I presume you have a directory tree with hidden files and a sub-set list of these hidden files which you want to retain. You want to delete all other hidden files.

我假设您有一个包含隐藏文件的目录树以及您要保留的这些隐藏文件的子集列表。您想要删除所有其他隐藏文件。

Lets start with the list of hidden files.

让我们从隐藏文件列表开始。

find `pwd` -iname ".*" -type f > all-hidden-files.txt

Now, assume that you have some filter which will reduce the list to all files that you want to retain (creating your list_files). Here SomeFilter could be you manually editing the files list to retain the ones you do not want to delete.

现在,假设您有一些过滤器,它会将列表缩减为您要保留的所有文件(创建list_files)。这里SomeFilter可能是您手动编辑文件列表以保留您不想删除的文件列表。

SomeFilter all-hidden-files.txt > list_files

The next command will identify lines in all-hidden-files.txt that are missing from the list_files, which gives you files that can be removed.

下一个命令将识别list_files中缺少的all-hidden-files.txt中的行,这些行为您提供了可以删除的文件。

comm -3 all-hidden-files.txt list_files > removable-files.txt

Edit: Just realized that the input files to comm should be sorted. So use this as,

编辑:刚刚意识到应该对comm的输入文件进行排序。所以用这个,

comm -3 <(sort all-hidden-files.txt | uniq) <(sort list_files | uniq) \
    > removable-files.txt

You can confirm this works well for you and then delete the list of files generated with something like,

您可以确认这适用于您,然后删除生成的文件列表,如,

for i in $(<removable-files.txt); do rm $i; done;

#2


You can do this by invoking exec with a bash script so something like this :-

您可以通过使用bash脚本调用exec来执行此操作,如下所示: -

find . -iname ".*" -exec bash -c "fgrep {} /tmp/list_files >/dev/null || rm -i {}" \;

Be very careful how your construct your list of files. The list of files to exclude must be identical to the output produced by find or you will delete all files matching your pattern.

如何构建文件列表时要非常小心。要排除的文件列表必须与find生成的输出相同,否则您将删除与您的模式匹配的所有文件。

I have put the interactive option on to rm and you may wish to use this for testing. If you wish to remove directories with this technique then you will need to modify the rm options.

我已将交互选项放到rm上,您可能希望将其用于测试。如果您希望使用此技术删除目录,则需要修改rm选项。

You may wish to construct your list of files using find from the same folder you will use to run the find to ensure the exclusions will be honoured although an absolute rather than a relative path would be a better i.e. safer option, so your find would become

您可能希望使用用于运行查找的相同文件夹中的find来构建文件列表,以确保排除将被尊重,尽管绝对而非相对路径将是更好,即更安全的选项,因此您的查找将变为

find /some/folder/name -name "some pattern" -exec ....

#3


create a temporary directory in your source directory, move everything to the temp directory, move the files you want to save back up to the original place, and then recursively remove the temp directory. Since the moves are all on one filesystem, it should be almost instantaneous with any decent filesystem, and this is pretty safe.

在源目录中创建一个临时目录,将所有内容移动到临时目录,将要保存的文件移回原始位置,然后递归删除临时目录。由于移动都在一个文件系统上,因此对于任何体面的文件系统来说它几乎都是即时的,这是非常安全的。

#1


Removing files should always be done safely.

应始终安全地删除文件。

I presume you have a directory tree with hidden files and a sub-set list of these hidden files which you want to retain. You want to delete all other hidden files.

我假设您有一个包含隐藏文件的目录树以及您要保留的这些隐藏文件的子集列表。您想要删除所有其他隐藏文件。

Lets start with the list of hidden files.

让我们从隐藏文件列表开始。

find `pwd` -iname ".*" -type f > all-hidden-files.txt

Now, assume that you have some filter which will reduce the list to all files that you want to retain (creating your list_files). Here SomeFilter could be you manually editing the files list to retain the ones you do not want to delete.

现在,假设您有一些过滤器,它会将列表缩减为您要保留的所有文件(创建list_files)。这里SomeFilter可能是您手动编辑文件列表以保留您不想删除的文件列表。

SomeFilter all-hidden-files.txt > list_files

The next command will identify lines in all-hidden-files.txt that are missing from the list_files, which gives you files that can be removed.

下一个命令将识别list_files中缺少的all-hidden-files.txt中的行,这些行为您提供了可以删除的文件。

comm -3 all-hidden-files.txt list_files > removable-files.txt

Edit: Just realized that the input files to comm should be sorted. So use this as,

编辑:刚刚意识到应该对comm的输入文件进行排序。所以用这个,

comm -3 <(sort all-hidden-files.txt | uniq) <(sort list_files | uniq) \
    > removable-files.txt

You can confirm this works well for you and then delete the list of files generated with something like,

您可以确认这适用于您,然后删除生成的文件列表,如,

for i in $(<removable-files.txt); do rm $i; done;

#2


You can do this by invoking exec with a bash script so something like this :-

您可以通过使用bash脚本调用exec来执行此操作,如下所示: -

find . -iname ".*" -exec bash -c "fgrep {} /tmp/list_files >/dev/null || rm -i {}" \;

Be very careful how your construct your list of files. The list of files to exclude must be identical to the output produced by find or you will delete all files matching your pattern.

如何构建文件列表时要非常小心。要排除的文件列表必须与find生成的输出相同,否则您将删除与您的模式匹配的所有文件。

I have put the interactive option on to rm and you may wish to use this for testing. If you wish to remove directories with this technique then you will need to modify the rm options.

我已将交互选项放到rm上,您可能希望将其用于测试。如果您希望使用此技术删除目录,则需要修改rm选项。

You may wish to construct your list of files using find from the same folder you will use to run the find to ensure the exclusions will be honoured although an absolute rather than a relative path would be a better i.e. safer option, so your find would become

您可能希望使用用于运行查找的相同文件夹中的find来构建文件列表,以确保排除将被尊重,尽管绝对而非相对路径将是更好,即更安全的选项,因此您的查找将变为

find /some/folder/name -name "some pattern" -exec ....

#3


create a temporary directory in your source directory, move everything to the temp directory, move the files you want to save back up to the original place, and then recursively remove the temp directory. Since the moves are all on one filesystem, it should be almost instantaneous with any decent filesystem, and this is pretty safe.

在源目录中创建一个临时目录,将所有内容移动到临时目录,将要保存的文件移回原始位置,然后递归删除临时目录。由于移动都在一个文件系统上,因此对于任何体面的文件系统来说它几乎都是即时的,这是非常安全的。