If you are using an old shell, this approach might not work `rm' asks you whether to remove each file matching the given The simplest way to remove such files is: People are sometimes stymied by files whose names containĬharacters such as spaces, tabs, control characters, or characters with `find' can help you remove or rename a file with strange characters in This command is also likely to do something you did not intend:įind dirname -path dirname/foo -prune -o -deleteīecause `-delete' turns on `-depth', the `-prune' action has noĮffect and files in `dirname/foo' will be deleted too.
So the aboveĮxample will probably delete a lot of files the user didn't want to `-delete' also disables the default `-print' operation. The `-name quux' test will be performed forįiles we successfully deleted, but that test has no effect since Here, there is no action or test before the `-delete' so it Program will normally execute an action if the preceding action This is because `find'Įvaluates the items on the command line as an expression. Unpleasant surprise this command will attempt to delete everything at If the user hoped to delete only files named `quux' they will get an Lastly, an example of a program that almost certainly does not do It uses `-mindepth' toĪvoid removing `/tmp' itself if it becomes totally empty.Īrents become empty and can be removed too. Parents become empty and can be removed too. The second `find' command above cleans out empty directoriesĭepth-first (`-delete' implies `-depth' anyway), hoping that the Removing old files from `/tmp' is commonly done from `cron':įind /tmp /var/tmp -depth -not -type d -mtime +3 -deleteįind /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
The next example illustrates how to find and edit the list of files with a single invocation:įind /usr/include -name '*.h' | xargs grep -l mode_t | xargs sh -c 'emacs #'.įind ~ \( -name '*~' -o -name '#*#' \) -print0 | In the example above `xargs' will run `emacs' once for each file listed in the file `todo'. The example below illustrates a method of using xargs to direct input to an output file and then execute a command for each element in the output file:įind /usr/include -name '*.h' | xargs grep -l mode_t > todo Therefore, if there is a need to process a large number of files or items an alternate method is needed. It should be noted that the command line has a maximum length. c and containing the string "llo" would be displayed. name '*.c'| xargs grep -l llo `įor the command above, only the files ending in. To further refine the filtering process, the output of find could have been piped to xargs and filtered by grep as shown below: To restrict the program to only finding files on the current filesystem, use find -xdev to prevents the command from decending to directories on other file systems. The find command is enclosed in backquotes so that the shell will replace the enclosed programs(s) with their output. In this example, the viewing command less is utilized to view the list of file names output by the find command.
c that are under the current directory hierarchy. Using `-printf' and `sort' to compare timestampsīelow is an example using find to locate and view all files that end in. Updating A Timestamp File (find -newer -exec) Strange File Names (find -okdir -maxdepth -inum)Ī more secure version of `-exec' (find -exec)Ĭopying A Subset of Files (find -name -prune -o)
This page documents common tasks that utilize find and provides examples that use find to solve real problems.Įxamples Viewing And Editing (find -xdev) View the info pages for find for additional information and examples.
The find manual page documents the complete list of options, operators, actions and setting available for the find command. Find can compare or identify files based on name, location, type, size, creation and many more attributes. The find program searches for files that match the specified criteria and optionally executes some function on those files.