AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1110359
Accepted
Charles Green
Charles Green
Asked: 2019-01-17 09:59:52 +0800 CST2019-01-17 09:59:52 +0800 CST 2019-01-17 09:59:52 +0800 CST

从子文件夹中移动文件

  • 772

一段时间以来,我一直在反对这一点,但我真的不是一个好的剧本作家。道歉...

我正在运行 Ubuntu/gnome 18.10,并且有大量从我妻子的 mac 导出的图片。目录结构为:

year1
  (login dir name with spaces) - Month
    Image names
year2
  ...

如:

2013
  May 4, 2013
    Image1.jpg
    Image2.jpg
  May 5, 2013
    Image 1.jpg
    Image 3.jpg
  June 22, 2013

我想要的是:

2013
   January
     All the "january" images...
   February
     All the...

我可以轻松地创建目录mkdir {January..December}就足够了。但我不知道如何遍历丑陋的目录树(从 Mac 导出),移动图像,然后删除丑陋的目录。

command-line bash
  • 3 3 个回答
  • 545 Views

3 个回答

  • Voted
  1. Charles Green
    2019-01-19T08:49:25+08:002019-01-19T08:49:25+08:00

    有一个应用程序可以在单个命令行中移动和重命名文件 -exiftool

    sudo apt install libimage-exiftool-perl
    

    就我而言,使用的特定命令行是

    exiftool  '-FileName<DateTimeOriginal' -d <path-to-output-dir>/%Y/%m-%B/%Y%m%d_%H%M%S%%-c.%%e . -r
    

    或者

    exiftool  '-FileName<CreateDate' -d <path-to-output-dir>/%Y/%m-%B/%Y%m%d_%H%M%S%%-c.%%e . -r
    

    取决于图像中存在哪个标签。如果请求的标签不存在,应用程序会抛出警告并且不会移动该文件,而是继续处理。此应用程序还通过在文件名末尾附加一个数字来处理具有相同日期/时间的多个图像。

    我确实有几个没有 exif 数据的图像,我可以使用命令将 exif 数据添加到图像中

    exiftool -createdate='2011:12:04 12:00:00' * -overwrite_original
    

    例如,我将一些图像放在目录 ~/aa/test1 中并运行该工具,将输出放在 ~/aa/test2 中。结果如下:

    chick@dad:~/aa$ tree .
    .
    ├── test1
    │   ├── DSC00018.JPG
    │   ├── DSC00022.JPG
    │   ├── DSC00024.JPG
    │   ├── DSC00025.JPG
    │   ├── DSC00026.JPG
    │   ├── DSC00028.JPG
    │   ├── DSC00031.JPG
    │   ├── DSC00033.JPG
    │   └── Thumbs.db
    └── test2
        └── 2000
            └── 12-December
                ├── 20001222_185523.JPG
                ├── 20001222_200726.JPG
                ├── 20001222_200819.JPG
                ├── 20001222_201205.JPG
                ├── 20001222_201223.JPG
                ├── 20001222_210536.JPG
                ├── 20001222_211858.JPG
                └── 20001222_215950.JPG
    
    • 2
  2. Best Answer
    pa4080
    2019-01-18T02:54:28+08:002019-01-18T02:54:28+08:00

    这是这样的脚本:

    #!/bin/bash
    
    # The destination where the new directory structure will be created
    DEST="/tmp/new-order-pictures/"
    
    MONTHS=('Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec')
    
    # Walk through the first level directories, located in the current directory and go inside
    for year in */
    do
        cd "$year"
        # Walk through the months of the year
        for month in "${MONTHS[@]}"
        do
            # Walk through the second level directories
            for dir in */
            do
                # If we have coincidence between the name of the directory and the month
                # go inside, make a new destination directory; ignore character cases^^
                if [[ ${dir^^} =~ ${month^^} ]]
                then
                    cd "$dir"
                    dest="${DEST}${year}${month}"
                    mkdir -p "$dest"
                    find . -type f | while IFS= read -r item
                    do
                        # Copy the files to the new destination and
                        # add the file's md5sum to its name to prevent files lose
                        filename=$(basename -- "$item")
                        extn="${filename##*.}"
                        name="${filename%.*}"
                        cp "$item" "${dest}/${name}-$(md5sum "$item" | cut -f1 -d' ').${extn}"
                    done
                    cd ..
                fi
            done
        done
        cd ..
    done
    

    该脚本应在图像所在的第一级目录中执行。您应该调整目标目录 - DEST="/tmp/new-order-pictures/". 此版本的脚本依赖于所有文件都位于以某种方式包含月份名称的目录中。使用示例:

    user@host:~/Pictures$ tree .
    .
    ├── 2013
    │   ├── January 17, 2013
    │   │   ├── Image1.jpg
    │   │   └── Image 3.jpg
    │   ├── January 24, 2013
    │   │   └── Image2.jpg
    │   ├── January 25, 2013
    │   │   └── Image 3.jpg
    │   ├── June 22, 2013
    │   │   └── image1.jpg
    │   ├── May 4, 2013
    │   │   └── Image1.jpg
    │   └── May 5, 2013
    │       ├── Image1.jpg
    │       └── Image 2.jpg
    └── 2014
        ├── January 17, 2014
        │   ├── Image1.jpg
        │   └── Image 3.jpg
        ├── January 24, 2014
        │   └── Image2.jpg
        ├── January 25, 2014
        │   └── Image 3.jpg
        └── May 5
            ├── Image1.jpg
            └── Image 2.jpg
    
    12 directories, 14 files
    
    
    user@host:~/Pictures$ order.sh 
    
    
    user@host:~/Pictures$ tree /tmp/new-order-pictures/
    /tmp/new-order-pictures/
    ├── 2013
    │   ├── Jan
    │   │   ├── Image1-7b71d9fdfe5b15a2d1a4968c195f93ae.jpg
    │   │   ├── Image2-cbf4d36ff84e7ec24c05f8181236e6b8.jpg
    │   │   ├── Image 3-0bca5188fd3f3eb470533fdaf0630633.jpg
    │   │   └── Image 3-6a83880cae1aa57e19a7c45de7759e68.jpg
    │   ├── Jun
    │   │   └── image1-adb3bf995f1a25d008f758a7266d7be5.jpg
    │   └── May
    │       ├── Image1-511d541b35fcb38af8ada18d7961268c.jpg
    │       ├── Image1-a66c5863e6986605cb2ca6d622ae72a0.jpg
    │       └── Image 2-c34ffc32ce5d3901e1ad89b9fd15a877.jpg
    └── 2014
        ├── Jan
        │   ├── Image1-7b71d9fdfe5b15a2d1a4968c195f93ae.jpg
        │   ├── Image2-cbf4d36ff84e7ec24c05f8181236e6b8.jpg
        │   ├── Image 3-0bca5188fd3f3eb470533fdaf0630633.jpg
        │   └── Image 3-6a83880cae1aa57e19a7c45de7759e68.jpg
        └── May
            ├── Image1-511d541b35fcb38af8ada18d7961268c.jpg
            └── Image 2-c34ffc32ce5d3901e1ad89b9fd15a877.jpg
    
    7 directories, 14 files
    

    在我的例子中,脚本被命名order.sh并且它位于~/bin,因此我可以将它用作 shell 命令。在示例中,您可以看到目录结构已更改,但两种结构中的文件数均为 14。


    这是另一个版本的脚本,它使用mv而不是cp处理不在包含月份名称的目录中的文件。在运行此脚本之前,最好创建原始目录结构的备份副本。

    #!/bin/bash
    
    # The destination where the new directory structure will be created
    DEST="/tmp/new-order-pictures/"
    
    MONTHS=('Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec')
    
    # Walk through the first level directories, located in the current directory and go inside
    for year in */
    do
    
        cd "$year"
    
        # Walk through the months of the year
        for month in "${MONTHS[@]}"
        do
            # Walk through the second level directories
            for dir in */
            do
                # If we have coincidence between the name of the directory and the month
                # go inside, make a new destination directory; ignore character cases^^
                if [[ ${dir^^} =~ ${month^^} ]]
                then
    
                    cd "$dir"
                    dest="${DEST}${year}${month}"
                    mkdir -p "$dest"
    
                    while IFS= read -r item
                    do
                        # Copy the files to the new destination and
                        # add the file's md5sum to its name to prevent files lose
                        filename=$(basename -- "$item")
                        extn="${filename##*.}"
                        name="${filename%.*}"
                        mv "$item" "${dest}/${name}-$(md5sum "$item" | cut -f1 -d' ').${extn}"
                    done < <(find . -type f)
    
                    cd ..
    
                fi
    
            done
    
        done
    
        # Dial with the rest of the files for that $year
    
        dest="${DEST}${year}other"
    
        while IFS= read -r item
        do
            mkdir -p "$dest"
            filename=$(basename -- "$item")
            extn="${filename##*.}"
            name="${filename%.*}"
            mv "$item" "${dest}/${name}-$(md5sum "$item" | cut -f1 -d' ').${extn}"
        done < <(find . -type f)
    
        cd ..
    
    done
    

    使用示例:

    user@host:~/Pictures$ tree .
    .
    ├── 2013
    │   ├── January 17, 2013
    │   │   ├── Image1.jpg
    │   │   ├── Image 3.jpg
    │   │   └── video 7.mpg
    │   ├── January 25, 2013
    │   │   └── Image 3.jpg
    │   ├── June 22, 2013
    │   │   └── image1.jpg
    │   └── May 5, 2013
    │       ├── Image1.jpg
    │       └── Image 2.jpg
    └── 2014
        ├── Apr 7
        │   ├── Image1.jpg
        │   └── Image 2.jpg
        ├── Image 2.jpg
        ├── January 11, 2014
        │   ├── Image1.jpg
        │   └── Image 3.jpg
        ├── some other name
        │   └── some other name file inside.jpg
        ├── some other name file inside.jpg
        └── video 1.avi
    
    9 directories, 15 files
    
    user@host:~/Pictures$ order.sh 
    
    user@host:~/Pictures$ tree /tmp/new-order-pictures/
    /tmp/new-order-pictures/
    ├── 2013
    │   ├── Jan
    │   │   ├── Image1-7b71d9fdfe5b15a2d1a4968c195f93ae.jpg
    │   │   ├── Image 3-0bca5188fd3f3eb470533fdaf0630633.jpg
    │   │   ├── Image 3-6a83880cae1aa57e19a7c45de7759e68.jpg
    │   │   └── video 7-86764d9565469adfb22c8ef4f0b9c04f.mpg
    │   ├── Jun
    │   │   └── image1-adb3bf995f1a25d008f758a7266d7be5.jpg
    │   └── May
    │       ├── Image1-511d541b35fcb38af8ada18d7961268c.jpg
    │       └── Image 2-c34ffc32ce5d3901e1ad89b9fd15a877.jpg
    └── 2014
        ├── Apr
        │   ├── Image1-3c19da25e0e56ef0fc752a9e4f75b190.jpg
        │   └── Image 2-dcc35e86de393a014ac62e8c4390c7e6.jpg
        ├── Jan
        │   ├── Image1-ae34289b0bc5258f286165745ff3c258.jpg
        │   └── Image 3-1724adf2dfcc1d4a0dc50cb38ad2c510.jpg
        └── other
            ├── Image 2-eff5208f7eee6a536e48f9982b918dfb.jpg
            ├── some other name file inside-7d0a68e0b4e9cc3928744cb83f4d1136.jpg
            ├── some other name file inside-c2dd637e94a9025c3e1004d66f59539c.jpg
            └── video 1-c277d93a2427bedf3f0b8ae07427edb9.avi
    
    8 directories, 15 files
    

    之后,您可以进入目标目录并使用循环rename内的命令for来处理长名称:

    # For each directory on the second level
    for dir in */*
    do
        cd "$dir"
        rename 's/^.*(\.[0-9a-zA-Z]+)$/our $i; sprintf("Image-%03d$1", 1+$i++)/e' *
        cd ..
        cd ..
    done
    

    例子:

    user@host:~/Pictures$ cd /tmp/new-order-pictures/
    
    user@host:/tmp/new-order-pictures$ for dir in */*; do cd "$dir"; rename 's/^.*(\.[0-9a-zA-Z]+)$/our $i; sprintf("Image-%03d$1", 1+$i++)/e' *; cd ..; cd ..; done
    
    user@host:/tmp/new-order-pictures$ tree .
    .
    ├── 2013
    │   ├── Jan
    │   │   ├── Image-001.jpg
    │   │   ├── Image-002.jpg
    │   │   ├── Image-003.jpg
    │   │   └── Image-004.mpg
    │   ├── Jun
    │   │   └── Image-001.jpg
    │   └── May
    │       ├── Image-001.jpg
    │       └── Image-002.jpg
    └── 2014
        ├── Apr
        │   ├── Image-001.jpg
        │   └── Image-002.jpg
        ├── Jan
        │   ├── Image-001.jpg
        │   └── Image-002.jpg
        └── other
            ├── Image-001.jpg
            ├── Image-002.jpg
            ├── Image-003.jpg
            └── Image-004.avi
    
    8 directories, 15 files
    

    或者您可以(\.[0-9a-zA-Z]+)使用(\.jpg),然后在下一次迭代中使用(\.mpg)(分别Image-使用Video-)等进行更改。有关此用法的参考rename:

    • 如何从命令行按顺序重命名多个文件?

    • 一次重命名数百个文件以进行正确排序

    • 1
  3. puneet
    2019-01-18T06:41:53+08:002019-01-18T06:41:53+08:00

    所以,我假设你想要一个循序渐进的简单解决方案。首先,我将解决文件夹中的空白和逗号问题。我会先 cd 进入 year 文件夹并用于rename首先删除 Whitespace

    rename "s/ //g" *
    

    同样,去掉逗号

    rename "s/,//g" * 
    

    现在我已经拥有了所需的所有文件夹,我将使用创建一个基于月份的列表

    ls | grep "January" > January.txt
    

    现在制作文件夹“一月”

    mkdir January
    

    然后通过列表循环使用xargs以复制其内容

    cat January.txt | xargs -I {} cp -R {}/. ./January/
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve