Unix: Finding your files your way

It's easy to find files on Unix systems. You can find them by almost any criteria that you can imagine. The bigger challenge is deciding how to describe what you're looking for and deciding what to do with the files once you find them.

By  

What do you best remember or most know about files when you need to find them and should they be removed, chmod'ed, compressed or edited? The find command gives you more options than most of us will ever use and, combined with exec and xargs commands, can become some of your favorite command line powerhouses.

The most obvious option is to search for files by name. You can search for files by their full names or using some portion of the names. If you use .pl extensions on your perl scripts, for example, you can locate all your perl scripts with a command like this:

$ find . -name "*.pl" -ls

If you don't use file extensions, you can serach for scripts by looking for the related shebang line:

$ find . -type f -exec grep "/usr/bin/perl" {} \; -ls

This command will show the shebang line and then the name of the file, but it works

#!/usr/bin/perl -w
12731688    4 -rw-------   1 me       admins       2440 Apr 22  2012 ./chkPass
#!/usr/bin/perl
12763143    4 -rwx------   1 me       admins        374 Jul 13 10:58 ./bin/doit

Another common way to look for files is to search for them by age. Maybe you were working on a script last month and just can't remember what you called it. If you know you created or updated the file within the last couple of weeks, however, you can look for files that have been modified within that time frame. This command will list files modified within the last two weeks.

$ find . -mtime -14 -ls

You can also look for files that have been created or modified since some particular data and time, but this requires an extra command. I like to use commands like these. First, I create a file with a specific time stamp -- 00:00 on Nov 1st (i.e., midnight on Oct 31st) -- and then I look for files that are newer than it.

$ touch -t 11010000 Nov
$ find . -newer Nov -ls

Using touch, you can create a file with any date and time you need. The syntax for touch -t is:

touch -t [[CC]YY]MMDDhhmm[.ss] filename

The CCYY part (i.e., the year) will default to the current year, of course, so spell out 11 or 2011 if you want to begin your search some earlier year -- but be prepared for a possible avalanche of output. A lot of files are added in the span of a year+ and you might
just end up refining your search several times before you get something that works for you.

You can also use the -newer argument to look for files that are newer than some other file that you expect was created or modified around the same time as the one you happen to be looking for.

Want to find files that were modified today? Use find's -mtime option but add -daystart so that it doesn't start its search with yesterday.

$ find .
Photo Credit: 

flickr / Key Foster

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness