How to remove all files from a directory?

The closest I've gotten is

# rm /path/to/directory/*.*


but that doesn't work for files that don't have an extension...

-

Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.

To remove everything in a directory use:

rm /path/to/directory/*


You can use the -r option to also remove any directories inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.

-
If you also want to delete hidden files run shopt -s dotglob before running rm (...) –  danjjl Sep 6 '11 at 8:10
Wow that's pretty incredible that you know that off the top of your head. What exactly does the asterisk mean? –  user784637 Sep 6 '11 at 8:12
The * means all files ;) *.* means all files containing a . somewhere in the name –  Rinzwind Sep 6 '11 at 8:20
@Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la. –  DisgruntledGoat Sep 6 '11 at 13:43

If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...

-
• rm -r /path/to/directory/* -f

or

• sudo rm -r /path/to/directory/*
-

If you want to delete only files in /path/to/directory you can do

find /path/to/directory -type f -print0| xargs -0 rm

or

find /path/to/directory -type f -exec rm {} \;

You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.

-
GNU find as a -delete predicate. If you still want to use -exec, substituting \; with + will gather rm calls together, increasing efficiency. –  enzotib Sep 6 '11 at 12:19
large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree. –  Osis Sep 6 '11 at 12:53
You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} \; –  Richm Sep 6 '11 at 13:52
you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100. –  Richm Sep 6 '11 at 13:54
You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f –  Richm Sep 6 '11 at 14:10

You can cd into the directory and then run the command rm *.* just like in DOS if you remember.

-

To delete all files and directories(including the hidden ones) in a directory, you can try the following:

• delete the folder, then recreate it

\rm -rf dir_name && mkdir dir_name

• use find

find dir_name -mindepth 1 -delete

Here we specify -mindepth 1 to exclude the directory dir_name itself.
Take a look at the following link:
http://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory

-