简体   繁体   English

移动除一个以外的所有文件

[英]Move all files except one

How can I move all files except one?如何移动除一个以外的所有文件? I am looking for something like:我正在寻找类似的东西:

'mv ~/Linux/Old/!Tux.png ~/Linux/New/'

where I move old stuff to new stuff -folder except Tux.png .我将旧的东西移动到新的东西文件夹除了Tux.png .-sign represents a negation? .-符号表示否定? Is there some tool for the job?有什么工具可以完成这项工作吗?

If you use bash and have the extglob shell option set (which is usually the case):如果您使用 bash 并设置了extglob shell 选项(通常是这种情况):

mv ~/Linux/Old/!(Tux.png) ~/Linux/New/

Put the following to your.bashrc将以下内容放入 your.bashrc

shopt -s extglob

It extends regexes.它扩展了正则表达式。 You can then move all files except one by然后,您可以移动除一个以外的所有文件

mv !(fileOne) ~/path/newFolder

Exceptions in relation to other commands与其他命令相关的异常

Note that, in copying directories, the forward-flash cannot be used in the name as noticed in the thread Why extglob except breaking except condition?请注意,在复制目录时,不能在线程中注意到的名称中使用正向闪存Why extglob except break except condition? :

cp -r !(Backups.backupdb) /home/masi/Documents/

so Backups.backupdb/ is wrong here before the negation and I would not use it neither in moving directories because of the risk of using wrongly then globs with other commands and possible other exceptions.所以Backups.backupdb/在否定之前在这里是错误的,我不会在移动目录中使用它,因为使用错误的风险然后 glob 与其他命令和可能的其他异常。

I would go with the traditional find & xargs way:我会用传统的 find & xargs 方式 go :

find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png -print0 | 
    xargs -0 mv -t ~/Linux/New

-maxdepth 1 makes it not search recursively. -maxdepth 1使其不递归搜索。 If you only care about files, you can say -type f .如果你只关心文件,你可以说-type f -mindepth 1 makes it not include the ~/Linux/Old path itself into the result. -mindepth 1使其不包含~/Linux/Old路径本身到结果中。 Works with any filenames, including with those that contain embedded newlines.适用于任何文件名,包括那些包含嵌入换行符的文件名。

One comment notes that the mv -t option is a probably GNU extension.一条评论指出 mv -t选项可能是 GNU 扩展。 For systems that don't have it对于没有它的系统

find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png \
    -exec mv '{}' ~/Linux/New \;

A quick way would be to modify the tux filename so that your move command will not match.一种快速的方法是修改 tux 文件名,以便您的移动命令不匹配。

For example:例如:

mv Tux.png .Tux.png

mv * ~/somefolder

mv .Tux.png Tux.png

I think the easiest way to do is with backticks我认为最简单的方法是使用反引号

mv `ls -1 ~/Linux/Old/ | grep -v Tux.png` ~/Linux/New/

Edit:编辑:

Use backslash with ls instead to prevent using it with alias, ie mostly ls is aliased as ls --color.将反斜杠与 ls 一起使用以防止将其与别名一起使用,即大多数情况下 ls 被别名为 ls --color。

mv `\ls -1 ~/Linux/Old/ | grep -v Tux.png` ~/Linux/New/

Thanks @Arnold Roa谢谢@Arnold Roa

For bash, sth answer is correct.对于bash, ……答案是正确的。 Here is the zsh (my shell of choice) syntax:这是zsh (我选择的 shell)语法:

mv ~/Linux/Old/^Tux.png ~/Linux/New/

Requires EXTENDED_GLOB shell option to be set.需要设置EXTENDED_GLOB shell 选项。

I find this to be a bit safer and easier to rely on for simple moves that exclude certain files or directories.对于排除某些文件或目录的简单移动,我发现这更安全、更容易依赖。

ls -1 | grep -v ^$EXCLUDE | xargs -I{} mv {} $TARGET

The following is not a 100% guaranteed method, and should not at all be attempted for scripting.以下不是 100% 保证的方法,根本不应该尝试编写脚本。 But some times it is good enough for quick interactive shell usage.但有时它足以用于快速交互式 shell 使用。 A file file glob like一个文件文件 glob 像

[abc]*

(which will match all files with names starting with a, b or c) can be negated by inserting a "^" character first, ie (将匹配名称以 a、b 或 c 开头的所有文件)可以通过先插入“^”字符来否定,即

[^abc]*

I sometimes use this for not matching the "lost+found" directory, like for instance:我有时会使用它来匹配“lost+found”目录,例如:

mv /mnt/usbdisk/[^l]* /home/user/stuff/.

Of course if there are other files starting with l I have to process those afterwards.当然,如果还有其他以 l 开头的文件,我必须在之后处理这些文件。

How about:怎么样:

mv $(echo * | sed s:Tux.png::g) ~/Linux/New/

You have to be in the folder though.不过,您必须在文件夹中。

This could be simpler and easy to remember and it works for me.这可能更简单,更容易记住,它对我有用。

mv $(ls ~/folder | grep -v ~/folder/exclude.png) ~/destination

mv `find Linux/Old '!' -type d | fgrep -v Tux.png` Linux/New

The find command lists all regular files and the fgrep command filters out any Tux.png. find 命令列出所有常规文件,fgrep 命令过滤掉所有 Tux.png。 The backticks tell mv to move the resulting file list.反引号告诉 mv 移动生成的文件列表。

ls ~/Linux/Old/ | grep -v Tux.png | xargs -i {} mv ~/Linux/New/'

One can skip grep like this:可以像这样跳过 grep

ls ~/Linux/Old/ -QI Tux.png | xargs -I{} mv ~/Linux/Old/{} ~/Linux/New/

How about you move everything in the folder and then just move Tux.png back?你如何移动文件夹中的所有内容,然后将 Tux.png 移回?

I can't think of any shell syntax to say "everything except..." offhand.我想不出任何 shell 语法可以随便说“除...之外的所有内容”。

move all files(not include except file) to except_file将所有文件(不包括文件除外)移动到 except_file
find -maxdepth 1 -mindepth 1 -not -name except_file -print0 |xargs -0 mv -t./except_file
for example(cache is current except file)例如(缓存是当前的,除了文件)
find -maxdepth 1 -mindepth 1 -not -name cache -print0 |xargs -0 mv -t./cache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM