Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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;
share|improve this question

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

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

    
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

4 Answers 4

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.

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

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

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

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

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

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.

share|improve this answer

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.