 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

16th March 2008, 11:31 AM
|
|
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.
|

16th March 2008, 12:14 PM
|
|
Registered User
|
|
Join Date: Jul 2006
Location: Russia,Moscow
Age: 30
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)
|

16th March 2008, 02:24 PM
|
|
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
|

17th March 2008, 04:40 AM
|
 |
Registered User
|
|
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,300

|
|
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 '.'.
|

17th March 2008, 01:10 AM
|
|
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.
|

17th March 2008, 02:35 AM
|
|
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 :) )
|

17th March 2008, 05:49 AM
|
|
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
|

17th March 2008, 08:04 AM
|
|
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.
|

17th March 2008, 08:24 AM
|
|
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.
|

17th March 2008, 04:21 PM
|
|
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 :) )
|

17th March 2008, 06:39 PM
|
 |
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
|

17th March 2008, 09:40 PM
|
|
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
|

17th March 2008, 10:12 PM
|
|
Registered User
|
|
Join Date: Nov 2005
Posts: 447

|
|
|
just tried "rm -r * .*" on Ubuntu 7.10 and Fedora 8. It tells me:
rm: cannot remove directory `.'
rm: cannot remove directory `..'
|

17th March 2008, 10:37 PM
|
|
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.
|

17th March 2008, 10:41 PM
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 22:32 (Sunday, 19-05-2013)
|
|
 |
 |
 |
 |
|
|