フォルダ名の一部を削除する方法

フォルダ名の一部を削除する方法

フォルダ名のリストがあります

file1xxx
file2xxx
file3xxx

名前から「xxx」を削除したいです。私は次のbashを試しました

for dirname in $(cat ${in}/all.txt); do # all.txt include the name of the folders in specific path ${in}
    [ -d "$dirname" ] || continue
    mv ${in}/$dirname ${in}/${dirname//.nii/}
done

しかし、それは役に立たない!どんな提案がありますか?

ベストアンサー1

ここで行うbashことができます:

while IFS= read -r dir; do [[ -d $dir ]] && mv -i "$dir" "${dir%???}"; done <all.txt

all.txtその後、ファイルを1行ずつ読み、その行が表すディレクトリがあることを確認します。存在する場合、それに応じて名前が変更されます。

編集する:

1文字だけを削除するには、以下を使用してください。パラメータ拡張パターン${dir%???}はです${dir%?}。ここで、メタ文字は?の個々の文字を表しますbash。だから:

while IFS= read -r dir; do [[ -d $dir ]] && mv -i "$dir" "${dir%?}"; done <all.txt

おすすめ記事