### find -exec example with /bin/rm Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Example command that finds a specific file and deletes it using /bin/rm. This command is vulnerable to a race condition where an attacker can replace the directory with a symbolic link, causing deletion of unintended files. ```shell find /tmp -path /tmp/umsp/passwd -exec /bin/rm ``` -------------------------------- ### Use -execdir with unstripped script to list stripped and unstripped binaries Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Example of using the unstripped script with -execdir to list stripped executables in sbins and unstripped ones in ubins. The -execdir action evaluates to the exit status of the executed program. ```shell find /usr/local -type f -perm +a=x \ \( -execdir unstripped '{}' \; -fprint ubins -o -fprint sbins \) ``` -------------------------------- ### Get findutils version: find --version Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Run this command to display the version number of the installed find program. The manual asks users to include this version number when reporting bugs or suggestions to bug-findutils@gnu.org. ```shell find --version ``` -------------------------------- ### find -print piped to xargs Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Example construction using -print with xargs. This approach is insecure due to a time gap and does not cope correctly with newlines or whitespace in filenames, making it less secure than -print0. ```shell find .... -print | xargs .... ``` -------------------------------- ### Edit files matching criteria with emacs Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Similar to the viewing example, but opens the files in an editor. The command lists files in /usr/include that contain mode_t and opens them with emacs. ```shell emacs `find /usr/include -name '*.h' | xargs grep -l mode_t` ``` -------------------------------- ### Edit files with xargs --arg-file to avoid command line length limits Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Because command lines have a length limit, this example writes the file list to a file and then uses xargs --arg-file to run emacs as many times as necessary to visit all files listed in the todo file. ```shell find /usr/include -name '*.h' | xargs grep -l mode_t > todo xargs --arg-file=todo emacs ``` -------------------------------- ### Archive files with cpio using null-delimited names Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Use find with -depth and -print0 to produce a null-delimited list of files, then pipe to cpio --create --null --format=crc to create an archive. The -depth option ensures directory permissions are restored after contents. ```shell find . -depth -print0 | cpio --create --null --format=crc --file=/dev/nrst0 ``` -------------------------------- ### locate command syntax Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Shows the general command-line syntax for invoking locate with optional options and patterns. ```bash locate [option...] pattern... ``` -------------------------------- ### Multiple changes with comma-separated modes a+r,go-w Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Specify more than one simple symbolic mode separated by commas. a+r,go-w gives everyone read permission and removes write permission for all users except the owner. ```text a+r,go-w ``` -------------------------------- ### Copy group permissions to others (o+g) Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Adds the permissions for users in the file's group to the permissions that other users have for the file. For example, mode 664 becomes 666, and mode 741 becomes 745. ```text o+g ``` -------------------------------- ### Remove CVS backup files with xargs Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Remove CVS backup files (names starting with .#) using find -print0 and xargs -0r rm -f. The -r option prevents errors if no files match. ```shell find . -name '.#*' -print0 | xargs -0r rm -f ``` -------------------------------- ### Archive and restore with tar using null-delimited names Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Use find with -depth and -print0 to pipe a null-delimited file list to tar --create --null --files-from=-. The restore command uses tar --extract --null --preserve-perm --same-owner. ```shell find . -depth -print0 | tar --create --null --files-from=- --file=/dev/nrst0 tar --extract --null --preserve-perm --same-owner \ --file=/dev/nrst0 ``` -------------------------------- ### find command line syntax Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Shows the general command line syntax for find. The -H, -L, or -P options may precede the file list; if none is given, -P is assumed. If no paths are given, the current directory is used, and if no expression is given, -print is used. ```shell find [-H] [-L] [-P] [file...] [expression] ``` -------------------------------- ### updatedb command syntax Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Shows the basic command-line syntax for invoking updatedb. ```plaintext updatedb [option...] ``` -------------------------------- ### Equivalent modes og+rX and og-w Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html The mode og+rX-w is equivalent to the two modes og+rX and og-w. ```text og+rX og-w ``` -------------------------------- ### find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Refined method using -print0 and xargs -0 to avoid misinterpreting filenames with spaces, and -r to avoid running grep if find produces no output. ```shell find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing ``` -------------------------------- ### Quit after first match with -quit Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Exits immediately after printing the first path, so only /tmp/foo is printed even though /tmp/bar is also specified. ```shell find /tmp/foo /tmp/bar -print -quit ``` -------------------------------- ### Alternative equivalent mode u+r,g+rx,o+r,g-w Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Another way to write the mode a+r,g+x-w. It gives all users read permission, group members execute permission, and removes write permission for group members. ```text u+r,g+rx,o+r,g-w ``` -------------------------------- ### Combined methods with a+r,g+x-w Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Combines multiple operation parts and comma-separated modes. a+r,g+x-w gives all users read permission, and gives group members execute permission but not write permission. ```text a+r,g+x-w ``` -------------------------------- ### find: list files read 2 to 6 minutes ago Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Lists files in /u/bill last read from 2 to 6 minutes ago using -amin with a range. The plus and minus signs set lower and upper bounds on the age in minutes. ```shell find /u/bill -amin +2 -amin -6 ``` -------------------------------- ### Set read and write for all users (a=rw) Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Gives everyone permission to read and write a file, but not to execute it. ```text a=rw ``` -------------------------------- ### xargs command syntax Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Shows the general syntax for invoking xargs. It reads arguments from standard input, delimited by blanks or newlines, and executes the command (default /bin/echo) with initial-arguments followed by the read arguments. ```shell xargs [option...] [command [initial-arguments]] ``` -------------------------------- ### find: list regular files modified yesterday with -daystart Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Measures times from the beginning of today rather than from 24 hours ago. Lists regular files in the home directory that were modified yesterday. ```shell find ~ -daystart -type f -mtime 1 ``` -------------------------------- ### View files matching criteria with less Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Use backquotes to substitute the output of a find and xargs pipeline as arguments to a file viewing program. The command lists files in /usr/include that contain mode_t and opens them with less. ```shell less `find /usr/include -name '*.h' | xargs grep -l mode_t` ``` -------------------------------- ### Multiple changes with og+rX-w Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Specify multiple operation and permissions parts after a users part. og+rX-w gives users other than the owner read permission and conditional execute permission, and denies write permission. It does not affect the owner's permissions. ```text og+rX-w ``` -------------------------------- ### Set all non-special permissions with u=rwx,g=rx,o= Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Sets all non-special permissions explicitly. u=rwx,g=rx,o= gives users not in the file's group no permission at all. ```text u=rwx,g=rx,o= ``` -------------------------------- ### Save program on swap device with o+t Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Use o+t to cause a program to be saved on the swap device (sticky bit). ```text o+t ``` -------------------------------- ### grep -l thing *.[ch] Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Search current directory for C source files containing the string 'thing'. ```shell grep -l thing *.[ch] ``` -------------------------------- ### Remove all permissions for group and others (go=) Source: http://gnu.ist.utl.pt/software/findutils/manual/html_mono/find.html Gives everyone except the file's owner no permission to do anything with the file. Other users could still remove the file if they have write permission on the directory. ```text go= ```