2013-12-21 5 views
3

보관 용 서비스에 의해 (많은 수의) 충돌 파일이 생성되었습니다. 이 파일들은 로컬 리눅스 파일 시스템에 있습니다.일괄 적으로 이름 바꾸기 보관함 충돌 파일

예제 파일 이름 = (마스터의 충돌 사본 2013년 12월 21일)를 컴파일 .sh로

나는이 경우 compile.sh에, 올바른 원래 이름으로 파일 이름을 변경하여 기존 파일을 삭제하고 싶습니다 그 이름. 이상적으로는 스크립트를 사용하거나 재귀 적으로 사용할 수 있습니다. 현재 디렉토리에서

#!/bin/bash 

folder=/path/to/dropbox 

clear 

echo "This script will climb through the $folder tree and repair conflict files" 
echo "Press a key to continue..." 
read -n 1 
echo "------------------------------" 

find $folder -type f -print0 | while read -d $'\0' file; do 
    newname=$(echo "$file" | sed 's/ (.*conflicted copy.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo "Found conflict file - $file" 

     if test -f $newname 
     then 
      backupname=$newname.backup 
      echo " " 
      echo "File with original name already exists, backup as $backupname" 
      mv "$newname" "$backupname" 
     fi 

     echo "moving $file to $newname" 
     mv "$file" "$newname" 

     echo 
    fi 
done 

답변

2

모든 파일 :

for file in * 
do 
    newname=$(echo "$file" | sed 's/ (.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo moving "$file" to "$newname" 
#  mv "$file" "$newname"  #<--- remove the comment once you are sure your script does the right thing 
    fi 
done 

편집

제공하는 솔루션을 찾고 주위 재생 및 추가 연구 후에 나는 함께 나를 위해 잘 작동 뭔가를 자갈길

또는 재발행하려면 다음 스크립트를 호출 할 스크립트에 넣으십시오. /tmp/myrename :

file="$1" 
newname=$(echo "$file" | sed 's/ (.*)//') 
if [ "$file" != "$newname" ]; then 
    echo moving "$file" to "$newname" 
#  mv "$file" "$newname"  #<--- remove the comment once you are sure your script does the right thing 
fi 

다음 find . -type f -print0 | xargs -0 -n 1 /tmp/myrename (파일 이름에 공백이 있기 때문에 추가 스크립트를 사용하지 않으면 명령 행에서 약간 어렵습니다).

+0

해결책 Guntram에 감사드립니다. 저를 올바른 길로 인도했습니다. 나는 당신이 제공 한 것을 가져 갔고 나의 목적에 더 잘 부합하도록 그것을 수정/확장했습니다. 리눅스 배쉬 (존경)의 힘에 대해 더 배웠다. – cemlo

1

작은 기여 :

나는이 스크립트에 문제가있었습니다. 이름에 공백이있는 파일은 사본을 만들지 않습니다. 그래서 라인 (17) 수정 :

------- 상처를 ------------- 컷 ---------

if test -f "$newname"

------- 잘라 내기 ------------- 잘라 내기 ---------

1

위의 스크립트는 이제 구식이되었습니다. 드롭 박스의 최신 버전이 글을 쓰는 시점에서 리눅스 민트에서 실행 괜찮 다음 작품 :

#!/bin/bash 

#modify this as needed 
folder="./" 
clear 

echo "This script will climb through the $folder tree and repair conflict files" 
echo "Press a key to continue..." 
read -n 1 
echo "------------------------------" 

find "$folder" -type f -print0 | while read -d $'\0' file; do 
    newname=$(echo "$file" | sed 's/ (.*Case Conflict.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo "Found conflict file - $file" 

     if test -f "$newname" 
     then 
      backupname=$newname.backup 
      echo " " 
      echo "File with original name already exists, backup as $backupname" 
      mv "$newname" "$backupname" 
     fi 

     echo "moving $file to $newname" 
     mv "$file" "$newname" 

     echo 
fi 
done 
관련 문제