Skip to main content

Some examples of using UNIX find command

Some examples of using UNIX find command.

Contents:

Introduction
Search for file with a specific name in a set of files (-name)
How to apply a unix command to a set of file (-exec).
How to apply a complex selection of files (-o and -a).
How to search for a string in a selection of files (-exec grep ...).
Introduction

The find command allows the Unix user to process a set of files and/or directories in a file subtree.

You can specify the following:

where to search (pathname)
what type of file to search for (-type: directories, data files, links)
how to process the files (-exec: run a process against a selected file)
the name of the file(s) (-name)
perform logical operations on selections (-o and -a)
Search for file with a specific name in a set of files (-name)

find . -name "rc.conf" -print
This command will search in the current directory and all sub directories for a file named rc.conf.

Note: The -print option will print out the path of any file that is found with that name. In general -print wil print out the path of any file that meets the find criteria.

How to apply a unix command to a set of file (-exec).

find . -name "rc.conf" -exec chmod o+r '{}' \;
This command will search in the current directory and all sub directories. All files named rc.conf will be processed by the chmod -o+r command. The argument '{}' inserts each found file into the chmod command line. The \; argument indicates the exec command line has ended.

The end results of this command is all rc.conf files have the other permissions set to read access (if the operator is the owner of the file).


How to apply a complex selection of files (-o and -a).

find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:

-not means the negation of the expression that follows
\( means the start of a complex expression.
\) means the end of a complex expression.
-o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'
The above example is shows how to select all file that are not part of the RCS system. This is important when you want go through a source tree and modify all the source files... but ... you don't want to affect the RCS version control files.


How to search for a string in a selection of files (-exec grep ...).

find . -exec grep "www.athabasca" '{}' \; -print
This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "www.athabasca" '{}' \; -print
This command is very important for process a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string "www.athabascau.ca". You can then process the files with a sed script to change those occurrances of "www.athabascau.ca" with "intra.athabascau.ca".

Source: http://www.athabascau.ca/html/depts/compserv/webunit/HOWTO/find.htm

Comments

Popular posts from this blog

Cisco Command "Auto secure"

Cisco Command "Auto secure" In today's article, I'm going to quickly inform you about the Privileged EXEC command named "auto secure". Network administrators (like you) use the "auto secure" command to secure the management and forwarding planes of a router. Another way of saying it is, CCNAs use this command to secure a router by disabling common IP services which can be exploited by attackers to initiate network attacks. When the command is typed on a router, it takes the user (ccna) through a command line-interface (CLI) semi-interactive session (which is also known as the AutoSecure dialogue). Below is the command's syntax: auto secure [management | forwarding] [no-interact | full] [ntp | login | ssh | firewall | tcp-intercept] As you can see, the command can use several "optional" keywords: management - This (optional) keyword is used to only secure the management plane of a router. forwarding - This (optional) keyword is used to
How to install kismet on MacBook, Mac OS X 10.5.6 Leopard How to installed and run kismet. cd /tmp svn co https://www.kismetwireless.net/code/svn/trunk kismet-devel cd kismet-devel ./configure sudo make install Edit kismet config file like: sudo vim /opt/local/etc/kismet.conf and change the following two lines: a.) suiduser=Your_Username_For_Mac b.) source=darwin,en1,airport_extreme Run kismet and enjoy! sudo kismet

What's the difference between 802.11a, 802.11b, and 802.11g?

What's the difference between 802.11a, 802.11b, and 802.11g? The first difference is that 802.11a uses the 5GHz radio frequency (RF) spectrum while 802.11g and 802.11b share the 2.4GHz spectrum. Therefore 802.11a will not interfere with 802.11g or 802.11b. Since 802.11a uses the 5GHz spectrum, it will have less distance capabilities due the physical nature of electromagnetic radiation. 802.11b and 802.11g use an RF spectrum that allows for 3 non-interfering channels (in North America). 802.11a uses a wider spectrum that allows for 12 non-interfering channels which will be 23 non-interfering channels with soon to be released standard. The idea of non-interfering (or non-overlapping) channels allows more wireless users to be concentrated in an area without having to share the transmission medium. 802.11b, while sharing the same 2.4GHz frequency spectrum as 802.11g, has lower transmission rates and different modulation techniques than 802.11g. For a discussion of transmission ra