4

I recently found a script for hiding desktop files and folders. The following is the script:

#!/bin/bash
#
cd /home/ramvignesh/Desktop
for f in `ls`; do
mv "$f" ".$f"
done

The script isn't working properly. It is not hiding the files named with spaces in them. For example, If I have a file named 'Untitled Document', I get the following errors. . .

mv: cannot stat ‘Untitled’: No such file or directory
mv: cannot stat ‘Document’: No such file or directory

Please let me know why the script behaves in such a way. Can anyone correct the script for me? Thanks in advance.

4
  • 1
    And if you have in your directory file and .file for a start, you silently loose the second one. Very dangerous scripts...
    – Rmano
    Sep 28, 2014 at 7:19
  • Is there a script a safer script?
    – Ramvignesh
    Sep 28, 2014 at 7:38
  • 1
    @Ramvignesh Yes. Try this one out. Sep 28, 2014 at 7:51
  • It's evidently written by inexperienced linux user. It's using ls to list files. That's one. Second, you wouldn't loose files named with leading dot because by default ls shows only non-hidden files. You need ls -a for that. A.B in his answer uses rename which is better approach. Aug 15, 2015 at 19:07

2 Answers 2

2

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/
1
  • Needs av upvote :)
    – A.B.
    Aug 15, 2015 at 20:43
0

Using rename and a little script with the name hide_desktop_files.

#!/bin/bash
dir="$PWD"
cd ~/Desktop
rename 's/(.*)/.$1/' *
cd "$dir"

Example

% 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .