2017-01-23 10 views
2

"DOCUMENT.PDF"という名前のファイルが何千もあり、パスの数値識別子に基づいて名前を変更したいとします。残念ながら、私はリネームコマンドにアクセスできないようです。パス内のパターンに基づいてファイルの名前を変更

三つの例:

/000/000/002/605/950/ÐÐ-02605950-00001/DOCUMENT.PDF 
/000/000/002/591/945/ÐÐ-02591945-00002/DOCUMENT.PDF 
/000/000/002/573/780/ÐÐ-02573780-00002/DOCUMENT.PDF 

が自分の親ディレクトリを変更せず、として名前を変更する:

2605950.pdf 
2591945.pdf 
2573780.pdf 
+0

私は名前の変更コマンド*へのアクセス権を持っていないように見えます*、あなたは 'MVを実行する能力を持っていない意味'コマンド?元のファイルが '/ 000/000/002/...'にある場合、どのフォルダに基づいていますか?現在のディレクトリ、またはルート?結果のファイルはどこに行きたいのですか? – lurker

+0

はい、mvまたはcpを実行できます。元のファイルは、現在のディレクトリ(ルートではない)からのものです。結果のファイルは現在のディレクトリに移動できます。ご協力いただきありがとうございます! – Cinda

+0

'mv'はUnixのファイルの名前を変更する方法です。あなたは** m ** o ** v **別の名前にしています。 – lurker

答えて

0

は、forループを使用して、mvコマンドに

for file in * 
do 
    num=$(awk -F "/" '{print $(NF-1)}' file.txt | cut -d "-" -f2); 
    mv "$file" "$num.pdf" 
done 
0

を使用Bash 4.0以降のglobstarでこれを行うことができます:

cd _your_base_dir_ 
shopt -s globstar    
for file in **/DOCUMENT.PDF; do # loop picks only DOCUMENT.PDF files 
    # here, we assume that the serial number is extracted from the 7th component in the directory path - change it according to your need 
    # and we don't strip out the leading zero in the serial number 
    new_name=$(dirname "$file")/$(cut -f7 -d/ <<< "$file" | cut -f2 -d-).pdf 

    echo "Renaming $file to $new_name" 

    # mv "$file" "$new_name" # uncomment after verifying 
done 

同様の問題を語るこの関連記事を参照してください:How to recursively traverse a directory tree and find only files?

関連する問題