Delete all the files and subdirectories for the year 2006

Hi

I have lot of files and subdirectories inside a directory which are created in the years 2006, 2007, 2008, 2009 and 2010.

I want to delete all the files and subdirectories belonging to the year 2006 alone.

How can I do that ?

find directory -type f -ls | awk '($10=="2006"){print $11}' | xargs -n10 rm
1 Like

If these files are not modified in 2006.

find directory -type f -mtime +1200 -exec rm {} \;

You can replace 1200 to any day.

1 Like
touch -t 200512312359.59 oldest
touch -t 200701010000.00 newest
find /dir -newer oldest \! -newer newest -exec rm -f {} \;
1 Like

hi

Thanks for your reply.

Is there a way to find how many files and subdirectories are present currently inside the directory with the year stamp as 2006 ?

Not sure about what you mean by "inside the directory with the year stamp as 2006"

Will count the number of files stamped as 2006 inside directory (no matter how deep they can be in subdir, if you don't want to go in subdir, just add the -prune option to the find command)

find directory -type f -ls | awk '($10=="2006"){print $11}' | wc -l

will count the number of subdirectories stamped as 2006 inside directory (no matter how deep they can be in the directory tree, if you don't want to go in subdir, just add the -prune option to the find command)

find directory -type d -ls | awk '($10=="2006"){print $11}' | wc -l