LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices



Reply
 
Search this Thread
Old 06-07-2013, 03:15 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Rep: Reputation: 49
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.

Last edited by stf92; 06-07-2013 at 03:23 PM.
 
Old 06-07-2013, 04:21 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
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 **/.*(.)
 
Old 06-07-2013, 04:22 PM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,119

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
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).
 
Old 06-07-2013, 04:31 PM   #4
edorig
Member
 
Registered: Apr 2013
Location: France
Distribution: Slackware; Scientific Linux
Posts: 106

Rep: Reputation: Disabled
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.
 
Old 06-07-2013, 05:18 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
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.
 
Old 06-07-2013, 05:57 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
As far as I understand it the -delete option only deletes files.
 
Old 06-07-2013, 06:06 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,268

Original Poster
Rep: Reputation: 49
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!
 
Old 06-07-2013, 06:13 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
Code:
find /home/source_user -name ".*" -maxdepth 1 -exec cp -r '{}' /home/destination_user/ \;
Should do the job.
 
Old 06-07-2013, 09:04 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,608

Rep: Reputation: 673Reputation: 673Reputation: 673Reputation: 673Reputation: 673Reputation: 673
Quote:
Originally Posted by TobiSGD View Post
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 "."

Last edited by rknichols; 06-07-2013 at 09:07 PM.
 
Old 06-07-2013, 09:25 PM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
Quote:
Originally Posted by rknichols View Post
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 "."
Thanks for correcting that. I am never stopping to learn.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Delete regular files (not hidden files) with find + rm in one line older than 15 Virtuose Linux - Newbie 1 01-08-2011 06:24 PM
[SOLVED] How to delete multiple hidden files dbuehler Linux - Newbie 8 03-18-2010 08:09 AM
bash: mv hidden and not hidden files lupe Linux - General 4 06-22-2009 02:27 PM
Delete multiple hidden files Beresford Linux - General 3 06-27-2008 10:44 AM
How to delete hidden files??? kabucek Linux - Distributions 5 08-28-2006 12:21 AM


All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration