Unix find command tips

actually there are many things can be done by using find command in unix/linux.

in example, we want to find something inside /test folder we use

find /test -name *something*

-name option is the key word to find something with the name.

we also can find something by time,

find /test -mtime -1

the -mtime option is use to detect file modified date, -1 mean older than, +1 mean later than. you can put any day, but please use it carefully, because +1 not mean 1 days, it mean 48 hours, 0 was include. keep testing to get best result.

the find command cannot find file which create by specific date or hour. so we use trick to make it find file like created last 2 hours.

first we need to create a file with certain time stamp by using touch command

touch -t YYMMDDHHMM filename 

let say we want to find files that create last 3 hours. the date time now is 201711160900, so we touch a timer file with time stamp 201711160600.

find /test -newer timer

by comparing file time stamp, it will search the file created in last 3 hours.

to reverse it just put a ! infront the option like
find .  ! -name *something* ! -newer timer

find . mean find current folder.

we also can execute some command from the file result.

find /source -mtime +10 -exec mv {} ./target \;

use -exec option to execute command on the return result. \; is a must to close the command

find /source -mtime +10 -exec rm {} \;

here some useful tip for find command, there are more, please check in find manual. the command was tested in solaris, might not able to work on other platform, but it almost similar.

Comments