簡體   English   中英

用於批量重命名文件的 Shell 腳本

[英]Shell Script for Bulk renaming of files

我想通過更改前綴來遞歸地重命名目錄路徑中的所有文件。

例如

XYZMyFile.h
XYZMyFile.m
XYZMyFile1.h
XYZMyFile1.m
XYZMyFile2.h
XYZMyFile2.m

ABCMyFile.h
ABCMyFile.m
ABCMyFile1.h
ABCMyFile1.m
ABCMyFile2.h
ABCMyFile2.m

這些文件位於具有多層的目錄結構下。 有人可以幫我編寫此批量任務的 shell 腳本嗎?

一種不同的方法可能:

ls *.{h,m} | while read a; do n=ABC$(echo $a | sed -e 's/^XYZ//'); mv $a $n; done

說明:

ls *.{h,m} --> Find all files with .h or .m extension
n=ABC --> Add a ABC prefix to the file name
sed -e 's/^XYZ//' --> Removes the XYZ prefix from the file name
mv $a $n --> Performs the rename

首先設置globstar ,然后使用如下重命名:

# shopt -s globstar # This will cause '**' to expand to each and everything
# ls -R
.:
nXYZ1.c  nXYZ2.c  nXYZ3.c  subdir  XYZ1.m  XYZ2.m  XYZ3.m
nXYZ1.h  nXYZ2.h  nXYZ3.h  XYZ1.c  XYZ2.c  XYZ3.c
nXYZ1.m  nXYZ2.m  nXYZ3.m  XYZ1.h  XYZ2.h  XYZ3.h

./subdir:
nXYZ1.c  nXYZ1.m  nXYZ2.h  nXYZ3.c  nXYZ3.m  XYZ1.h  XYZ2.c  XYZ2.m  XYZ3.h
nXYZ1.h  nXYZ2.c  nXYZ2.m  nXYZ3.h  XYZ1.c   XYZ1.m  XYZ2.h  XYZ3.c  XYZ3.m
# rename 's/^XYZ(.*.[mh])$/ABC$1/;s/^([^\/]*\/)XYZ(.*.[mh])$/$1ABC$2/' **
# ls -R
.:
ABC1.h  ABC2.m  nXYZ1.c  nXYZ2.c  nXYZ3.c  subdir  XYZ3.c
ABC1.m  ABC3.h  nXYZ1.h  nXYZ2.h  nXYZ3.h  XYZ1.c
ABC2.h  ABC3.m  nXYZ1.m  nXYZ2.m  nXYZ3.m  XYZ2.c

./subdir:
ABC1.h  ABC2.h  ABC3.h  nXYZ1.c  nXYZ1.m  nXYZ2.h  nXYZ3.c  nXYZ3.m  XYZ2.c
ABC1.m  ABC2.m  ABC3.m  nXYZ1.h  nXYZ2.c  nXYZ2.m  nXYZ3.h  XYZ1.c   XYZ3.c
# shopt -u globstar # Unset gobstar

這可能是實現目標的最簡單方法。


注意1:這里我沒有像您注意到的nXYZnABC更改為nABC 如果要更改它們,則簡化的rename命令將是

rename 's/XYZ(.*.[mh])$/ABC$1/' **

注2:這個問題沒有提到多次出現XYZ 所以在這方面什么也沒做。

對於非遞歸,您可以使用rename這是一個perl腳本:

rename -v -n 's/^.+(?=MyFile)/what-you-want/' *.{h,m}  

測試

 dir >  ls | cat -n
     1  XYZMyFile1.h
     2  XYZMyFile1.m
     3  XYZMyFile.h
     4  XYZMyFile.m
 dir >  
 dir >  rename -v -n 's/^.+(?=MyFile)/what-you-want/' *.{h,m}
rename(XYZMyFile1.h, what-you-wantMyFile1.h)
rename(XYZMyFile1.m, what-you-wantMyFile1.m)
rename(XYZMyFile.h, what-you-wantMyFile.h)
rename(XYZMyFile.m, what-you-wantMyFile.m)
 dir >  

對於遞歸,使用find + 這個命令


如果您無權訪問rename ,則可以直接使用perl ,如下所示:

perl -le '($old=$_) && s/^xzy/abc/g && rename($old,$_) for <*.[mh]>'

這是一個屏幕截圖

在此處輸入圖片說明


使用renrem ,這是我使用 C++ 開發的 CLI,專門用於重命名

人行

輕松findrename (/usr/bin 中的二進制文件,而不是提到的 Perl 函數)

是的,已經有一個命令可以非遞歸地執行此操作。

rename XYZ ABC XYZ*

rename --help

Usage:
 rename [options] expression replacement file...

Options:
 -v, --verbose    explain what is being done
 -s, --symlink    act on symlink target

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see rename(1).

編輯:錯過了問題的“多層目錄”部分,b/c 有點亂。 添加查找。

最容易記住:

find . -type f -name "*.pdf" -exec rename XYZ ABC {} \;

完成速度可能更快:

find . -type d -not -path "*/\.*" -not -name ".*" -exec rename XYZ ABC {}/*.pdf \;

我不確定如何比一個命令行代碼更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM