Linux學習期間下載了一些英文學習資料,但下載時文件自動將空格轉換成下劃線_,且是大寫,不利於快速識別。故想通過腳本實現文件批量重命名。


Personal Requests

  • 腳本提示要操作的目錄路徑
  • 可對目錄下子目錄進行操作(一級子目錄即可)
  • 生成重命令後的文件,源文件移至文件目錄的origin子目錄下

Thinking

  • 通過read p提示輸入目錄路徑
  • 判斷路徑是否存在
  • 查找目錄下子目錄,通過管道符|輸出給while循環
  • 列出子目錄下符合條件的文件,通過管道符|輸出給while循環
  • 對文件名進行處理
  • 生成重命名後文件,將源文件保存至件目錄的origin子目錄下

Implementations

對文件名的處理,主要通過sed,使用bash字符串處理工具太過繁瑣,且有些需求無法滿足。

腳本下載 GitHub

#!/bin/bash
# 2016.01.20 17:30 Sat
# https://lempstacker.com
#Use Script To Auto Batch Rename Files

#將文件名中下劃線替換爲空格; 將Red Hat Enterprise Linux 7|Red Hat 7替換爲RHEL7;

# saveDir=$(dirname $(readlink -f "$0"))

#提示輸入目錄路徑
read -t 30 -p 'Please input filepath: ' path

#判斷輸入參數是否爲空
if [ -z "$path" ];then
    echo -e '\e[31mSorry, Please Specify A Filepath, Bye!\e[0m'
    exit 2
else
#判斷路徑是否存在
    if [ ! -d "$path" ];then
        echo -e '\e[31mSorry, Filepath not exists!\e[0m'
        exit 3
    fi
fi

#查詢目錄下子目錄
find "$path" -type d | while read subpath;do

    ls "$subpath"/*.pdf | while read filepath;do
        filename=${filepath##*/}
        #如果要將字母轉換爲小寫,可使用filename=${filename,,}
        cp -p "$filepath" "$subpath"/`echo "$filename" | sed -r 's@[_-]@ @g;s@Red Hat( Enterprise Linux)? 7 @RHEL7-@g;s@ en US@@g;s@ @@g'`

    mkdir "$subpath"/origin &> /dev/null
    mv "$filepath" "$subpath"/origin/

    done
    echo -e "\e[34mSuccessfully Format FileName in $subpath .\e[0m"

done

#腳本結束

Usage

命令格式bash BASH_NAME,根據提示輸入FILEPATH,FILEPATH建議使用絕對路徑,如果其中含有空格,須用雙引號""將其包裹。

[flying@lemp test]$ bash filenameTransform.sh
Please input filepath: /home/flying/test
s: cannot access /home/flying/test/*.pdf: No such file or directory
Successfully Format FileName in /home/flying/test .
Successfully Format FileName in /home/flying/test/1Getting started .
Successfully Format FileName in /home/flying/test/2System administration .
ls: cannot access /home/flying/test/3Linux Containers and Atomic Host/*.pdf: No such file or directory
Successfully Format FileName in /home/flying/test/3Linux Containers and Atomic Host .
Successfully Format FileName in /home/flying/test/4Storage .
Successfully Format FileName in /home/flying/test/5Security .
Successfully Format FileName in /home/flying/test/6Development .
Successfully Format FileName in /home/flying/test/7Virtualization .
Successfully Format FileName in /home/flying/test/8Clustering .
Successfully Format FileName in /home/flying/test/9Identity management .
ls: cannot access /home/flying/test/epub/*.pdf: No such file or directory
Successfully Format FileName in /home/flying/test/epub .
[flying@lemp test]$

以下是操作前目錄層級

[flying@lemp test]$ pwd
/home/flying/test
[flying@lemp test]$ tree -L 1
.
├── 1Getting\ started
├── 2System\ administration
├── 3Linux\ Containers\ and\ Atomic\ Host
├── 4Storage
├── 5Security
├── 6Development
├── 7Virtualization
├── 8Clustering
├── 9Identity\ management
├── epub
├── filenameTransform.sh
└── test.tar.gz

10 directories, 2 files
[flying@lemp test]$

操作前子目錄下文件

[flying@lemp test]$ cd 1Getting\ started/
[flying@lemp 1Getting started]$ tree
.
├── Red_Hat_Enterprise_Linux-7-7.0_Release_Notes-en-US.pdf
├── Red_Hat_Enterprise_Linux-7-7.1_Release_Notes-en-US.pdf
├── Red_Hat_Enterprise_Linux-7-7.2_Release_Notes-en-US.pdf
├── Red_Hat_Enterprise_Linux-7-Installation_Guide-en-US.pdf
├── Red_Hat_Enterprise_Linux-7-Migration_Planning_Guide-en-US.pdf
└── Red_Hat_Enterprise_Linux-7-Package_Manifest-en-US.pdf

0 directories, 6 files
[flying@lemp 1Getting started]$

操作後

[flying@lemp 1Getting started]$ pwd
/home/flying/test/1Getting started
[flying@lemp 1Getting started]$ tree -L 1
.
├── origin
├── RHEL7-7.0ReleaseNotes.pdf
├── RHEL7-7.1ReleaseNotes.pdf
├── RHEL7-7.2ReleaseNotes.pdf
├── RHEL7-InstallationGuide.pdf
├── RHEL7-MigrationPlanningGuide.pdf
└── RHEL7-PackageManifest.pdf

1 directory, 6 files
[flying@lemp 1Getting started]$ tree -L 2
.
├── origin
│   ├── Red_Hat_Enterprise_Linux-7-7.0_Release_Notes-en-US.pdf
│   ├── Red_Hat_Enterprise_Linux-7-7.1_Release_Notes-en-US.pdf
│   ├── Red_Hat_Enterprise_Linux-7-7.2_Release_Notes-en-US.pdf
│   ├── Red_Hat_Enterprise_Linux-7-Installation_Guide-en-US.pdf
│   ├── Red_Hat_Enterprise_Linux-7-Migration_Planning_Guide-en-US.pdf
│   └── Red_Hat_Enterprise_Linux-7-Package_Manifest-en-US.pdf
├── RHEL7-7.0ReleaseNotes.pdf
├── RHEL7-7.1ReleaseNotes.pdf
├── RHEL7-7.2ReleaseNotes.pdf
├── RHEL7-InstallationGuide.pdf
├── RHEL7-MigrationPlanningGuide.pdf
└── RHEL7-PackageManifest.pdf

1 directory, 12 files
[flying@lemp 1Getting started]$

Expansion Proble

該腳本專爲處理PDF格式文件編寫,功能簡單。不支持目錄遞歸,自定義指定文件格式,對文件名的轉換要求須手動指定。待以後有相關需求,再進行改寫。


Change Log

  • 2016.01.30 20:00 Sat Asia/Beijing
    • 初稿完成,上傳至lempstacker部落格

  • Note Time: 2016.01.30 20:00 Sat
  • Note Location: Asia/Beijing
  • Writer: lempstacker