LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04: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, 05:03 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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, 05: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, 11:37 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 03: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, 11:00 AM   #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, 11:20 AM   #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, 12: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 12:34 PM.
 
Old 09-15-2011, 12: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, 02: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, 05: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



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

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

All times are GMT -5. The time now is 04:33 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