The script that you found is flawed with parsing output of ls
command (you can read about why you shouldn't use ls
in scripting here).
A better approach is to use find
command and pipe its output to xargs
.
Since in the original script you operate on the files that are in a specific directory, I've tailored the command accordingly. Navigate to the directory where you want to hide the files and run the part bellow:
find . -maxdepth 1 -type f ! -name ".*" -printf "%f\0" | xargs -0 -I file mv file .file
Here is a small demo in my home directory. I create 3 files and use above command to hide them.
$ 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
The above works for files. To make it work for directories, simply change -type f
to -type d
.
Demo:
$ 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/
file
and.file
for a start, you silently loose the second one. Very dangerous scripts...ls -a
for that. A.B in his answer usesrename
which is better approach.