我正试图将嵌套在子目录中的文件向上移动一级。我在osx终端,是bash的新手。我很确定这很简单,我就是不知道怎么做。
我想更改如下的文件结构:
~/container
/1-A
LEVEL 1 - 000.jpg
LEVEL 1 - 001.jpg
LEVEL 1 - 002.jpg
/1-B
/2-A
LEVEL 2 - 007.jpg
LEVEL 2 - 008.jpg
LEVEL 2 - 009.jpg
/1-C
LEVEL 1 - 003.jpg
LEVEL 1 - 004.jpg
LEVEL 1 - 005.jpg
LEVEL 1 - 006.jpg
/1-D
/2-C
LEVEL 2 - 010.jpg
LEVEL 2 - 011.jpg
LEVEL 2 - 012.jpg
LEVEL 2 - 013.jpg
LEVEL 2 - 014.jpg
/1-E
LEVEL 1 - 015.jpg
LEVEL 1 - 016.jpg
LEVEL 1 - 017.jpg
/1-F
/2-B
/3-A
LEVEL 3 - 018.jpg
LEVEL 3 - 019.jpg
LEVEL 3 - 020.jpg
LEVEL 3 - 021.jpg
一个看起来像这样的:
~/container
/1-A
LEVEL 1 - 000.jpg
LEVEL 1 - 001.jpg
LEVEL 1 - 002.jpg
/1-B
LEVEL 2 - 007.jpg
LEVEL 2 - 008.jpg
LEVEL 2 - 009.jpg
/2-A
/1-C
LEVEL 1 - 003.jpg
LEVEL 1 - 004.jpg
LEVEL 1 - 005.jpg
LEVEL 1 - 006.jpg
/1-D
LEVEL 2 - 010.jpg
LEVEL 2 - 011.jpg
LEVEL 2 - 012.jpg
LEVEL 2 - 013.jpg
LEVEL 2 - 014.jpg
/2-C
/1-E
LEVEL 1 - 015.jpg
LEVEL 1 - 016.jpg
LEVEL 1 - 017.jpg
/1-F
/2-B
LEVEL 3 - 018.jpg
LEVEL 3 - 019.jpg
LEVEL 3 - 020.jpg
LEVEL 3 - 021.jpg
/3-A
我试过了:
find ~/container -mindepth 3 -type f -exec mv {} . ;
和
find ~/container -mindepth 3 -type f -exec mv {} .. ;
但是,它们将文件相对于根目录移动,而不是相对于文件本身所在的目录。换句话说,它们会将文件向上移动太远。我希望它们准确地向上移动一个级别,无论它们一开始嵌套得多么深。
有人能帮忙吗?
这应该可以解决您的问题:
find ~/container -mindepth 3 -type f -execdir mv "{}" ./.. ;
描述:
在此文件夹内进行find ~/container
搜索-mindepth 3
仅显示3个目录下的文件-type f
只显示文件(不显示目录(-execdir
对其目录中的每个文件执行以下命令mv "{}" ./..
将文件向上移动一个目录;
为所选的每个文件重复一个新命令
find ~/container -mindepth 3 -type f | xargs -i bash -c 'mv "{}" $(dirname "{}")/..'
对于每个文件,它都会找到它的目录名,并将其向上移动一级。
**更新**
使用GNU查找。。。
find ~/container -mindepth 3 -type f -execdir mv "{}" $(dirname "{}")/.. ;
while循环。。。
find ~/container -mindepth 3 -type f | while read file; do
mv "$file" "$(dirname "$file")/.."
done