Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 4 '11 at 11:26

This question came from our site for professional and enthusiast programmers.

    
cd into the directory and do an rm -Rf of all the files and directories in there. –  Noufal Ibrahim May 4 '11 at 7:39

9 Answers 9

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.

share|improve this answer
5  
hidden file will not be deleted. –  reto Aug 19 '13 at 17:58

To delete hidden files, you have to specify:

rm -r images/* images/.*

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 a -delete switch.

share|improve this answer
    
NEVER RUN: rm -r .* as this will remove everything on some systems. .* matches .. which will start traversing your file system upwards. If you want to delete the '.' files you should be running rm -r .[!.]* –  DarkHeart May 26 at 0:28
    
My suggestion was to use 'rm -r images/.*' which would match 'images/..' but not 'images/../..'. The recursion can't go upwards, since 'images/../images/..' is again the same directory. –  user unknown May 26 at 4:39
    
@DarkHeart: With the "images/" in front of the ".*"? Can you provide a concrete example? –  user unknown 7 hours ago
    
Sorry, I may of been mistaken. I cannot replicate it on any current system. –  DarkHeart 5 hours ago

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`

  • ues find to match all files/directories

    find dir_name -mindepth 1 -delete

  • delete the folder, then recreate it

    rm -rf dir_name && mkdir dir_name

  • in Bash,

    shopt -s dotglob
    rm -rf dir_name/*

share|improve this answer

Try this version:

 rm -r test/*
share|improve this answer
    
Thanks demas... Its working........ –  White rose May 4 '11 at 8:37
    
This won't remove hidden files (ones starting with "."). –  gbmhunter Mar 4 at 22:59

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 {} \;
share|improve this answer

rm's syntax is:

rm [OPTION]... FILE...

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

rm -r sub_dir/
share|improve this answer

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.

share|improve this answer

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/*
share|improve this answer
1  
It looks like you have this backwards. –  HalosGhost Feb 26 at 0:12

cd into the folder then execute the following command:

ls |xargs rm -rf
share|improve this answer
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 at 0:04

Your Answer

 
discard

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