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 have folder names in the below date format

20130801
20130802
20130803
20130804
20130805
20130806
20130807
20130808
.
.
.
20130819

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 stackoverflow.com Aug 21 '13 at 0:45

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

2 Answers 2

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"

yields

Tue
Tue
Tue

which isn't as useful.

share|improve this answer

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

#!/bin/bash

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

execute ./a.bash

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.