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

发布于 2024-07-16 01:42:16 字数 184 浏览 9 评论 0原文

如何移动除一个文件之外的所有文件? 我正在寻找类似的东西:

'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

影子是时光的心 2024-07-23 01:42:16

如果您使用 bash 并设置了 extglob shell 选项(通常是这种情况):

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

If you use bash and have the extglob shell option set (which is usually the case):

mv ~/Linux/Old/!(Tux.png) ~/Linux/New/
惜醉颜 2024-07-23 01:42:16

将以下内容放入您的 .bashrc

shopt -s extglob

它扩展了正则表达式。
移动除一个文件之外的所有文件。

mv !(fileOne) ~/path/newFolder

然后,您可以通过与其他命令相关的异常来

请注意,在复制目录时,不能在名称中使用前向闪存,如线程 为什么 extglob 除了中断例外条件?

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

所以 Backups.backupdb/ 在否定之前在这里是错误的,我不会在移动目录时使用它,因为错误使用然后与其他命令和可能的其他异常一起使用的风险。

Put the following to 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?:

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.

浅沫记忆 2024-07-23 01:42:16

我会选择传统的 find & 方法。 xargs方式:

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

-maxdepth 1使其不递归搜索。 如果你只关心文件,你可以说-type f-mindepth 1 使其不将 ~/Linux/Old 路径本身包含到结果中。 适用于任何文件名,包括包含嵌入换行符的文件名。

一条评论指出 mv -t 选项可能是 GNU 扩展。 对于没有它的系统

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

I would go with the traditional find & xargs way:

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.

One comment notes that the mv -t option is a probably GNU extension. For systems that don't have it

find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png \
    -exec mv '{}' ~/Linux/New \;
第七度阳光i 2024-07-23 01:42:16

一种快速方法是修改 tux 文件名,以便您的移动命令不匹配。

例如:

mv Tux.png .Tux.png

mv * ~/somefolder

mv .Tux.png Tux.png

A quick way would be to modify the tux filename so that your move command will not match.

For example:

mv Tux.png .Tux.png

mv * ~/somefolder

mv .Tux.png Tux.png
桃气十足 2024-07-23 01:42:16

我认为最简单的方法是使用反引号

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

编辑:

将反斜杠与 ls 一起使用,以防止将其与别名一起使用,即大多数 ls 别名为 ls --color。

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

谢谢@阿诺德·罗阿

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, i.e. mostly ls is aliased as ls --color.

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

Thanks @Arnold Roa

往事风中埋 2024-07-23 01:42:16

对于 bash,某个答案是正确的。 这是 zsh (我选择的 shell)语法:

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

需要 EXTENDED_GLOB shell 选项被设置。

For bash, sth answer is correct. Here is the zsh (my shell of choice) syntax:

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

Requires EXTENDED_GLOB shell option to be set.

电影里的梦 2024-07-23 01:42:16

我发现这对于排除某些文件或目录的简单移动来说更安全、更容易依赖。

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

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
吻安 2024-07-23 01:42:16

这可能更简单、更容易记住,而且对我有用。

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

以歌曲疗慰 2024-07-23 01:42:16

以下方法不是 100% 有保证的方法,根本不应尝试编写脚本。 但有时它对于快速交互式 shell 使用来说已经足够了。 像文件文件 glob 这样的文件

[abc]*

(将匹配名称以 a、b 或 c 开头的所有文件)可以通过先插入“^”字符来否定,即

[^abc]*

我有时使用它来不匹配“lost+found”目录,例如例如:

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

当然,如果还有其他以 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

[abc]*

(which will match all files with names starting with a, b or c) can be negated by inserting a "^" character first, i.e.

[^abc]*

I sometimes use this for not matching the "lost+found" directory, like for instance:

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

Of course if there are other files starting with l I have to process those afterwards.

若水微香 2024-07-23 01:42:16

怎么样:

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

不过你必须在文件夹中。

How about:

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

You have to be in the folder though.

究竟谁懂我的在乎 2024-07-23 01:42:16

这可以在没有 grep 的情况下完成,如下所示:

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

注意:-I 是大写 i 并使 ls 命令忽略 Tux.png 文件,随后列出。

然后,ls 的输出通过 xargs 通过管道传输到 mv,这允许使用 ls 的输出作为 mv 的源参数。

ls -Q 只是引用 ls 列出的文件名。

This can bei done without grep like this:

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

Note: -I is a captial i and makes the ls command ignore the Tux.png file, which is listed afterwards.

The output of ls is then piped into mv via xargs, which allows to use the output of ls as source argument for mv.

ls -Q just quotes the filenames listed by ls.

拔了角的鹿 2024-07-23 01:42:16
mv `find Linux/Old '!' -type d | fgrep -v Tux.png` Linux/New

find 命令列出所有常规文件,fgrep 命令过滤掉任何 Tux.png。 反引号告诉 mv 移动结果文件列表。

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.

旧人哭 2024-07-23 01:42:16
ls ~/Linux/Old/ | grep -v Tux.png | xargs -i {} mv ~/Linux/New/'
ls ~/Linux/Old/ | grep -v Tux.png | xargs -i {} mv ~/Linux/New/'
战皆罪 2024-07-23 01:42:16

将所有文件(不包括文件除外)移动到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文