2015-10-27 102 views
0

我有一個文件夾(稱爲testdir)與子文件夾中的許多不同的文件類型。有許多文件名稱相同但擴展名不同,即使是具有相同名稱/擴展名的文件(但可能不完全相同)。 我需要一個腳本,它只將來自testdir及其子文件夾的mp3和mp4文件存檔到一個存檔文件夾中(而不會將它們放入子文件夾中)。如果archieve文件夾不存在,請創建它。 但是 - 只複製沒有.mp3(同名)的.mp4文件 - 比較.mp3文件,如果有多個相同的名稱並複製它們不相同並重命名它們 - 複製.mp4文件(僅)沒有.mp3(具有相同名稱) 該腳本需要有2個參數,因此它應該像arch arch_dir archive_dir一樣工作,並在用戶使用它時顯示錯誤消息。另外如果source_dir不存在。 這是我走到這一步:腳本來比較mp3和mp4文件

#!/bin/bash 
#declare variables 
SOURCE_DIR=$1 
ARCHIEVE_DIR=$2 
#if the user forgets to use 2 arguments: 
if [ $# -ne 2 ] 
then echo Usage: arch source_dir archieve_dir 
fi 
#if the source_dir does not exist: 
if [ ! -d $SOURCE_DIR ] 
then echo ERROR: Source directory is missing! 
fi 
# if archieve_dir does not exist, create it 
if [ ! -d $ARCHIEVE_DIR ] 
then mkdir $ARCHIEVE_DIR 
fi 
echo $ARCHIEVE_DIR created. 
cd $ARCHIEVE_DIR 
find . -type f \(-iname "*.mp3" -o -iname "-.mp4" \) 
# copy all the .mp4 files that have no .mp3 pairs (with the same name) 
#copy .mp3 files that have .mp4 identicals (leave mp4s) 
# copy and rename the .mp3 files that are not identical but have the same name 

任何形式的幫助,非常感謝!在此先感謝

+0

對不起,我剛剛注意到,在標題中我輸入了錯誤的文件類型。我正在尋找mp3和mp4實現。 – astennu

+0

您可以編輯自己的問題,只需點擊問題 –

+0

下方的小編輯鏈接,謝謝!我剛來這地方。 – astennu

回答

0

對於你的第一個要求,你可以去通過在一個循環中的所有的MP4文件,並測試它們:

#!/bin/bash 
# .. your parameter checks .. 

# copy all the .mp4 files that have no .mp3 pairs (with the same name) 
for file in $(find . -type f -iname "*.mp4") 
do 
    filemp3=$(echo $file | sed s'/.$/3/') 
    if [[ $(find . -type f -iwholename $filemp3|wc -l) == 0 ]] ; then 
    cp $file $ARCHIVE_DIR/ 
    fi 
done 

對於第二個要求,這將是足以只是複製所有的MP3:

# copy .mp3 files that have .mp4 identicals (leave mp4s) 
find . -type f -iname "*.mp3" -exec cp -t $ARCHIVE_DIR/ {} + 

你的第三個要求,我不知道如果我挺其次,如果你的意思是你想檢查的相同名稱的mp3文件的內容不同,你可以使用diff工具,像下面,但查找所有相同名稱的文件和提供的邏輯他們的差異,你需要弄清楚自己

# copy and rename the .mp3 files that are not identical but have the same name 
# ... 
DIFF=$(diff a b) 
if [ "$DIFF" != "" ] 
then 
    echo "The files differ" 
fi 
# ... 

也有使用一些tool,旨在尋找重複,像dupeguru(贏,蘋果,Ubuntu的,Arch Linux的)的選項。