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

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

Move all folders except one

linuxbashshellunix

提问by Victor

I have two directories dir1 and dir2. I need to move the content of folder dir1 to dir2 except one folder dir1/src.

我有两个目录 dir1 和 dir2。我需要将文件夹 dir1 的内容移动到 dir2,除了一个文件夹 dir1/src。

I tried this

我试过这个

mv !(src) dir1/* dir2/

But it dosn't work, it still displays this error

但它不起作用,它仍然显示此错误

bash: !: event not found

回答by David

Maybe you are looking for something like this?

也许你正在寻找的东西像这样

The answer to my question there states that what you are trying to to is achievable by using the extglobbash shell option. You can turn it on by executing shopt -s extglobor by adding that command to your ~/.bashrcand relogin. Afterwards you can use the function.

我的问题的答案指出,您可以通过使用extglobbash shell 选项来实现您的目标。您可以通过执行shopt -s extglob或将该命令添加到您的~/.bashrc并重新登录来打开它。之后就可以使用该功能了。



To use your example of moving everything from dir1except dir1/srcto dir2, this should work:

要使用您的移动一切从例子dir1,除了dir1/srcdir2,这应该工作:

mv -vt dir2/ dir1/!(src)

Example output:

示例输出:

$ mkdir -pv dir1/{a,b,c,src} dir2
mkdir: created directory 'dir1'
mkdir: created directory 'dir1/a'
mkdir: created directory 'dir1/b'
mkdir: created directory 'dir1/c'
mkdir: created directory 'dir1/src'
mkdir: created directory 'dir2'
$ ls -l dir1/
total 16
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 a
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 b
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 c
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 src
$ ls -l dir2/
total 0
$ shopt -s extglob
$ mv -vt dir2/ dir1/!(src)
'dir1/a' -> 'dir2/a'
'dir1/b' -> 'dir2/b'
'dir1/c' -> 'dir2/c'
$ ls -l dir1/
total 4
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 src
$ ls -l dir2/
total 12
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 a
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 b
drwxrwxr-x 2 dw dw 4096 Apr  7 13:30 c


More information about extglob can be found here.

可以在此处找到有关 extglob 的更多信息。