Unix Command to delete all files in a directory but save the directory

I am looking for a unix command to delete all files in a directory without deleting the directory itself. (note that the directory does not contain subdirectories).

+7
source share
8 answers
rm -i <directory>/* 

this should do the trick

EDIT: Added -i just in case (security first). the directory must be a full or relative path (e.g. /tmp/foo or ../trash/stuffs )

+9
source

to try

 rm -r yourDirectory/* 

it deletes the entire file in the directory "yourdirectory"

+5
source

You can use find /path/to/your/folder/ -delete to delete everything in this folder.

While the rm wildcard will work with too many files ("The argument list is too long"), this works regardless of the number of files.

You can also delete only files, but save any subdirectories:

 find /path/to/your/folder/ -type f -delete 

You can also specify any other find support criteria to limit "results".

+3
source

you can delete all files from the current directory with rm * if you want to delete from a specific directory, enter rm /path/*

+2
source

If you are in the directory where you want to delete all files, then the following command works fine:

 rm * 
+1
source

you can use rm -r /UrDir/*.* This ignores files in subdirectories

0
source

It will help you

 rm path/* 

eg:

 rm ../mydir/* 

In this command, if mydir has any sub_directory! it will cause an error message and skip this sub_directory and delete the rest of the files in the main directory.

0
source

If you want to delete the entire file, as well as the entire directory, which means everything, try the following:

 rm -rf * 
0
source

All Articles