Anyone with Windows experience who is
new to UNIX/Linux/CygWin pretty quickly figures out that the LS command
in UNIX is the equivalent of the DIR command in Windows. At least, it
is equivalent up to a point. Issuing the DIR command in Windows (with
no arguments) will display the contents of the current folder. Issuing
the LS command in UNIX with no arguments will do the same. After that,
however, the similarities end. Say, for example, you’re searching for a
file called “debug.log” that you know to be in a subfolder, or
sub-subfolder, of the current directory. In Windows, the command “DIR
/S debug.log” will do the job nicely. The “/S” switch tells the DIR
command to recurse through every subdirectory, while the “debug.log”
argument tells the DIR command what file(s) to look for.
In UNIX, however, the tool for that job is the FIND command, not the LS command. Specifically,
in this case, it would be “find . -name debug.log”. The first argument
after the word find — the lone period — tells the FIND command where to
start looking. (As with Windows, a single period means the current
directory.) The “-name” switch, together with its argument of
“debug.log”, establishes a criterion for the name of the files to be
found. There are dozens of other criteria available, which can be used
separately or in combination. These criteria, or “tests”, include being
able to test on the age of the file, the type of file, the size of the
file, and attribute flags of the file. Other options available with the
FIND command include actions that can be taken against the found files,
such as deleting the file(s) found, or executing an arbitrary command
and passing it the name of each file found.
Tip #1: Use “MAN FIND” to read all about the find command. Be sure to scroll all the way to the bottom to see a number of examples.
Tip #2: If you have trouble coming up with a
combination of criteria to do what you expect, then take advantage of
the -D switch to turn on one or more of the debug options. This gets
the FIND command to “show its work” so you can see what it’s thinking,
and how it’s interpreting the switches.
Tip #3: As you can see, the UNIX FIND command is
significantly more powerful than the Windows DIR command. CygWin, the
Linux emulator for Windows, allows the FIND command to be used in
Windows — to search Windows drives. (See our previous articles for how
to get started with CygWin.)