find /tmp -type f -mtime +180
I have this script get the list to clean up files older than 180 days under /tmp.
But, I want to make sure to grep only a type of files, which have only 6 character long.
....
LT3hqa dRMoya zZefaa
LTAeia dRahaa zZqbaa
LTIkMa dRqbaa zlQeEa
LTenMa dRunia zlqgEa
LTmlqa dSEeUa zoaaya
LV3nEa dSMb7a zpYhia
LV7caa dSYiqa zpqgUa
LVAp7a dSaaEa zvmpaa
LVYjUa dSacya zw3cMa
LVqbaa dSmnia zzAmaa
LVylqa dTAeaa
LWmbia dTEfMa
...
Please advise how to
find
files with 6-charactor long and 180 days old.
Subbeh
October 11, 2012, 10:37am
2
This should work:
find /tmp -type f -mtime +180 | grep '^.\{6\}$'
It returns nothing.
If I just run
find /tmp -type f -mtime +180
, I am getting these outputs.
....
/tmp/yZqcMa
/tmp/yZyiya
/tmp/y_Mdia
/tmp/y_Qcia
/tmp/y_Qcya
/tmp/y_ap7a
/tmp/y_mkia
/tmp/y_qfUa
/tmp/y_yc7a
/tmp/ypMe7a
/tmp/ypYk7a
/tmp/yxAi7a
....
Is it the reason??
Please advise.
Subbeh
October 11, 2012, 10:52am
4
Yes, just add 5 more characters to include /tmp/:
find /tmp -type f -mtime +180 | grep '^.\{11\}$'
pamu
October 11, 2012, 10:53am
5
try this..
find /tmp -type f -mtime +180 | awk -F/ 'length($NF) == 6'
It pulls out:
olympus:/> find /tmp -type f -mtime +180 | awk -F/ 'length($NF) == 6'
/tmp/.alist
/tmp/EFILE1
/tmp/acsisvc/f88c1119/.inuse
/tmp/acsisvc/f88c1119/sdscan
/tmp/diagSEgenSnap/general/errlog
/tmp/diagSEgenSnap/general/ike.db
/tmp/diagSEgenSnap/general/lsfilt
/tmp/diagSEgenSnap/general/trcfmt
/tmp/ecfile
/tmp/ibmsupt/general/trcfmt
/tmp/ibmsupt/tcpip/rc.net
/tmp/ibmsupt/tcpip/rc.qos
/tmp/prereq
.....
It should have pulled out:
/tmp/.alist
/tmp/EFILE1
/tmp/ecfile
/tmp/prereq
..
which only 6 character long followed by /tmp/
Please advise.
Try it the simple/stupid findless and egrepless way:
ls -a /tmp/ | grep '^......$'
It won't add /tmp/ to the names.
Scott
October 11, 2012, 11:36am
8
Why not just -name = "??????"
?
Because it will still be recursive and pull out all sorts of files he didn't want.
Scott
October 11, 2012, 11:41am
10
Yes, just noticed he didn't want that. Then just ls ??????
or maxdepth
:
find /tmp -maxdepth 1 -type f -mtime +180 -name '??????'
corona688:
Try it the simple/stupid findless and egrepless way:
ls -a /tmp/ | grep '^......$'
It won't add /tmp/ to the names.
olympus:/> ls -a /tmp/ | grep '^......$'
.alist
EFILE1
ecfile
prereq
tl_fix
It pulls out 6-character long file names, but somehow, it doesn't work with
olympus:/> find /tmp -type f -mtime +180 | grep '^......$'
This returns nothing while there are many of files older than 180 days.
pamu
October 11, 2012, 12:33pm
14
try with this....
find /tmp -type f -mtime +180 | grep '/tmp/......$'
Still nothing returns. I am on AIX 6.1
pamu
October 11, 2012, 1:28pm
16
try with this...
find /tmp -type f -mtime +180 | awk 'length($0) == 11'
Here many ways of doing this are described in this thread but any of these are not solving your purpose...
alister
October 11, 2012, 1:36pm
17
Since that find command is allowed to descend into subdirectories, there exists the possiblity that another directory named "tmp" could be visited. Because of this, your grep regular expression is underspecified. You must anchor it at the beginning as well.
If I just run
find /tmp -type f -mtime +180
, I am getting these outputs.
....
/tmp/yZqcMa
/tmp/yZyiya
/tmp/y_Mdia
/tmp/y_Qcia
/tmp/y_Qcya
/tmp/y_ap7a
/tmp/y_mkia
/tmp/y_qfUa
/tmp/y_yc7a
/tmp/ypMe7a
/tmp/ypYk7a
/tmp/yxAi7a
....
Based on that output, I don't see how pamu's suggestion could not have returned something.
Could there be whitespace after the filenames? If not, you must have made some mistake in typing the command. Perhaps a dot too many/few?
Here's yet another suggestion, a find approach that restricts itself to AIX 6.1 primaries:
find /tmp -type f -mtime +180 -name '??????' -print -o -type d -exec test {} \!= /tmp \; -prune
Regards,
Alister