2016-10-04 20 views
-1

私はディレクトリで実行され、現在のディレクトリの下にあるディレクトリのファイル名を変更するbashスクリプトを書こうとしています。私は、各ファイルの現在のパス名と、そのファイルの新しいパス名をエコーできるところに達しました。ディレクトリのディレクトリでファイル名を変更するには?

私がしなければならないことは、エコーをmvに変更して彼女を裂くことだと思った。違う!どうやらbashはmvがパス名で動作することを許しません。

ここから進める方法についてのアドバイスはありがたいです。

+1

もっと正確にあなたが行っていることと結果を表示できますか? "bashは' mv'がパス名で動作することを許さないでしょうが、一般的に真実ではありません。 –

+0

...適切な[MCVE](http://stackoverflow.com/help/mcve)、他の人々があなたが得ているのと同じエラーを見るために実行できるコードは理想的でしょう。たとえば、次のようなことをするかもしれません: 'tempdir = $(mktemp -d test.XXXXXX); mkdir -p "$ tempdir"/{dir1、dir2、dir3}/{subdir1、subdir2、subdir3}; if mv "$ tempdir/dir1" "$ tempdir/dir1new";次に "名前変更された$ tempdir/dir1から$ tempdir/dir1new"をエコーし​​ます。 else echo "$ tempdir/dir1の名前を$ tempdir/dir1newに変更できませんでした";あなたがやっていることと失敗の仕方を示すのに必要なすべてのディレクトリを作成します。 –

+0

チャールズ、私が得るメッセージは、以下のようなものです:usage:mv [-f | -i | -n] [-v]ソースターゲット mv [-f | -i | -n] [-v] source ...ディレクトリ – grok12

答えて

0

私は今働いています。私は下のコードを投稿しようとします。チャールズ・ダフィーとグレッグのウィキに心から感謝しています。

#!/bin/bash 
# I listen to many audiobooks. Sometimes the file names are 
# such that they cannot be directly loaded into iTunes because 
# the files names contain the track number before the disk number 
# so iTunes sorts all the track 01 and then all the track 02, etc rather 
# than all the tracks of disk 1 in order and then all the tracks of 
# disk 2 in order, etc. 
# 
# This script fixes this problem. The files are in sub-directories of 
# a master directory with one sub-directory for each disk and the 
# format of the files names is: 
# 
# 01 Disc 01 We Were Soldiers Once ... and Young.mp3 
# 
# The first 2 chars are the track number and it needs to be moved 
# somewhere after the disc number. This script moves it to just 
# before the .mp3. 

# You might have the same problem but a different format but you can 
# change the script. Greg's Wiki gives great Examples of Filename 
# Manipulation. Find it here: http://mywiki.wooledge.org/BashFAQ/073 
# 
#set -x 
cd " Place full path name of the master directory here " 
pwd 
for dir in * 
do 
    echo $dir 
    cd "${dir}" # Move from Master Directory to a sub-directory 
    pwd 
#ls 
    for nameext in *.mp3 
    do 
     echo $nameext 
     name=${nameext%.*} # name has the .mp3 removed 
     echo ${name} 
     tracknum=${name:0:2} # tracknum is the 1st 2 chars 
     echo ${tracknum} 
     name=${name:3} # Remove tracknum from front of name 
     echo ${name} 
     name=${name}" "${tracknum} # Add tracknum to end of name 
     echo ${name} 
     newnameext=${name}.mp3 # Add .mp3 back onto name 
     echo ${newnameext} 
     echo ${nameext}-${newnameext} 
     mv "${nameext}" "${newnameext}" 
     echo 


    done 

    cd .. # Move back to Master Directory 
done 
exit 
done 
関連する問題