Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The closest I've gotten is

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

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

share|improve this question

9 Answers 9

up vote 39 down vote accepted

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.

share|improve this answer
6  
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
1  
The * means all files ;) *.* means all files containing a . somewhere in the name –  Rinzwind Sep 6 '11 at 8:20
6  
@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
    
@user784637 you are too easily impressed –  barlop Sep 3 at 16:10
  • rm -r /path/to/directory/* -f

or

  • sudo rm -r /path/to/directory/*
share|improve this answer

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...

share|improve this answer

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.

share|improve this answer
1  
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
1  
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.

share|improve this answer

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

share|improve this answer
    
Thanks for your advice and I've added more explanation for the links. –  zeekvfu Nov 28 '13 at 5:04

To remove all files in directory (including hidden files and subdirectories) run:

rm -rf /path/to/directory/{*,.*}
share|improve this answer

a simple way to remove all the files from directory

  1. simply go to that directory in command-line
  2. use the command

    rm `ls`
    
share|improve this answer
2  
That will not work itf you have fuile names containing e.g. space characters. It doesn't have any advantages over a simple rm *. –  Florian Diesch May 27 at 15:07
rm -rf /path/to/directory/*

This will delete the entire directory. If we use

rm -r /path/to/directory/*

it will only delete sub-directories inside the directory and if we use

rm /path/to/directory/*

it will only delete the files present inside the directory. And if a sub-directory is present it will throw an error stating it's a directory.

share|improve this answer
1  
-1 This is plain wrong. rm -r path/* and rm -rf path/* both delete all (non-hidden) entries of path whether they're directories or not. path itself isn't deleted by any of those commands. Please see rm(1). –  David Foerster Nov 23 at 9:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.