簡體   English   中英

在Linux中使用bash腳本重命名子目錄中的文件

[英]Renaming files in subdirectories using a bash script in linux

我試圖重命名文件夾中子目錄內的所有文件。 文件結構如下:

PQR/
    aaa.txt
    bbb.jpg
    ccc.jif
XYZ/
    aaa.txt
    bbb.jpg
    ccc.jif 
LMN/
    aaa.txt
    bbb.jpg
    ccc.jif

我想要的是:

PQR/
    PQR_aaa.txt
    PQR_bbb.jpg
    PQR_ccc.jif
XYZ/
    XYZ_aaa.txt
    XYZ_bbb.jpg
    XYZ_ccc.jif 
LMN/
    LMN_aaa.txt
    LMN_bbb.jpg
    LMN_ccc.jif

我正在嘗試使用以下bash腳本,但是它給了我各種各樣的錯誤(我對shell腳本比較陌生,請耐心等待)。

#!/bin/bash/
for dirname in */
do
 cd $dirname
 dirnew=${dirname/\///}  #To escape the forward slash
 for file in *.*          #Reading the files in the directory
 do
   mv "$file" "$dirnew"_"$file"
 done
 cd ..
done
$dirnew = ${dirname/\///}

變量賦值的等號周圍不能有空格,並且左側不應有$ 另外,您的替代品中斜線過多。

dirnew=${dirname/\//}

暫無
暫無

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

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