Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all files in a directory (do not touch any folders or anything within them)

I would like to know whether rm can remove all files within a directory (but not the subfolders or files within the subfolders)?

I know some people use:

rm -f /direcname/*.*

but this assumes the filename has an extension which not all do (I want all files - with or without an extension to be removed).

like image 361
toop Avatar asked May 05 '12 08:05

toop


People also ask

How do you delete all files in a directory except some?

Using Extended Globbing and Pattern Matching Operators Also, with the ! operator, we can exclude all files we don't want glob to match during deletion. Let's look at the list of pattern matching operators: ?(pattern-list) matches at least zero and at most one occurrence.

Which command is used to delete all files in a directory?

Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.

What is rmdir command?

The rmdir command removes the directory, specified by the Directory parameter, from the system. The directory must be empty before you can remove it, and you must have write permission in its parent directory.


1 Answers

Although find allows you to delete files using -exec rm {} \; you can use

find /direcname -maxdepth 1 -type f -delete

and it is faster. Using -delete implies the -depth option, which means process directory contents before directory.

like image 82
troller Avatar answered Sep 28 '22 09:09

troller