I am using the program hazel to tidy my downloads folder every time a file is downloaded. I am now telling it to run a shell script, every week, to delete every folder in my downloads folder and then recreate it.

Is there an easier way to do this (deleting folders/recreating folders) by re-iterating through every folder in my Downloads folder and deleting its contents?

cd ~/Downloads;
rm -rf Archives;
rm -rf DMGs;
rm -rf Documents;
rm -rf Fonts;
rm -rf Mail\ Attachments;
rm -rf Mobile\ Provisioning;
rm -rf Music;
rm -rf Photoshop;
rm -rf Pictures;
rm -rf Videos;

mkdir Archives;
mkdir DMGs;
mkdir Documents;
mkdir Archives;
mkdir Fonts;
mkdir Mobile\ Provisioning;
mkdir Mail\ Attachments;
mkdir Music;
mkdir Photoshop;
mkdir Pictures;
mkdir Videos;
link|improve this question
What about recursively deleting the "Downloads" folder and creating it back? – Dor Jul 29 '11 at 15:26
note: ; is only needed if you have multiple commands in one line – yi_H Jul 29 '11 at 15:28
feedback

migrated from stackoverflow.com Jul 31 '11 at 1:16

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

5 Answers

up vote 6 down vote accepted

How about:

cd ~/Downloads && rm -rf */*

Or am I missing something?

Note: This will not delete hidden files/directories immediately under the top level.

link|improve this answer
Having deleted all of the folders, I want to re-create the folders with the same name – XcodeDev Jul 29 '11 at 15:26
Trust us, it won't delete the outer folders, just their contents. – yi_H Jul 29 '11 at 15:27
3  
The above does not delete the folders - it deletes the contents of the folders. – qbert220 Jul 29 '11 at 15:27
1  
This command is unsafe: if the Downloads folder is missing, it will delete everything in your home folder! When using cd like this, always either make the next action conditional (cd ~/Downloads && rm -rf */*) or set the exit-on-error flag (set -e) before cding. – Kevin Reid Jul 31 '11 at 23:24
@Kevin - good point. I've modified my answer – qbert220 Aug 1 '11 at 10:51
feedback

If you want to delete files while leaving the complete folder structure intact, use

find . -type f -print -exec rm '{}' ';'

If you only need the top level folder structure intact, rm -rf */* as others have suggested is good

link|improve this answer
2  
Find also includes the ability to only delete files that match certain criteria, so it's more flexible than rm. – Spencer Rathbun Jul 29 '11 at 17:56
feedback

It's actually quite simply, this will do it: rm -rf */*

link|improve this answer
Does that go through every-folder and delete its contents within the Downloads folder? – XcodeDev Jul 29 '11 at 15:27
yepp. 9moretogo:) – yi_H Jul 29 '11 at 15:28
feedback
cd /path/to/Downloads
for i in *; do 
    if [ -d "./$i" ]; then
        rm -rf "./$i"
        mkdir "$i"
    fi
done
link|improve this answer
This command is unsafe: if the Downloads folder is missing, it will delete everything in /path/to's subfolders! When using cd like this, always either make the next action conditional (cd ~/Downloads && for ...) or set the exit-on-error flag (set -e) before cding. – Kevin Reid Jul 31 '11 at 23:25
feedback

You could also run :

find ~/Downloads -type f -exec rm {} \;

This will delete all files in that directory.

Replace rm {} \; with ls -l {} \; before running for real to check if everything is okay before deleting the files.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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