LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2011, 05:31 AM   #1
proleader
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
Question What command to delete all files and subfolders, but not root folder ?


Hello,

What command to delete all files and subfolders, but not root folder ?

Example:
Folder A has: folder B, folder C, ... and files
How to delete everything in folder A but not delete folder A?
 
Old 09-14-2011, 06:03 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,699
Blog Entries: 23

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
enter folder A then issue:

Quote:
rmdir -R folderB
Luck, and, be carefull what you delete. Alternatively (and safer) - delete from the desktop and recover fron the dustbin when needed, if you did'nt have to do that for a while, then delete the dustbin...

Thor
 
Old 09-14-2011, 06:09 AM   #3
Blackened Justice
Member
 
Registered: Jun 2011
Posts: 82

Rep: Reputation: Disabled
Quote:
Originally Posted by proleader View Post
Hello,

What command to delete all files and subfolders, but not root folder ?

Example:
Folder A has: folder B, folder C, ... and files
How to delete everything in folder A but not delete folder A?
I would cd into Folder A and then rm -r *
That should do the trick.
 
Old 09-14-2011, 12:37 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Or if you don't want to cd first, just include the path to the parent directory.
Code:
rm -r ./directoryA/*
rm -r /full/path/to/directoryA/*
Read here for more on globbing:
http://mywiki.wooledge.org/glob

And a similar recent thread in which I posted about a new globbing feature:
http://www.linuxquestions.org/questi...command-902529

And finally, for more complex stuff, use find.
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

(PS: "folder" is the Windows term. *nix uses "directory". )
 
Old 09-14-2011, 04:42 PM   #5
wcampos
LQ Newbie
 
Registered: May 2011
Posts: 2

Rep: Reputation: Disabled
Well this looks fine:

rm -r /full/path/to/directoryA/*

I will just add the option -f

rm -Rf /full/path/to/directoryA/* and this will delete all inside directoryA without erasing the root directory

---------- Post added 09-14-11 at 02:43 PM ----------

Well this looks fine:

rm -r /full/path/to/directoryA/*

I will just add the option -f

rm -Rf /full/path/to/directoryA/* and this will delete all inside directoryA without erasing the root directory
 
Old 09-15-2011, 12:00 PM   #6
proleader
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
I will try these commands.
Thank you!
 
Old 09-15-2011, 12:20 PM   #7
samtoddler
Member
 
Registered: Jul 2011
Location: Delhi
Distribution: Cent OS/RHEL
Posts: 37

Rep: Reputation: Disabled
What command to delete all files and subfolders, but not root folder ?

Quote:
Originally Posted by proleader View Post
I will try these commands.
Thank you!
Please mark it as solved if you got the solution..

samtoddler
 
Old 09-15-2011, 01:27 PM   #8
rahulkya
Member
 
Registered: Feb 2009
Location: New Delhi
Distribution: Linux mint,Ubuntu,Debian,RHEL 5,slackware 13.1, free BSD,solaris.
Posts: 186
Blog Entries: 3

Rep: Reputation: 33
add i to see what are the files your command is deleting..
Quote:
rm -rfi *
Quote:
Don't forget to cd into that directory which you want empty
Mark this as solved as suggested above ..

Last edited by rahulkya; 09-15-2011 at 01:34 PM.
 
Old 09-15-2011, 01:53 PM   #9
samtoddler
Member
 
Registered: Jul 2011
Location: Delhi
Distribution: Cent OS/RHEL
Posts: 37

Rep: Reputation: Disabled
What command to delete all files and subfolders, but not root folder ?

Quote:
Originally Posted by rahulkya View Post
add i to see what are the files your command is deleting..


Mark this as solved as suggested above ..

just add "-v" switch to the mentioned command this makes the command verbose.

Code:
cd <directory>
rm -rvf *

samtoddler
 
Old 09-15-2011, 03:25 PM   #10
rahulkya
Member
 
Registered: Feb 2009
Location: New Delhi
Distribution: Linux mint,Ubuntu,Debian,RHEL 5,slackware 13.1, free BSD,solaris.
Posts: 186
Blog Entries: 3

Rep: Reputation: 33
Thanks Sam, you clarified my POST...
Adding i will prompt for deletion ....
And adding v will show file being deleted
 
Old 09-16-2011, 06:02 AM   #11
samtoddler
Member
 
Registered: Jul 2011
Location: Delhi
Distribution: Cent OS/RHEL
Posts: 37

Rep: Reputation: Disabled
What command to delete all files and subfolders, but not root folder ?

Quote:
Originally Posted by rahulkya View Post
Thanks Sam, you clarified my POST...
Adding i will prompt for deletion ....
And adding v will show file being deleted
Hi,

I think you can get all your questions answered by just one single command
Code:
rm --help
or man rm
for the query you mentioned

remove -f switch from the

Code:
rm -rvf
samtoddler
 
  


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] Moving files from folders and subfolders to a specific folder mrj2 Linux - Newbie 12 08-22-2010 01:40 PM
Which unnecessary files can i delete in the root folder? epamuk Linux - Server 5 08-13-2009 08:06 AM
find all files in current folder and all subfolders and sort all by timestamp dlnlinux Linux - Newbie 2 06-03-2008 12:29 PM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 11:54 AM
changeing permissions for a folder + subfolders and files ? YBA^[x] Slackware 7 08-06-2004 05:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration