我在磁盤的不同檔案夾中有大量的report.txt檔案。有些檔案被命名為Report.txt,有些檔案的后綴是reportmonday.txt。 所以我想找到并復制所有包含報告的檔案到一個單一的檔案夾,如果已經存在,則重命名。例如 report.txt report1.txt report2.txt
我試過一些命令
找到. -type f -iname 'report' -exec mv {}_renamed /home/ram/allreport. {}_renamed /home/ram/allreport*這個命令沒有重命名我的檔案名,而是重寫了。請幫助我
uj5u.com熱心網友回復:
一個有點復雜的方法來解決這個問題:
find . -type f -iname *report* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. 47{print $NF}47|sort -n|tail -n1"; com | getline result}{result ; printf "mv "%s" /home/ram/allreport/report.%s
",$0,result}' | bash,并再次運行它 ... find . -type f -iname *report* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. 47{print $NF}47|sort -n|tail -n1"; com | getline result}{result ; printf "mv "%s" /home/ram/allreport/report.%s
",$0,結果}'|bash這里有一個漂亮的awk部分的版本,有解釋的話:
BEGIN { # 前提動作,這發生在我們檢查我們 "找到 "的東西之前
com = "ls /home/ram/allreport/*[Rr]eport*|awk -F. '{print $NF}'|sort -n|tail -n1" # 我們在目標目錄上執行ls,以獲得最高的現有報告編號
com | getline result # 并將該數字存盤在變數result中。
}
{ # 這里開始處理find的輸出結果
result # 每一行的結果增加一個
printf "mv "%s" /home/ram/allreport/report.%s
", $0, result # 列印我們要執行的命令。
# 將原始檔案拖到目標目錄,并加上新的名稱和計數器 "result"。
}
uj5u.com熱心網友回復:
這是一個腳本,它將檢查任何檔案的名稱中是否包含 "報告"(不區分大小寫),并將其復制到一個所需的目錄。如果該目錄下已經存在一個該名稱的檔案,它將嘗試重命名為report1.txt,report2.txt等,如果它不能重命名為該數字,它將分配一個隨機后綴。比如report1354.txt。
注意,它將檢查目錄是否存在。如果目錄不存在,腳本將創建一個目錄。
#!/bin/bash
# 確保下面的變數不包含尾部的'/'。
# 它應該是'/home/tfp/allreports'而不是'/home/tfp/allreports/'
ALL_REPORTS_DIRECTORY="/home/tfp/allreports"
FILE_NAMES_ARRAY=$(find . -type f -name "*[rR][eE][pP][oO][rR][tT]*" 2>>/dev/null)
rename_suffix=1
如果 [[ -d ${ALL_REPORTS_DIRECTORY}]; then
echo "目錄存在,未創建"
否則
mkdir ${ALL_REPORTS_DIRECTORY}.
echo "目錄不存在,正在創建"
fi
# 給子檔案夾下的檔案添加隨機后綴
for file in ${FILE_NAMES_ARRAY}; do
file_name=$(basename $file)
if [[ -f "${ALL_REPORTS_DIRECTORY}/${file_name}"]; then
if [[ -f "${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt"]; then
random_suffix=$RANDOM
echo " 無法重命名'${file_name}'。后綴'${RENAME_SUFFIX}'已經存在,所以給出隨機后綴。"
cp $file ${ALL_REPORTS_DIRECTORY}/report${random_suffix}.txt
echo " '$file' -> 新名稱: report${random_suffix}.txt"
否則
echo "檔案'${file}'存在于'${ALL_REPORTS_DIRECTORY}'下。重命名為 report${RENAME_SUFFIX}.txt"
cp $file ${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt
echo " '$file' -> 新名稱: report${RENAME_SUFFIX}.txt"
fi
RENAME_SUFFIX=$(echo $((${RENAME_SUFFIX} 1))
否則
cp $file ${ALL_REPORTS_DIRECTORY}。
echo "復制了'${file}'"
fi
完成
這里有一些輸出的例子。
$ls
myreport.txt report1.txt report3.txt report55.txt report6.txt Report.txt script.sh sundayReport.txt testfolder
$bash script.sh
目錄存在。未創建
復制了'./Report.txt'。
復制了'./testfolder/rePorT.txt'。
復制了'./testfolder/RReport.txt'。
復制了'./myreport.txt'。
復制 './sundayReport.txt'。
復制 './report1.txt'。
復制 './report6.txt'。
復制 './report55.txt'。
復制了'./report3.txt'。
$bash script.sh
目錄存在。未創建
無法重命名'Report.txt'。后綴'1'已經存在,所以給出隨機的后綴
'./Report.txt' -> 新名稱:report3262.txt
檔案'./testfolder/rePorT.txt'存在于'/home/tfp/allreports'下。重命名為 report2.txt
'./testfolder/rePorT.txt' -> 新名稱: report2.txt
無法重命名'RReport.txt'。后綴'3'已經存在,所以給出隨機后綴
'./testfolder/RReport.txt' -> 新名稱: report6693.txt
檔案 './myreport.txt' 存在于 '/home/tfp/allreports' 中。
'./myreport.txt' -> 新名稱: report4.txt
檔案'./sundayReport.txt'存在于'/home/tfp/allreports'下。 重命名為 report5.txt
'./sundayReport.txt' -> 新名稱: report5.txt
無法重命名'report1.txt'。后綴'6'已經存在,所以給出隨機后綴
'./report1.txt' -> 新名稱: report31626.txt
檔案 './report6.txt' 存在于 '/home/tfp/allreports' 中。
'./report6.txt' -> 新名稱: report7.txt
檔案 './report55.txt' 存在于 '/home/tfp/allreports'下。重命名為 report8.txt
'./report55.txt' -> 新名稱: report8.txt
檔案 './report3.txt' 存在于 '/home/tfp/allreports'下。重命名為 report9.txt
'./report3.txt' -> 新名稱: report9.txt
$bash script.sh
目錄存在。未創建
無法重命名'Report.txt'。后綴'1'已經存在,所以給出隨機后綴
'./Report.txt' -> 新名稱: report32658.txt
不能重命名'rePorT.txt'。后綴'2'已經存在,所以給出隨機的后綴
'./testfolder/rePorT.txt' -> 新名稱: report1976.txt
無法重命名'RReport.txt'。后綴'3'已經存在,所以給出隨機后綴
'./testfolder/RReport.txt' -> 新名稱: report26897.txt
無法重命名'myreport.txt'。后綴'4'已經存在,所以給出隨機后綴
'./myreport.txt' -> 新名稱: report31297.txt
無法重命名'sundayReport.txt'。后綴'5'已經存在,所以給出隨機后綴
'./sundayReport.txt' -> 新名稱: report29608.txt
無法重命名'report1.txt'。后綴'6'已經存在,所以給出隨機的后綴
'./report1.txt' -> 新名稱: report7259.txt
無法重命名'report6.txt'。后綴'7'已經存在,所以給出隨機的后綴
'./report6.txt' -> 新名稱: report16691.txt
無法重命名'report55.txt'。后綴'8'已經存在,所以給出隨機的后綴
'./report55.txt' -> 新名稱: report12176.txt
無法重命名'report3.txt'。后綴'9'已經存在,所以給出隨機的后綴
'./report3.txt' -> 新名稱: report11087.txt
最終的ls來自目標目錄
$ls -lah
共計8,0K
drwxrwxr-x 2 tfp tfp 4,0K Sep 15 18:37 .
drwxr-xr-x 46 tfp tfp 4,0K Sep 15 18:32 .
-rw-rw-r-1 tfp tfp 0 Sep 15 18:36 myreport.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report11087.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report12176.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report16691.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report1976.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report1.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report26897.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report29608.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report2.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report31297.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report31626.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report3262.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:37 report32658.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report3.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report4.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report55.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report5.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report6693.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report6.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report7259.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report7.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 report8.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report9.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 rePorT.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 Report.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 RReport.txt
-rw-rw-r- 1 tfp tfp 0 Sep 15 18:36 sundayReport.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/307695.html
標籤:
