LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-25-2014, 02:27 AM   #1
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Rep: Reputation: 1
a question on rm command


Hi,

When we have a list of files and directories, how can I remove all the files but not the directories?
 
Old 06-25-2014, 02:29 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
I think you cannot solve it with rm alone, you need to use find too.
 
Old 06-25-2014, 05:34 AM   #3
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
yeah you're probably best using find. ie:
Code:
find /dir/in/question -type f -exec rm {} \;
that should get rid of all files in subdirectories too but keep the directory structure intact
 
Old 06-25-2014, 09:52 AM   #4
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Quote:
Originally Posted by kbnuts View Post
Code:
find /dir/in/question -type f -exec rm {} \;
You'd probably want to quote that {} as "{}" in case there are file names with whitespace.

find, at least GNU find, also has a -delete option which requires no additional parameters.

Code:
find /dir/in/question -type f -delete
 
2 members found this post helpful.
Old 06-25-2014, 10:24 AM   #5
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
Quote:
{} as "{}" in case there are file names with whitespace.
You're right of course.
 
Old 06-26-2014, 01:52 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by kbnuts View Post
Quote:
{} as "{}" in case there are file names with whitespace.
You're right of course.
Except that it is (a) unnecessary, and (b) those quote marks wouldn't be seen by the find command anyway. Those curly braces are just a marker that tells find where to place the file name in the argument list it builds, and that argument list is not processed by another shell, so all characters in that file name will be passed literally in a single argument.
 
Old 06-26-2014, 02:33 PM   #7
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 5,999

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
I thought "rm *" would cover it?
 
  


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
ps command question MarkoSan Linux - Newbie 8 02-22-2013 01:00 AM
ar command question! ss_me Linux - Newbie 1 02-16-2010 03:50 AM
du command question mokku Linux - Newbie 4 07-16-2008 10:48 AM
command question simple2 Programming 4 04-09-2006 08:52 PM
mv command question skilletlicker Linux - General 2 02-03-2006 02:22 PM


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