eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to UNIX for Web Developers
The "find" Utility  
Like the "grep" utility, the "find" utility is very useful for searching throughout UNIX. However, unlike grep, which looks for patterns within files, find looks for files that match a given pattern. The find utility is a little more complex though. Typically, you will use the following syntax:

find [location to start from] [options]

Consider the following example in which we simply look for all files starting in the current directory (".") that have ".bak" extension.

[Find]

Notice that we used the "-print" option to instruct find to display the list of files to standard output. Notice also that we used the -name option to specify a file type to look for. Finally, notice that find does a recursive search through the directory tree startin at the specified point.

There are quite a few possible options. just take a look at the following list of fairly useful ones:

Option Explanation
-atime num_days Displays the file if it was accessed (read or executed) within the given number of days
-ctime num_days Displays the file if it was changed (inode modified such as a change in permissions) within the given number of days
-exec command {}\; Executes the given command on each file found.
-group name Displays the file if it is owned by the given group
-mtime days Displays the file if it was modified (file was written to) within the given number of days
-name file_name Displays the file if it's name matches the regular expression
-newer file Displays the file if it is newer than the given file
-ok command Does the same thing as -exec but asks the user before executing the command. This is particularly useful if the command you are executing is "rm"
-perm octal Displays the file if its permissions match the given octal
-print Sends the list of matching files to standard output
-type flag Displays the file if it matches the given type (For example, d = directory and f=file)
-user name Displays the file if it is owned by the specified user.

Let's take a look at some of the more useful ways you can use find

The -exec option is one of the most useful of all the find options because it allows you to execute a command on each file found. Consider the following example in which we modify the permissions of all the files found. Note that as usual, this will be done recursively throughout the directory tree.

[Spacer]
[Find and Execute]

[Spacer] Alternatively, you can use the -ok option to make sure that UNIX prompts you before it executes each command. This is particularly useful for commands such as "rm".

[Spacer]
[Find and Execute with OK]

[Spacer] Similarly, you can find all files owned by a specific user or group using the -user and -group options. In the following example we demonstrate finding files by username.

[Spacer]
[Find by user]

A final cool usage of find is to incorporate it into another command such as "cp". For example in the following command, we copy all .bak files found by find, to the "Backup" directory

cp `find . -name '*.bak' -print` Backup

In this case, the use of back ticks (`) allows us to substitute the results of the command into the rest of the line.

[Spacer] In the next case, we find all files with the keyword "selena" in them.

[Grep]

Previous | Next | Table of Contents