Linux 移动除一个之外的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/670460/
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
Move all files except one
提问by Léo Léopold Hertz ??
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
. !-sign represents a negation. Is there some tool for the job?
我将旧东西移到新东西文件夹的地方,除了Tux.png
. !-sign 表示否定。有什么工具可以完成这项工作吗?
采纳答案by Léo Léopold Hertz ??
Put the following to your .bashrc
将以下内容放入您的 .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 中所注意到的,除了中断除外条件?:
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/
在否定之前这里是错误的,我不会在移动目录中使用它,因为有错误地将 globs 与其他命令和可能的其他异常一起使用的风险。
回答by David Z
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 语法可以说“除了......之外的一切”。
回答by John T
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
回答by sth
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/
回答by David Norman
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. The backticks tell mv to move the resulting file list.
find 命令列出所有常规文件,fgrep 命令过滤掉任何 Tux.png。反引号告诉 mv 移动生成的文件列表。
回答by Juliano
For bash, sth answeris correct. Here is the zsh(my shell of choice) syntax:
对于 bash,答案是正确的。这是zsh(我选择的 shell)语法:
mv ~/Linux/Old/^Tux.png ~/Linux/New/
Requires EXTENDED_GLOB
shell option to be set.
需要EXTENDED_GLOB
设置 shell 选项。
回答by Johannes Schaub - litb
I would go with the traditional find & xargs way:
我会采用传统的 find & xargs 方式:
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. If you only care about files, you can say -type f
. -mindepth 1
makes it not include the ~/Linux/Old
path itself into the result. Works with any filenames, including with those that contain embedded newlines.
-maxdepth 1
使其不递归搜索。如果你只关心文件,你可以说-type f
. -mindepth 1
使其不将~/Linux/Old
路径本身包含在结果中。适用于任何文件名,包括包含嵌入换行符的文件名。
One comment notes that the mv -t
option is a probably GNU extension. For systems that don't have it
一个评论指出 mv-t
选项可能是一个 GNU 扩展。对于没有它的系统
find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png \
-exec mv '{}' ~/Linux/New \;
回答by hlovdal
The following is not a 100% guaranteed method, and should not at all be attempted for scripting. But some times it is good enough for quick interactive shell usage. A file file glob like
以下不是 100% 保证的方法,根本不应该尝试编写脚本。但有时它对于快速交互式 shell 使用来说已经足够了。一个文件文件 glob 像
[abc]*
(which will match all files with names starting with a, b or c) can be negated by inserting a "^" character first, i.e.
(它将匹配名称以 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 开头的文件,我必须在之后处理这些文件。
回答by alex.pilon
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
回答by aitor
ls ~/Linux/Old/ | grep -v Tux.png | xargs -i {} mv ~/Linux/New/'