이름에 공백이 있는 디렉터리에서 파일 복사

이름에 공백이 있는 디렉터리에서 파일 복사

이것이 내가 시도한 것입니다.

inputFile=$(zenity --file-selection --title "Test" --text "Please select the file for analysis." | sed -e "s/ /\\\ /g")

복사 명령이 작동하도록 sed공백을 a 및 공백으로 바꾸는 작업을 수행했습니다 . \그런 다음 Zenity 파일 선택 GUI에서 내부 값 inputFile/home/username/Documents/Learning\ and\ Development/testfile.txt.

이제 이 파일을 작업 디렉토리에 복사하려고 하면

cp $inputFile .

그래도 오류가 반환됩니다.

cp: cannot stat ‘/home/user/Documents/Learning\\’: No such file or directory
cp: cannot stat ‘and\\’: No such file or directory
cp: cannot stat ‘Development/WhatsApp.apk’: No such file or directory

이것을 우회할 수 있는 방법이 있나요? 사실 저는 프로그램을 작성하고 있어요. 따라서 공백을 피하기 위해 폴더 이름을 바꾸라고 사용자에게 말하고 싶지 않습니다.

답변1

큰따옴표로 묶어서 쉽게 트릭을 수행할 수 있습니다.

cp "$inputFile" /destination/

문자를 큰따옴표('"')로 묶으면 '$', '`', '\'를 제외하고 따옴표 안의 모든 문자의 리터럴 값이 유지됩니다. 자세히 알아보기:http://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html

답변2

공백이 나타난 곳에 *를 추가하면 다음과 같이 복사할 수 있습니다.

 cp  /home/user/users*tst.txt  /home/user/Downloads

사용자 tst.txt를 홈 폴더의 다운로드 폴더로 복사하려는 위치

관련 정보