The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: delete compressed files from year 2005
deano6410
Newbie
Rank: 1



UID 110
Digest Posts 0
Credits 0
Posts 10
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:53 AM  Profile | P.M. 
delete compressed files from year 2005



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.


Top
goscript
Newbie
Rank: 1



UID 162
Digest Posts 0
Credits 0
Posts 27
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:53 AM  Profile | P.M. 
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"
Top
frankcow
Newbie
Rank: 1



UID 150
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:53 AM  Profile | P.M. 
that worked! thank you
Top
jmweb
Newbie
Rank: 1



UID 206
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:53 AM  Profile | P.M. 
how do i remove only those files tha i captured using that command?
Top
-jay-
Newbie
Rank: 1



UID 26
Digest Posts 0
Credits 0
Posts 47
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:54 AM  Profile | P.M. 
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.
Top
MaryMary
Newbie
Rank: 1



UID 252
Digest Posts 0
Credits 0
Posts 15
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 7, 2007 10:54 AM  Profile | P.M. 
thank you. this is a big help!!!
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:41 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005700 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP