移动除一个文件之外的所有文件
如何移动除一个文件之外的所有文件? 我正在寻找类似的东西:
'mv ~/Linux/Old/!Tux.png ~/Linux/New/'
我将旧的东西移动到新的东西文件夹,除了 Tux.png
。 !-符号代表否定。 有一些工具可以完成这项工作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果您使用 bash 并设置了 extglob shell 选项(通常是这种情况):
If you use bash and have the
extglob
shell option set (which is usually the case):将以下内容放入您的 .bashrc
它扩展了正则表达式。
移动除一个文件之外的所有文件。
然后,您可以通过与其他命令相关的异常来
请注意,在复制目录时,不能在名称中使用前向闪存,如线程 为什么 extglob 除了中断例外条件?:
所以
Backups.backupdb/
在否定之前在这里是错误的,我不会在移动目录时使用它,因为错误使用然后与其他命令和可能的其他异常一起使用的风险。Put the following to your .bashrc
It extends regexes.
You can then move all files except one by
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?:
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.我会选择传统的 find & 方法。 xargs方式:
-maxdepth 1
使其不递归搜索。 如果你只关心文件,你可以说-type f
。-mindepth 1
使其不将~/Linux/Old
路径本身包含到结果中。 适用于任何文件名,包括包含嵌入换行符的文件名。一条评论指出 mv
-t
选项可能是 GNU 扩展。 对于没有它的系统I would go with the traditional find & xargs way:
-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.One comment notes that the mv
-t
option is a probably GNU extension. For systems that don't have it一种快速方法是修改 tux 文件名,以便您的移动命令不匹配。
例如:
A quick way would be to modify the tux filename so that your move command will not match.
For example:
我认为最简单的方法是使用反引号
编辑:
将反斜杠与 ls 一起使用,以防止将其与别名一起使用,即大多数 ls 别名为 ls --color。
谢谢@阿诺德·罗阿
I think the easiest way to do is with backticks
Edit:
Use backslash with ls instead to prevent using it with alias, i.e. mostly ls is aliased as ls --color.
Thanks @Arnold Roa
对于 bash,某个答案是正确的。 这是 zsh (我选择的 shell)语法:
需要
EXTENDED_GLOB
shell 选项被设置。For bash, sth answer is correct. Here is the zsh (my shell of choice) syntax:
Requires
EXTENDED_GLOB
shell option to be set.我发现这对于排除某些文件或目录的简单移动来说更安全、更容易依赖。
I find this to be a bit safer and easier to rely on for simple moves that exclude certain files or directories.
这可能更简单、更容易记住,而且对我有用。
mv $(ls ~/folder | grep -v ~/folder/exclude.png) ~/destination
This could be simpler and easy to remember and it works for me.
mv $(ls ~/folder | grep -v ~/folder/exclude.png) ~/destination
以下方法不是 100% 有保证的方法,根本不应尝试编写脚本。 但有时它对于快速交互式 shell 使用来说已经足够了。 像文件文件 glob 这样的文件
(将匹配名称以 a、b 或 c 开头的所有文件)可以通过先插入“^”字符来否定,即
我有时使用它来不匹配“lost+found”目录,例如例如:
当然,如果还有其他以 l 开头的文件,我必须稍后处理这些文件。
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
(which will match all files with names starting with a, b or c) can be negated by inserting a "^" character first, i.e.
I sometimes use this for not matching the "lost+found" directory, like for instance:
Of course if there are other files starting with l I have to process those afterwards.
怎么样:
不过你必须在文件夹中。
How about:
You have to be in the folder though.
这可以在没有
grep
的情况下完成,如下所示:注意:
-I
是大写 i 并使ls
命令忽略 Tux.png 文件,随后列出。然后,
ls
的输出通过xargs
通过管道传输到mv
,这允许使用 ls 的输出作为 mv 的源参数。ls -Q
只是引用 ls 列出的文件名。This can bei done without
grep
like this:Note:
-I
is a captial i and makes thels
command ignore the Tux.png file, which is listed afterwards.The output of
ls
is then piped intomv
viaxargs
, which allows to use the output of ls as source argument for mv.ls -Q
just quotes the filenames listed by ls.find 命令列出所有常规文件,fgrep 命令过滤掉任何 Tux.png。 反引号告诉 mv 移动结果文件列表。
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.
将所有文件(不包括文件除外)移动到 except_file
find -max深度 1 -min深度 1 -not -name except_file -print0 |xargs -0 mv -t ./ except_file
例如(缓存是当前的,除了文件)
find -max深度 1 -min深度 1 -not -name 缓存 -print0 |xargs -0 mv -t ./cache
move all files(not include except file) to 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