LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (http://www.linuxquestions.org/questions/general-10/)
-   -   How to delete all the hidden files and only them? (http://www.linuxquestions.org/questions/general-10/how-to-delete-all-the-hidden-files-and-only-them-4175465148/)

stf92 06-07-2013 03:15 PM

How to delete all the hidden files and only them?
 
Hi: I want to delete all the hidden files and directories from my home directory. I made a test in a test directory and 'rm .*' seems to work, though it wants to erase . and .. too (but he can't because they are directories). Would it be safe to use it?

I would remove write permissions from the non hidden files but i have seen that rm does NOT care about the write bit.

TobiSGD 06-07-2013 04:21 PM

If you only want regular files to be deleted you can use find for that:
Code:

find . -type f -name ".*" -delete
If you use zsh as shell you also can do
Code:

rm **/.*(.)

tronayne 06-07-2013 04:22 PM

You do not want to delete the . or ..

You can use the find utility to get a list:
Code:

find ~/ -type f -name '.*'
For example, here's the list from my home directory:
Code:

find ~/ -type f -name '.*'   
/home/trona/.xscreensaver
/home/trona/.viminfo
/home/trona/.gnuplot-wxt
/home/trona/tutorial/.gmtcommands4
/home/trona/.ICEauthority
/home/trona/.bash_history
/home/trona/public_html/phpcb-examples/ch12/.DS_Store
/home/trona/public_html/phpcb-examples/.DS_Store
/home/trona/.esd_auth
/home/trona/.config/libreoffice/3/user/store/.templdir.cache
/home/trona/.config/chromium/Default/File System/000/p/.usage
/home/trona/.thunderbird/kadbhve8.default/.parentlock
/home/trona/.cvspass
/home/trona/.serverauth.2627
/home/trona/.screenrc
/home/trona/.install4j
/home/trona/.mozilla/firefox/lh09h7sl.default/.parentlock
/home/trona/.mozilla/seamonkey/i8p0jf67.default/.parentlock
/home/trona/Ikin/_rels/.rels
/home/trona/maps/.gmtcommands4
/home/trona/maps/.gmtdefaults4
/home/trona/.exrc
/home/trona/.sh_history
/home/trona/.Xauthority
/home/trona/.lesshst
/home/trona/.profile
/home/trona/Notes/.gmtcommands4
/home/trona/Desktop/.directory
/home/trona/.gtk-bookmarks
/home/trona/.kshrc
/home/trona/src/distance/.gmtcommands4
/home/trona/tmp/.xscreensaver-getimage.cache

You can modify the find command to remove those files and directories:
Code:

find ~/ -type f -name '.*' -exec rm -r {} \;
That'll do it.

Before you do anything, however, back up your entire home directory somewhere.

Also, look at the list -- there may be files you do not want deleted; e.g., .bashr, .profile, maybe a few others. In this case, simply
Code:

find ~/ -type f -name '.*' > dot_list
Edit that file, removing anything you don't want deleted then
Code:

for file in $(cat dot_list)
do
    rm -r ${file}
done

Again, make a back up before you screw it up (so you can unscrew it).

edorig 06-07-2013 04:31 PM

It depends what you want to do. 'rm .*' will erase all files with a name beginning with . but will not erase
the directories. To find the list of directories you may run find -type d -name ".*" -print > hidden_directories.sh
Then, you could edit hidden directories using vi to remove . and .. and insert a "rm -rf " at the beginning of each
line with the command :1,$s/^/rm -rf / . Doing sh hidden_directories.sh will then remove all the hidden directories.
This is inelegant, since one could instead use the -exec rm -rf {}; option in print with the proper -prune option to delete all these hidden directories, but this is safer if you don't want to erase your current directory or you
want to actually keep some of the hidden directories.

stf92 06-07-2013 05:18 PM

Thank you very much, guys. That's a lot of information. I'll try first with a simple 'find . -name "*." -delete'. I think this will also erase hidden directories.

TobiSGD 06-07-2013 05:57 PM

As far as I understand it the -delete option only deletes files.

stf92 06-07-2013 06:06 PM

You are right. I just ran find with -exec and all went well. I am now trying to copy all hidden files and directories from a second user's home directory into the home directory of the first one, but I'm not sure how to do it. I never realized how difficult it can be to delete or copy files!

TobiSGD 06-07-2013 06:13 PM

Code:

find /home/source_user -name ".*" -maxdepth 1 -exec cp -r '{}' /home/destination_user/ \;
Should do the job.

rknichols 06-07-2013 09:04 PM

Quote:

Originally Posted by TobiSGD (Post 4967443)
As far as I understand it the -delete option only deletes files.

The "-delete" option will delete empty directories. The "-delete" option implies "-depth" (process a directory's contents before operating on the directory itself), so the "-delete" does have the opportunity to empty the directory before trying to delete it. Of course if the expression preceding the "-delete" leaves anything unmatched there, then the deletion will fail with "Directory not empty."

Oh yes, find is smart enough not to delete "." :tisk:

TobiSGD 06-07-2013 09:25 PM

Quote:

Originally Posted by rknichols (Post 4967509)
The "-delete" option will delete empty directories. The "-delete" option implies "-depth" (process a directory's contents before operating on the directory itself), so the "-delete" does have the opportunity to empty the directory before trying to delete it. Of course if the expression preceding the "-delete" leaves anything unmatched there, then the deletion will fail with "Directory not empty."

Oh yes, find is smart enough not to delete "." :tisk:

Thanks for correcting that. I am never stopping to learn.


All times are GMT -5. The time now is 12:52 PM.