최근에 데스크톱 파일과 폴더를 숨기는 스크립트를 발견했습니다. 다음은 스크립트입니다.
#!/bin/bash
#
cd /home/ramvignesh/Desktop
for f in `ls`; do
mv "$f" ".$f"
done
스크립트가 제대로 작동하지 않습니다. 공백이 포함된 파일은 숨기지 않습니다. 예를 들어 '제목 없는 문서'라는 파일이 있으면 다음과 같은 오류가 발생합니다. . .
mv: cannot stat ‘Untitled’: No such file or directory
mv: cannot stat ‘Document’: No such file or directory
스크립트가 왜 그런 방식으로 동작하는지 알려주세요. 누구든지 나를 위해 스크립트를 수정할 수 있나요? 미리 감사드립니다.
답변1
발견한 스크립트에 명령 출력 구문 분석에 결함이 있습니다 ( 스크립팅에서 ls
사용하면 안되는 이유를 읽을 수 있습니다).ls
여기).
더 나은 접근 방식은 명령을 사용 find
하고 출력을 xargs
.
원본 스크립트에서는 특정 디렉터리에 있는 파일에 대해 작업을 수행하므로 그에 따라 명령을 조정했습니다. 파일을 숨기려는 디렉터리로 이동하고 다음 부분을 실행합니다.
find . -maxdepth 1 -type f ! -name ".*" -printf "%f\0" | xargs -0 -I file mv file .file
여기 내 홈 디렉토리에 있는 작은 데모가 있습니다. 3개의 파일을 생성하고 위 명령을 사용하여 숨깁니다.
$ touch file1 file2 file3
$ find . -maxdepth 1 -type f ! -name ".*" -printf "%f\0" | xargs -0 -I file mv file .file
$ ls -a
./ .bash_logout Desktop/ .file1 .gnupg/ .macromedia/ Pictures/ .ssh/ .xsession-errors
../ .bashrc .dmrc .file2 .ICEauthority .mkshrc .profile Templates/ .xsession-errors.old
.adobe/ .cache/ Documents/ .file3 .lesshst .mozilla/ .psensor/ Videos/
.bash_history .config/ Downloads/ .gconf/ .local/ Music/ Public/ .Xauthority
위의 내용은 파일에 적용됩니다. 디렉토리에서 작동하게 하려면 -type f
간단히 -type d
.
데모:
$ ls
dirone/ dirthree/ dirtwo/
$ find . -maxdepth 1 -type d ! -name ".*" -printf "%f\0" | xargs -0 -I file mv file .file
$ ls
$ ls -a
./ ../ .dirone/ .dirthree/ .dirtwo/
답변2
rename
이라는 이름의 작은 스크립트를 사용합니다 hide_desktop_files
.
#!/bin/bash
dir="$PWD"
cd ~/Desktop
rename 's/(.*)/.$1/' *
cd "$dir"
예
% ls -ogla ~/Desktop
total 92
drwxr-xr-x 3 4096 Aug 15 20:45 .
drwxr-xr-x 236 86016 Aug 15 20:46 ..
-rw-rw-r-- 1 0 Aug 15 20:45 bar
-rw-rw-r-- 1 0 Aug 15 20:45 foo
drwxrwxr-x 2 4096 Aug 15 20:45 .foo
% ./hide_desktop_files
rename(bar, .bar)
foo not renamed: .foo already exists
% ls -ogla ~/Desktop
total 92
drwxr-xr-x 3 4096 Aug 15 20:45 .
drwxr-xr-x 236 86016 Aug 15 20:47 ..
-rw-rw-r-- 1 0 Aug 15 20:45 bar
-rw-rw-r-- 1 0 Aug 15 20:45 foo
drwxrwxr-x 2 4096 Aug 15 20:45 .foo