Bash 移动 * 到子文件夹失败:无法移动到自身的子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43260052/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:59:21  来源:igfitidea点击:

Bash move * to subfolder fail: cannot move to a subdirectory of itself

bash

提问by Augustin Riedinger

I have a folder with several files that I want to move to a subfolder:

我有一个包含多个文件的文件夹,我想将其移动到子文件夹中:

mkdir subfolder
mv ./* subfolder

But when I do this, I get:

但是当我这样做时,我得到:

mv: cannot move 'subfolder' to a subdirectory of itself

mv: cannot move 'subfolder' to a subdirectory of itself

How can I simply avoid this?

我怎样才能简单地避免这种情况?

回答by anubhava

Using extglobyou can do this:

使用extglob你可以这样做:

shopt -s extglob

mv !(subfolder) subfolder

Glob expression !(subfolder)will match everything except subfolder.

Glob 表达式!(subfolder)将匹配除subfolder.

回答by Keith Lyons

Was this on Windows Subsystem for Linux (WSL)?

这是在适用于 Linux 的 Windows 子系统 (WSL) 上吗?

If so, Windows recognizes the directory Jerry as equal to directory jerry, and WSL follows certain Windows rules for compatibility.

如果是这样,Windows 会将目录 Jerry 识别为等于目录 jerry,并且 WSL 遵循某些 Windows 兼容性规则。

If you want to rename the directory Jerry to jerry, you could do the following:

如果要将目录 Jerry 重命名为 jerry,可以执行以下操作:

mv Jerry placeholder_name
mv placeholder_name jerry

回答by HeyWatchThis

Umm I had a similar problem. Took me a couple of tries! But I ended up with

嗯,我遇到了类似的问题。花了我几次尝试!但我结束了

mkdir ../subfolder
mv * ../subfolder
mv ../subfolder .

回答by Augustin Riedinger

I went for:

我去了:

find . -maxdepth 1 -not -name subfolder -exec mv -t subfolder {} +

find . -maxdepth 1 -not -name subfolder -exec mv -t subfolder {} +

though I still find the syntax very unreadable...

虽然我仍然发现语法非常难以理解......