Fedora Linux Support Community & Resources Center
  #1  
Old 16th March 2008, 11:31 AM
mattcen Offline
Registered User
 
Join Date: Dec 2007
Location: Melbourne, Australia
Posts: 79
Remove directory contents but not directory?

Oh I am so annoyed that I have to ask this. I found a site somewhere that explained exactly what I want to do, but do you think I could find it again? Of course not.

This is a general Linux/Unix question and not specific to any distro, but I figured I'd ask it here rather than sign up to another linux forum. (I tried to search for this in the forum but it's hard to find appropriate keywords for this particular topic :S)

Basically I would like to remove the entire contents of a directory from within the directory, and retain the directory itself. "rm -rf .* *" would be the obvious choice, except that this also tried to remove "." and ".." from the directory, which is obviously undesirable.

The site I found with this information used some form of regular expression to specify not to delete . and .. (i think).

I realise that I could simply remove the entire directory and then recreate it (rm -rf <dir>; mkdir <dir>), but I have an ulterior motive for wanting to do this. I also would like to make a backup of my home directory, but want to back up the hidden files and standard files separately.
My backup method consists of simply bzipping the files into the root of the home directory, and then copying the bzip to another computer (I am doing this from my University Unix share and copying to my home server with SCP). I would like to bzip the hidden (starting with a dot) files to one file, and the standard files to another file.

Any help would be greatly appreciated.

Sincerely,
Mattcen
__________________
My PC Specs:

Desktop: Pentium D 2.8GHz, 3GB DDR2 RAM, NVidia Geforce 7950GT, ~800GB HDD Space, LeadTek DTV2000H TV Card
24" Dell 2408 WFP and 15" LCD, running WinXP and Ubuntu 8.04.

Laptop: Acer 4235 WLMi, Core2 Duo 2.0GHz, 1GB DDR2 RAM, NVidia 7300Go, 160GB HDD Space, 15.4 Inch widescreen LCD
running WinXP and Fedora 9 in dual boot.

Server: Pentium 4 2.8GHz, 2GB DDR RAM, 320GB HDD, Fedora 8 with various Virtual Machines, including Edubuntu Server 8.04 for PXE Booting.

Last edited by mattcen; 16th March 2008 at 11:37 AM.
Reply With Quote
  #2  
Old 16th March 2008, 12:14 PM
Astarta Offline
Registered User
 
Join Date: Jul 2006
Location: Russia,Moscow
Age: 31
Posts: 22
rm -rf /path_to_your_dir/*
or if you want to remove files when you're in the directory itself, just type
rm -rf *
__________________
Man doth not yield him to the angels, nor unto death utterly, save only through the weakness of his feeble will... (c)
Reply With Quote
  #3  
Old 16th March 2008, 02:24 PM
ivancat Offline
Registered User
 
Join Date: Dec 2007
Posts: 596
rm -rf ./* to remove files but not the directory itself

and to backup hidden files you can use

gzip -c --verbose --fast ./.* > hidden.gz (also you can add -r parameter for recursive) // this is for hidden
__________________
Ivan Cat (registered linux user #471784)
Linux Projects: constat | nconv
Fedora-related: Message In the Bottle
Reply With Quote
  #4  
Old 17th March 2008, 04:40 AM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,911
Quote:
Originally Posted by ivancat
rm -rf ./* to remove files but not the directory itself

and to backup hidden files you can use

gzip -c --verbose --fast ./.* > hidden.gz (also you can add -r parameter for recursive) // this is for hidden

Almost - no cigar ... The command "rm -rf ./*" does not remove all files in the current directory. It uses the shell '*' expansion which ignored hidden files - files that begin heir names with a '.'.
Reply With Quote
  #5  
Old 17th March 2008, 01:10 AM
mattcen Offline
Registered User
 
Join Date: Dec 2007
Location: Melbourne, Australia
Posts: 79
Thank you both for your responses, but neither of them answer my question.

ivancat, your gzip command is likely to be helpful, thank you.

Both of you have mentioned the "rm -rf" command, which I have already mentioned in my original post! Neither of these will delete hidden files or folders from the given directory!

I realise that my post may have been a bit longer than necessary, but that was so that I could clarify the situation properly! I even bolded the "rm -rf" command that was useless for my purposes!

Thanks again for the responses, but I can't help feeling that you didn't properly read my post .
Any other ideas are still welcome.

Kind regards,
Mattcen
__________________
My PC Specs:

Desktop: Pentium D 2.8GHz, 3GB DDR2 RAM, NVidia Geforce 7950GT, ~800GB HDD Space, LeadTek DTV2000H TV Card
24" Dell 2408 WFP and 15" LCD, running WinXP and Ubuntu 8.04.

Laptop: Acer 4235 WLMi, Core2 Duo 2.0GHz, 1GB DDR2 RAM, NVidia 7300Go, 160GB HDD Space, 15.4 Inch widescreen LCD
running WinXP and Fedora 9 in dual boot.

Server: Pentium 4 2.8GHz, 2GB DDR RAM, 320GB HDD, Fedora 8 with various Virtual Machines, including Edubuntu Server 8.04 for PXE Booting.
Reply With Quote
  #6  
Old 17th March 2008, 02:35 AM
jbannon Offline
Registered User
 
Join Date: Dec 2005
Posts: 909
rm -r /.* should work. In FC8 (& FC7 IIRC) rm is prevented from removing .. when used in this way.
__________________
Best regards,
Jim Bannon
(Registered Linux User #405603 :) )
Reply With Quote
  #7  
Old 17th March 2008, 05:49 AM
ivancat Offline
Registered User
 
Join Date: Dec 2007
Posts: 596
just add . so it will remove dotted files only
rm -rf ./.* // WARNING: as . and .. are considered as dotted by this command use -r with caution
rm -rf ./* // removes all others
__________________
Ivan Cat (registered linux user #471784)
Linux Projects: constat | nconv
Fedora-related: Message In the Bottle
Reply With Quote
  #8  
Old 17th March 2008, 08:04 AM
mattcen Offline
Registered User
 
Join Date: Dec 2007
Location: Melbourne, Australia
Posts: 79
Quote:
"rm -rf .* *" would be the obvious choice, except that this also tried to remove "." and ".." from the directory, which is obviously undesirable.
I just tried this on a test user, and because that command deletes "." and ".." as well, it completely wiped my entire home directory (and only stopped there because I didn't have permission to delete anything else!)

There is a regular expression that will prevent this, but I forget what it is.
__________________
My PC Specs:

Desktop: Pentium D 2.8GHz, 3GB DDR2 RAM, NVidia Geforce 7950GT, ~800GB HDD Space, LeadTek DTV2000H TV Card
24" Dell 2408 WFP and 15" LCD, running WinXP and Ubuntu 8.04.

Laptop: Acer 4235 WLMi, Core2 Duo 2.0GHz, 1GB DDR2 RAM, NVidia 7300Go, 160GB HDD Space, 15.4 Inch widescreen LCD
running WinXP and Fedora 9 in dual boot.

Server: Pentium 4 2.8GHz, 2GB DDR RAM, 320GB HDD, Fedora 8 with various Virtual Machines, including Edubuntu Server 8.04 for PXE Booting.
Reply With Quote
  #9  
Old 17th March 2008, 08:24 AM
mattcen Offline
Registered User
 
Join Date: Dec 2007
Location: Melbourne, Australia
Posts: 79
jbannon: I just tried your method which seems to work quite well, even though it comes up with warnings saying that "rm: cannot remove `.' or `..'" .

You said that this works on Fedora 7 and 8... are you saying that this is distribution specific (so if I try it on Solaris 10 for example, it may not work?)
__________________
My PC Specs:

Desktop: Pentium D 2.8GHz, 3GB DDR2 RAM, NVidia Geforce 7950GT, ~800GB HDD Space, LeadTek DTV2000H TV Card
24" Dell 2408 WFP and 15" LCD, running WinXP and Ubuntu 8.04.

Laptop: Acer 4235 WLMi, Core2 Duo 2.0GHz, 1GB DDR2 RAM, NVidia 7300Go, 160GB HDD Space, 15.4 Inch widescreen LCD
running WinXP and Fedora 9 in dual boot.

Server: Pentium 4 2.8GHz, 2GB DDR RAM, 320GB HDD, Fedora 8 with various Virtual Machines, including Edubuntu Server 8.04 for PXE Booting.
Reply With Quote
  #10  
Old 17th March 2008, 04:21 PM
jbannon Offline
Registered User
 
Join Date: Dec 2005
Posts: 909
Quote:
Originally Posted by mattcen
jbannon: I just tried your method which seems to work quite well, even though it comes up with warnings saying that "rm: cannot remove `.' or `..'" .

You said that this works on Fedora 7 and 8... are you saying that this is distribution specific (so if I try it on Solaris 10 for example, it may not work?)
Not sure matt, you would need to look at the documentation for the specific distribution.

If this is meant to be inside a script then the obvious thing to do would be to have a line like
Code:
(cd $1 && rm -r .?*)
where $1 is the directory argument. Note that ".." will not match the pattern because ? forces it to expect a non-special character after the ".". This might be what the source you can't find used.
__________________
Best regards,
Jim Bannon
(Registered Linux User #405603 :) )
Reply With Quote
  #11  
Old 17th March 2008, 06:39 PM
sideways Offline
Retired User
 
Join Date: Oct 2004
Location: London, UK
Posts: 4,999
There are many ways to do this, a tidy way in bash is to use the -A option on ls which specifically excludes . and ..

eg, change into the directory and type
Code:
rm -fr $(ls -A)
Reply With Quote
  #12  
Old 17th March 2008, 09:40 PM
Jongi Offline
Registered User
 
Join Date: Oct 2005
Posts: 1,952
I often will compile from source. when i do this i unzip to a folder in /usr/src. So for example, /usr/src/program. So when I want to remove the folder program and all its contents I do:

# cd /usr/src
# rm -rdfv program/

Program and everything in it it (hidden, dot files and sub directories) will be deleted. Everything else in /usr/src will remain.
__________________
Desktop (64-bit) - F12, Debian Sid, OpenSUSE 11.2, ArchLinux
Reply With Quote
  #13  
Old 17th March 2008, 10:12 PM
icydog Offline
Registered User
 
Join Date: Nov 2005
Posts: 451
just tried "rm -r * .*" on Ubuntu 7.10 and Fedora 8. It tells me:

rm: cannot remove directory `.'
rm: cannot remove directory `..'
Reply With Quote
  #14  
Old 17th March 2008, 10:37 PM
mattcen Offline
Registered User
 
Join Date: Dec 2007
Location: Melbourne, Australia
Posts: 79
Thank you all for your replies!

Jbannon: while it may seem that '..' will not match that pattern, 'rm' still seems to TRY to remove it (though it didn't succeed like it did when i tried 'rm -rf * .*'), so that is a little strange. Thanks.

sideways: That method is perfect. It's not the method that I remember finding, but it works fine - I don't know why I didn't think of it. I determined you can also use "rm -rf `ls -A`" instead (i think the $() is shell specific, while the `` is not... but am not sure).

Jongi: Thanks for the suggestion, but again, this method deletes the folder itself as well, which is not my intent (see original post).

icydog: So it looks like that command works as well - cool. The only difference between that and the one I tried (which wiped my whole home dir) is the force option, which I didn't thing to try removing .

Thank you all again. I think I have enough input for now to run with it, especially with the piping of "ls -A" into "rm" . I'm still going to look around for that regular expression though, because I thought it was REALLY cool!

Sincerely,
Mattcen
__________________
My PC Specs:

Desktop: Pentium D 2.8GHz, 3GB DDR2 RAM, NVidia Geforce 7950GT, ~800GB HDD Space, LeadTek DTV2000H TV Card
24" Dell 2408 WFP and 15" LCD, running WinXP and Ubuntu 8.04.

Laptop: Acer 4235 WLMi, Core2 Duo 2.0GHz, 1GB DDR2 RAM, NVidia 7300Go, 160GB HDD Space, 15.4 Inch widescreen LCD
running WinXP and Fedora 9 in dual boot.

Server: Pentium 4 2.8GHz, 2GB DDR RAM, 320GB HDD, Fedora 8 with various Virtual Machines, including Edubuntu Server 8.04 for PXE Booting.
Reply With Quote
  #15  
Old 17th March 2008, 10:41 PM
Jongi Offline
Registered User
 
Join Date: Oct 2005
Posts: 1,952
then try this from within the directory whose contents you want to remove:

to remove all files except for dot

rm -rdfv ./*

to remove the dot files and folders

rm -rdfv ./.*
__________________
Desktop (64-bit) - F12, Debian Sid, OpenSUSE 11.2, ArchLinux
Reply With Quote
Reply

Tags
contents, directory, remove

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make directory contents viewable in browser? larryjenkinz Linux Chat 3 1st June 2007 07:04 PM
Which is the contents of '/old' directory? guarriman Using Fedora 0 14th August 2006 05:25 PM
Compress contents of directory? Prion Using Fedora 2 15th November 2004 10:55 PM


Current GMT-time: 05:55 (Thursday, 23-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Uchana - University City Travel Photos on Instagram - Hatay Instagram Photos