LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 02:15 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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 02:23 PM.
 
Old 06-07-2013, 03:21 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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, 03: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,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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, 03:31 PM   #4
edorig
Member
 
Registered: Apr 2013
Location: France
Distribution: Slackware; Ubuntu
Posts: 134

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, 04:18 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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, 04:57 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

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

Original Poster
Rep: Reputation: 76
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, 05:13 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

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

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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 08:07 PM.
 
Old 06-07-2013, 08:25 PM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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



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 05:24 PM
[SOLVED] How to delete multiple hidden files dbuehler Linux - Newbie 8 03-18-2010 07:09 AM
bash: mv hidden and not hidden files lupe Linux - General 4 06-22-2009 01:27 PM
Delete multiple hidden files Beresford Linux - General 3 06-27-2008 09:44 AM
How to delete hidden files??? kabucek Linux - Distributions 5 08-27-2006 11:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 12:01 AM.

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
Open Source Consulting | Domain Registration