96

I am trying to remove all files and subdirectories in a directory. I used rm -r to remove all files, but I want to remove all files and subdirectories, excluding the top directory itself.

For example, I have a top directory like images. It contains the files header.png, footer.png and a subdirectory.

Now I want to delete header.png, footer.png and the subdirectory, but not images.

How can I do this in linux?

whiterose
  • 1,069
  • 1
  • 8
  • 4

14 Answers14

71

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

63

To delete hidden files, you have to specify:

rm -r images/* images/.*

With shells whose globs include . and .., this will lead to an error like

rm: cannot remove `.' directory `images/.'
rm: cannot remove `..' directory `images/..'

but it will delete hidden files.

An approach without errormessage is to use find/delete with mindepth. This is gnu-find.

find images -mindepth 1 -delete

Your find may lack the -mindepth or -delete predicate, in which case, you could do:

find images/. ! -name . -prune -exec rm -rf {} +
user unknown
  • 10,482
22

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

  • use ls -Ab to match all files/directories

    cd dir_name && rm -rf `ls -Ab`
    
  • use find to match all files/directories

    find dir_name -mindepth 1 -delete
    

    or, if your find doesn't support -delete:

    find dir_name -mindepth 1 -exec rm -rf {}
    
  • delete the folder, then recreate it

    rm -rf dir_name && mkdir dir_name
    
  • in bash,

    shopt -s dotglob  
    rm -rf dir_name/*
    
zeekvfu
  • 491
15

To delete all regular files recursively inside a directory, this command (assuming GNU or FreeBSD find) is good enough:

find . -type f -delete

That leaves all the non-regular files like symlinks (whether they point to regular files or not), directories, fifos, sockets, devices...

See also:

find . ! -type d -delete

to delete files of any type except directory.

6

Try this version:

 rm -r test/*
ceth
  • 225
4

The question was to empty a directory = remove contents of a directory including hidden files, but not the directory itself. The find command with -mindepth 1 is actually the correct way to go, but to avoid error messages it must be joined with -maxdepth 1:

find /path/to/dir -mindepth 1 -maxdepth 1 -exec rm -rf '{}' \;
Maddes
  • 141
3

I am using find command here:

Step 1: Find all the files and delete them :

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

Example:

find /home/user/Desktop/images/ -type f -exec rm {} \;

Step 2: Find all the sub-directories and delete them :

find /path/to/directory/ -type d -exec rm -R {} \;
  • find /path/to/directory/ -type d includes . so will delete the directory itself. You need to also use the -mindepth switch as others have said. – Arthur Tacca Jan 31 '17 at 11:48
1

Another option:

$ rm -rf /path/to/directory/{*,.*}

source: https://askubuntu.com/a/552834/56648

0

rm's syntax is:

rm [OPTION]... FILE...

So, you have to state the appropriate path explicitly, e.g.

rm -r sub_dir/
0

You can remove directory using following command:

sudo rm -r directoryname1/2/3/*

It will be delete entire directory after 3/*.

Example sudo rm -r Downloads/song/*

It will be delete all files which are within Downloads/song.

cuonglm
  • 153,898
0

This works perfectly for me, tested several variations worked every time.

From the directory that holds the images directory.

rm -frd ./images/*

before:

images/
    |_ header.png
    |_ footer.png
    |_ subdir/

after:

images/
techraf
  • 5,941
  • Not sure it was made clear in the original question, but just to point out that unless you enable dotglob, this will skip any "dot-file" or dot-directory under images (mkdir images/.somedir; touch images/.somefile) – Jeff Schaller May 16 '16 at 01:09
0

To delete all files and subdirectories in the current directory, including hidden ones, without error message:

rm -rf .[^.] .??* *

Or, from the parent directory:

rm -rf images/.[^.] images/.??* images/*

From https://serverfault.com/a/47940/269538.

this
  • 1
-2

Delete the folder "test" and all the files inside:

 rm -r test

Delete all the files inside but keep the folder "test":

 rm -r test/*
Celada
  • 44,132
Mike22LFC
  • 101
-4

cd into the folder then execute the following command:

ls |xargs rm -rf
sean
  • 1
  • 2
    This will fail in on filenames that contain whitespace. I would recommend against using the output of ls as input to other commands, it is generally unsafe. – dhag May 26 '15 at 00:04