delete compressed files from year 2005
Category: UNIX-Unix Beginner
delete compressed files from year 2005
i'm trying to delete files that were created/modified in the year 2005 that we compressed and have the .z extension on them. i tried using the awk utility but the syntax is incorrect. i don't know how to use a wildcard to capture all the compressed files. here's the code i used
( ls -lr | awk '$8 == "2005" && $9 == ".z" {print $0}' )
any suggestions on how to do this would be greatly appreciated.
quote:
originally posted by igidttam
i'm trying to delete files that were created/modified in the year 2005 that we compressed and have the .z extension on them. i tried using the awk utility but the syntax is incorrect. i don't know how to use a wildcard to capture all the compressed files. here's the code i used
( ls -lr | awk '$8 == "2005" && $9 == ".z" {print $0}' )
any suggestions on how to do this would be greatly appreciated.
thanks
try this -
ls -lr | awk '$8 == "2005" && $9 ~ /z$/ {print $0}'
this assumes you done have any "non-compressed" files that end with the character "z"
that worked! thank you
how do i remove only those files tha i captured using that command?
do i substitute rm -f for the print $0
it is never quite that easy, are there any subdirectories to worry about (your command for the ls included the -r option?
for that possibility, the best option would be
find ./ -type f -name '*.z' -exec ls -l {} \; | awk '$8 == 2005{print $nf}' | xargs rm -f
of course, you could use a system command in awk, but i prefer to do that outside the awk.
this assumes your system supports all these commands.
basically, this says - for all files in this directory and all subdirectories whose filename ends with a .z, do a long listing of it, see if the year is 2005 and if so print only the file name (including directory), and for those passing through the awk, do a "rm -f" on them.
technically, it is possible to add the mtime to the find to choose only those for 2005, but i agree that it just as easy to use the awk as this filter. however, using the right mtime parameters, it could all be done in one command (without pipes) by doing
find ./ -type f -name '*.z' -mtime +days -mtime -days -exec rm -f {} \;
but i am too lazy to calculate what the two days parameters would need to be.
thank you. this is a big help!!!
i appreciate it!
