![공백이 포함된 tar 디렉토리](https://linux55.com/image/7247/%EA%B3%B5%EB%B0%B1%EC%9D%B4%20%ED%8F%AC%ED%95%A8%EB%90%9C%20tar%20%EB%94%94%EB%A0%89%ED%86%A0%EB%A6%AC.png)
내 사진을 tar 아카이브에 보관하고 싶습니다.
매년 사진이 포함된 하나의 카탈로그가 있으며, 매년 여러 개의 카탈로그가 있습니다.
Photos
2005
2006
Some dir with spaces
Pic1.jpg
Pic2.jpg
...
Other dir here
...
2007
...
대상 구조에는 각 연도에 대한 디렉터리도 포함되어야 하지만, 해당 연도 디렉터리 내에는 다음과 같이 소스 디렉터리의 각 하위 디렉터리에 대한 tar.gz 파일이 있습니다.
Backup
2005
2006
Some_dir_with_spaces.tar.gz
Other_dir_here.tar.gz
...
2007
...
이를 위해 제가 만든 스크립트는 다음과 같습니다.
cd ~/Photos
for y in *
do
YEAR_DIR=~/Backup/$y
mkdir -p $YEAR_DIR
pushd $y
for d in *
do
f=`echo $d | tr ' ' '_' | tr -d ',.!'` # Clean the name
t=$YEAR_DIR/$f.tar.gz
d=$(printf '%q' "$d")
# echo tar czf $t "$d" # This outputs the command as expected
tar czf $t "$d" # but executing it results in error: Cannot stat: No such file or directory
done
popd
done
주석에서 제안한 대로 준비된 명령을 에코하는 것은 괜찮아 보이고 명령문을 복사하여 터미널에서 실행하면 작동하지만 tar czf $t "$d"
스크립트() 내에서 실행하면 오류가 발생합니다.
tar: Some\ dir\ with\ spaces: Cannot stat: No such file or directory
내가 뭘 잘못했나요?
PS 저는 Mac에서 이 작업을 수행했습니다.
답변1
공백을 탈출하기 위해 printf를 사용하고 있으며 ""를 인용하고 있습니다.
printf 호출을 생략하고 원본 $d를 큰따옴표와 함께 사용하면 작동합니다.
f=`echo $d | tr ' ' '_' | tr -d ',.!'` # Clean the name
t=$YEAR_DIR/$f.tar.gz
##d=$(printf '%q' "$d") escapes the spaces
# echo tar czf $t "$d" # This outputs the command as expected
tar czf $t "$d" ## "$d" also escapes the spaces