Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.
To remove all non-hidden files* in a directory use:
rm /path/to/directory/*
However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.
To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:
rm -r /path/to/directory/*
* Hidden files and directories are those whose names start with . (dot) character, e.g.: .hidden-file or .hidden-directory/. Note that, in Bash, if the dotgloboption (which is off by default) is set, rm will act on hidden files too, because they will be included when * is expanded by the shell to provide the list of filename arguments.
To remove a folder with all its contents (including all interior folders):
rm -rf /path/to/directory
To remove all the contents of the folder (including all interior folders) but not the folder itself:
rm -rf /path/to/directory/*
or, if you want to make sure that hidden files/directories are also removed:
rm -rf /path/to/directory/{*,.*}
To remove all the "files" from inside a folder(not removing interior folders):
rm -f /path/to/directory/{*,.*}
Warning: if you have spaces in your path, make sure to always use quotes.
rm -rf /path/to the/directory/*
is equivalent to 2 separate rm -rf calls:
rm -rf /path/to
rm -rf the/directory/*
To avoid this issue, you can use 'single-quotes'(prevents all expansions, even of shell variables) or "double-quotes"(allows expansion of shell variables, but prevents other expansions):
rm -rf "/path/to the/directory/"*
Where:
rm - stands for remove
-f - stands for force which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.
-r - stands for recursive which means that you want to go recursively down every folder and remove everything.
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...
Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).
Why not -depth or -delete?
Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert
-not \( -path "${DIR}/subdir" -prune \)
before the -mindepth argument to exclude subdir from having its contents deleted.
Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.
To remove all non-hidden files* in a directory use:
However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.
To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:
* Hidden files and directories are those whose names start with
.
(dot) character, e.g.:.hidden-file
or.hidden-directory/
. Note that, in Bash, if thedotglob
option (which is off by default) is set,rm
will act on hidden files too, because they will be included when*
is expanded by the shell to provide the list of filename arguments.To remove a folder with all its contents (including all interior folders):
To remove all the contents of the folder (including all interior folders) but not the folder itself:
or, if you want to make sure that hidden files/directories are also removed:
To remove all the "files" from inside a folder(not removing interior folders):
Warning: if you have spaces in your path, make sure to always use quotes.
is equivalent to 2 separate
rm -rf
calls:To avoid this issue, you can use
'
single-quotes'
(prevents all expansions, even of shell variables) or"
double-quotes"
(allows expansion of shell variables, but prevents other expansions):Where:
rm
- stands for remove-f
- stands for force which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.-r
- stands for recursive which means that you want to go recursively down every folder and remove everything.To remove all files in directory (including hidden files and subdirectories) run:
To delete all files and directories(including the hidden ones) in a directory, you can try the following:
delete the folder, then recreate it
use
find
Here we specify
-mindepth 1
to exclude the directory dir_name itself.Take a look at the following link:
https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory
If you want to delete only files in /path/to/directory you can do
or
You can do loads with
find
, the advantage is you can list what is found without piping it torm
so you can see what will be deleted before you start.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...Since this question is constantly at the top of Google when I search for this myself:
The other answers suffer from different problems:
Some of them include
.
and..
which is noisy, confusing, and annoying.Some of them forget hidden files (files beginning with a dot).
They don't delete in a correct (deepest-first) order to allow directory deletion.
They descend into other (mounted) file systems, which is often undesired.
They're difficult to extend properly with extra parameters (more on that below).
So, to RECURSIVELY delete all files AND folders in a directory, do this:
Note that I added an
-xdev
argument to prevent descending into mounts (like/proc
etc.).Why not
-depth
or-delete
?Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow
-prune
ing a subdirectory (without introducing more problems). By contrast with this method, you could insertbefore the
-mindepth
argument to excludesubdir
from having its contents deleted.You can
cd
into the directory and then run the commandrm *.*
just like in DOS if you remember.To delete current directory, you could for example use
rm -d ./*
-d tells to delete directories as well.