LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   a question on rm command (https://www.linuxquestions.org/questions/linux-newbie-8/a-question-on-rm-command-4175509094/)

sryzdn 06-25-2014 02:27 AM

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?

pan64 06-25-2014 02:29 AM

I think you cannot solve it with rm alone, you need to use find too.

kbnuts 06-25-2014 05:34 AM

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

Turbocapitalist 06-25-2014 09:52 AM

Quote:

Originally Posted by kbnuts (Post 5193591)
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

kbnuts 06-25-2014 10:24 AM

Quote:

{} as "{}" in case there are file names with whitespace.
You're right of course.

rknichols 06-26-2014 01:52 PM

Quote:

Originally Posted by kbnuts (Post 5193719)
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.

273 06-26-2014 02:33 PM

I thought "rm *" would cover it?


All times are GMT -5. The time now is 11:18 AM.