【问题标题】:moving files in subdirectories up one (and *only* one) level将子目录中的文件向上移动一级(并且*仅*一级)
【发布时间】:2015-11-25 01:01:23
【问题描述】:

我正在尝试将嵌套在子目录中的文件向上移动一层。我在 os x 终端中,并且是 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 {} .. \;

但是这些移动文件相对于根目录,而不是文件本身所在的目录。换句话说,它们将文件移动得太远了。我希望它们能向上移动一层,不管它们的嵌套有多深。

谁能帮忙?

【问题讨论】:

    标签: bash directory mv subdirectory


    【解决方案1】:

    这应该可以解决您的问题:

    find ~/container -mindepth 3 -type f -execdir mv "{}" ./.. \;
    

    说明:
    find ~/container 在此文件夹内搜索。
    -mindepth 3 仅显示低于 3 个目录的文件。
    -type f 仅显示文件(不显示目录)。
    @987654325 @ 对其目录中的每个文件执行以下命令。
    mv "{}" ./.. 将文件上移一个目录。
    \; 对每个选定的文件重复一个新命令。

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

      • 谢谢!不过,它仍然不适合我。当我尝试时,我收到这条消息:xargs: illegal option -- i usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr] [-L number] [-n number [-x]] [-P maxprocs] [-s size] [utility [argument ...]]
      • 您可能使用的是较旧的 xargs。试试xargs -I {} mv "{}" $(dirname "{}")/..
      • while 循环可以解决问题。非常感谢!如果你愿意,我很想知道 "$(dirname "$file")/.." 的语法是如何工作的。
      • $() 是在子shell 中运行命令的语法。 dirname filename 将返回文件的父目录,.. 将从那里上移一级
      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多