Unix Find

From HaFrWiki
Jump to: navigation, search

One of the powerful UNIX commands is find. But there are a few people knowing every trick f it. Here some basic examples.

Examples dirs

Command example Description
find . -name 'my*' Current directory
Searches in the current directory (represented by a period) and below it, for files and directories with names starting with my.
find . -name "my*" -type f Files only
Limits the results of the above search to only regular files, therefore excluding directories, special files, pipes, symbolic links, etc.
find . -name "my*" -type f -ls Commands
Prints an extended file information.
The previous examples created listings of results because, by default, find executes the '-print' action.
find / -name "myfile" -type f -print Search all directories
Searches every file on the computer for a file with the name myfile. Can be time consuming.
find /home/brian -name "myfile" -type f -print Specify a directory
Searches for files named myfile in the /home/brian directory.
find local /tmp -name mydir -type d -print Search several directories
Searches for directories named mydir in the local subdirectory of the current working directory and the /tmp directory.
find / -name "myfile" -type f -print 2>/dev/null Ignore errors
Ignore permission denied (and any other) errors which are send to stderr.
find . ( -name "*jsp" -or -name "*java" ) -type f -ls Find any one of differently named files
The -ls option prints extended information, and the example finds any file whose name ends with either 'jsp' or 'java'. Note that the parentheses are required. Also note that the operator "or" can be abbreviated as "o". The "and" operator is assumed where no operator is given. In many shells the parentheses must be escaped with a backslash, "\(" and "\)", to prevent them from being interpreted as special shell characters. The -ls option and the -or operator are not available on all versions of find.
find . -name .svn -print0 | xargs -0 rm -rf Finds all subversion directories and deletes them
Finds all (sub)-directories with the name .svn and remove the found occurrences. The print0 produces input suitable for xargs -0.
find . -type f -print0 | xargs -0 grep "<text>" Finds all files from the current directory and searches for all occurrences of <text>.

Please note that this version is also working for filenames with spaces!

find . -path ./misc -prune -o -name '*.txt' -print Finds *.txt files in the current directory, excluding the misc directory just add a -path ./misc -prune -o to your find command.
find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print Finds all files excluding the dir1, dir2 and dir3 and prints the result
find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -ls Finds all files excluding the dir1, dir2 and dir3 and shows ls information

Examples Execute

Command example Description
find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 744 {} \; Execute an action
Changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3. The action is carried out by specifying the option -exec chmod 744 {} \; in the command. For every file whose name ends in .mp3, the command chmod 744 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command. Permission 744, usually shown as rwxr--r--, gives the file owner full permission to read, write, and execute the file, while other users have read-only access.
find . -name "rc.conf" -exec chmod o+r '{}' \; Searches 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).
find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print Searches 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.

Examples text

Command example Description
find /tmp -exec grep "search string" '{}' /dev/null \; -print Finding a string
Searches for a string in all files from the /tmp directory and below.
find /tmp -exec grep -H "search string" '{}' \; -print The /dev/null argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep.
grep -R /tmp "search string" GNU grep can be used on its own to perform this task.
find / -name '*' | xargs grep 'xx'
   or
find . -name “*.v” | xargs grep parity_check
Finds example of find [dir] -name [files] | xargs grep [text to search for]
find / -name 'CNV_*' 2>/dev/null | xargs grep -l test | xargs ls -al Same as above but the output gives more file attributes.
Finds the CNV_* files from the root (/) directory.
Searches for the test test and shows the output with all attributes.
All errors are sent to the garbage-bin.
find . -name 'settings.yml' -exec ls -al {} \; Finds the occurrences of the file settings.yml and shows details of the file by doing the command ls -al.
find . -iname 'Cream*.*' | tr \\n \\0 | xargs -0 ls -aL Finds all occurrences which may contains spaces, shows details of the files by doing the command ls -al.

Find & Remove

Unix command Description
find . -name .svn -exec ls {} \; Finds all subversion directories and list them
Finds all occurrences of the directory .svn and shows the results.
find . -name .svn -exec rm -rf {} \; Finds all subversion directories and deletes them
Removes all the occurrences of the directory .svn and removes the directories.
find . -name .svn -print0 | xargs -0 rm -rf Finds all subversion directories and deletes them
Finds all (sub)-directories with the name .svn and remove the found occurrences. The print0 produces input suitable for xargs -0.

See also

top