Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have folder names in the below date format


Similarly ,I have folders created from past two years for each day, and each of these folders have some files in it.

I am trying to find a unix command that would search for folders created on Saturdays and Sundays and delete all the contents in the folder and the folder itself.

For example, in the dates that I have mentioned above, folders with names 20130803 and 20130804 and the contents inside it should be deleted.

share|improve this question

migrated from Aug 21 '13 at 0:45

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

You can use the date function to identify the weekend days:

find . -path "./2013*" -printf "%f\n" | \
awk '{ \
cmd = sprintf( "date -d %s +\"%a\"", $0 ); \
cmd | getline value; close( cmd ); \
if( value ~ /Sat/ || value ~ /Sun/ ) print $0; \

where all of that is actually on one line could go on one line. I've used the "\" to make it more readable though. You could drop the "\" and make it all one line from the shell.

That will output the directory names that correspond to "Sat" or "Sun" ( locale specific ) from the find output. You could use the output to create a script to "rm -r" the directories.

You could also directly use find's date formatting "%Aa" (which is backed by the same strftime format parsing as date) if you know that all of the directories update dates match their names. I assumed that wasn't the case (because it's not for my test).

For example with the directories that I made to test:

find . -path "./2013*" -printf "%Aa\n"



which isn't as useful.

share|improve this answer

You can create a bash script (a.bash) to do that :


for i in $(\ls -1d 2013*); 
     if [ $(date -d "$i" +%a) == "Sun" ] || [ $(date -d "$i" +%a) == "Sat" ]; then 
          echo "$i is weekend. Delete the content";
          rm -rf "$i"/*; 

execute ./a.bash

share|improve this answer

Your Answer


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.