### Common System Administration Patterns Source: https://context7.com/jamesyoungman/findutils/llms.txt Real-world examples combining find and xargs for tasks like log rotation, backup, permission auditing, and bulk renaming. ```bash find /var/log -name "*.log" -mtime +30 -delete find /project -mtime -7 -type f -print0 | xargs -0 tar -cvf backup.tar find /data -type f -printf "%s %p\n" | sort -n | uniq -d -w 15 find /src -name "*.py" -type f -print0 | xargs -0 wc -l | tail -1 find /usr -xtype l 2>/dev/null find / -perm -4000 -type f 2>/dev/null find /photos -name "*.jpg" | xargs -I {} mv {} {}.backup find /webroot -type d -exec chmod 755 {} \; -o -type f -exec chmod 644 {} \; ``` -------------------------------- ### Execute commands on found files with find -exec Source: https://context7.com/jamesyoungman/findutils/llms.txt Shows how to run commands against files matched by find. This includes per-file execution, batch processing for performance, and secure execution from the file's directory. ```bash find /tmp -name "*.tmp" -type f -exec rm {} \; find /project -name "*.o" -type f -exec rm {} + find /bin -type f -exec file '{}' \; find /shared -type d -exec chmod 755 {} \; find /shared -type f -exec chmod 644 {} \; find /src -name "*.c" -exec grep -l "main" {} \; find /important -name "*.bak" -ok rm {} \; find /data -name "*.csv" -exec cp {} /backup/ \; find /project -name "Makefile" -execdir make clean \; find /logs -name "*.log" -exec echo "Processing: {}" \; -exec gzip {} \; ``` -------------------------------- ### Advanced File Searching with find Source: https://context7.com/jamesyoungman/findutils/llms.txt Demonstrates complex file searching techniques including pruning directories, filtering by SCM status, and path exclusion. These commands are essential for optimizing search performance and targeting specific project structures. ```bash find /data -name ".*" -type d -prune -o -type f -print find /src \( -name .svn -o -name .git -o -name CVS \) -prune -o -type f -print find /repos \( -exec test -d '{}/.git' \; -or -exec test -d '{}/.svn' \; \) -print -prune find / -path /proc -prune -o -path /sys -prune -o -name "*.conf" -print ``` -------------------------------- ### Search for files using find Source: https://context7.com/jamesyoungman/findutils/llms.txt Demonstrates various file search criteria including name matching, type filtering, size constraints, and permission checks. These commands help locate specific files within directory hierarchies based on metadata and attributes. ```bash find /project -name "*.c" find /usr/include -iname "*.h" -type f find /var/log -mtime 0 -type f find /home -size +100M -type f find /tmp -type d -empty find /shared -perm -o=w -type f find /home -user alice -type f find /project -name "*.py" -type f -size +1k find /var -name "*.log" -o -name "*.txt" find /etc -type f ! -name "*.conf" find /src -newer /src/Makefile -type f find /data -type f -print0 find /home -maxdepth 2 -name ".bashrc" find /project -mindepth 1 -type d ``` -------------------------------- ### Search Code for Pattern and Show Context (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Searches C files in the /src directory for the pattern "TODO", lists the files containing the pattern, and then displays 2 lines before and 2 lines after each "TODO" occurrence with context. ```bash find /src -name "*.c" -print0 | xargs -0 grep -l "TODO" | xargs grep -B2 -A2 "TODO" ``` -------------------------------- ### Efficient Command Execution with xargs Source: https://context7.com/jamesyoungman/findutils/llms.txt Shows how to pipe find results into xargs for batch processing. Includes techniques for safe deletion, parallel execution, and argument limiting. ```bash find /tmp -name "*.tmp" -print0 | xargs -0 rm -f find /cache -type f -name "*.cache" -print0 | xargs -0 rm -f find /data -name "*.txt" | xargs -t wc -l find /images -name "*.jpg" | xargs -n 10 convert -resize 50% find /src -name "*.bak" | xargs -I {} mv {} /backup/ find /important -name "*.old" | xargs -p rm find /large -name "*.gz" -print0 | xargs -0 -P 4 gunzip xargs --show-limits < /dev/null cut -d: -f1 < /etc/passwd | sort | xargs echo find /maybe-empty -name "*.tmp" | xargs -r rm xargs -d '\n' echo < files.txt find /data -name "*.log" -print0 | xargs -0 -s 4096 cat ``` -------------------------------- ### Find Large Files and Display Sizes (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Searches the entire filesystem for files larger than 100MB, displays their size and path, sorts them numerically by size in descending order, and shows the top 20 largest files. Errors are redirected to /dev/null. ```bash find / -type f -size +100M -printf "%s %p\n" 2>/dev/null | sort -rn | head -20 ``` -------------------------------- ### Generate File Manifest with Checksums (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Generates a manifest file containing MD5 checksums for all files found in the /release directory. This is useful for verifying file integrity. ```bash find /release -type f -exec md5sum {} \; > manifest.md5 ``` -------------------------------- ### Monitor Filesystem Changes with Locate (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Monitors filesystem changes by comparing two database snapshots generated by `updatedb`. It first creates a database of files, waits for a period, creates another database, and then uses `diff` to show the differences between the two file lists. ```bash updatedb --output=/tmp/before.db # ... time passes ... updatedb --output=/tmp/after.db diff <(locate -d /tmp/before.db '') <(locate -d /tmp/after.db '') ``` -------------------------------- ### Fast File Lookups with locate Source: https://context7.com/jamesyoungman/findutils/llms.txt Utilizes the locate database for high-speed file searching. Covers case-insensitive matching, regex support, and database verification. ```bash locate readme locate -i README locate -b '\readme.md' locate -c "*.pdf" locate -l 10 "*.conf" locate -S locate -r '/usr/.*\.h$' locate -A project config locate -e "*.tmp" locate -0 "*.sh" | xargs -0 file locate -d /var/lib/mlocate/mlocate.db "*.c" locate --regextype posix-extended -r '\.c(pp)?$' locate -E "*.deleted" locate --max-database-age 1 pattern ``` -------------------------------- ### Exclude directories from search using -prune Source: https://context7.com/jamesyoungman/findutils/llms.txt Demonstrates the use of the -prune action to efficiently skip specific directories during a recursive search. ```bash find /project -name .git -prune -o -type f -print find /home -name .git -prune -o -name node_modules -prune -o -type f -name "*.js" -print ``` -------------------------------- ### Output-Generating Findutils Functions Source: https://github.com/jamesyoungman/findutils/blob/master/find/testsuite/excuses.txt This category includes functions that generate output files, making them challenging to test. These are primarily GNU extensions. ```C "fls", fls), "fprint", fprint), "fprint0", fprint0), "fprintf", fprintf), "fstype", fstype) ``` -------------------------------- ### Database Maintenance with updatedb Source: https://context7.com/jamesyoungman/findutils/llms.txt Configures and updates the locate database. Includes options for path exclusion, filesystem filtering, and custom output locations. ```bash sudo updatedb updatedb --localpaths='/home /data' --output=/tmp/mydb.db updatedb --prunepaths='/tmp /var/cache /proc /sys' updatedb --prunefs='nfs cifs tmpfs' updatedb --dbformat=slocate --output=/var/lib/slocate/slocate.db updatedb --findoptions='-ignore_readdir_race' updatedb --netpaths='/mnt/nfs /mnt/remote' sudo updatedb --localuser=nobody sudo updatedb --netuser=daemon --netpaths='/nfs/shared' #!/bin/sh updatedb --prunepaths='/tmp /var/tmp /usr/tmp /media /mnt' \ --prunefs='nfs NFS cifs smbfs proc sysfs' \ --output=/var/lib/mlocate/mlocate.db ``` -------------------------------- ### Potentially Testable Findutils Functions Source: https://github.com/jamesyoungman/findutils/blob/master/find/testsuite/excuses.txt This section lists functions that might be testable but require further analysis. It includes options for warning control and help/version parsing, with GNU extensions noted. ```C "nowarn", nowarn), "warn", warn), {ARG_TEST, "help", parse_help, NULL}, {ARG_TEST, "-help", parse_help, NULL}, {ARG_TEST, "version", parse_version, NULL}, {ARG_TEST, "-version", parse_version, NULL} ``` -------------------------------- ### Format file output with find -printf Source: https://context7.com/jamesyoungman/findutils/llms.txt Utilizes the -printf action to generate custom formatted output strings containing file metadata like size, permissions, timestamps, and paths. ```bash find /home -type f -printf "%p %s bytes\n" find /data -type f -printf "%f\t%k KB\t%t\n" find /etc -type f -printf "%M %u %g %p\n" find /var -type f -printf "%i %p\n" find /project -printf "%y %p\n" find /logs -type f -printf "%TY-%Tm-%Td %TH:%TM %p\n" find /src -printf "%d %p\n" find /shared -type f -printf "%10s %u %p\n" find /data -type f -printf "Access: %a\nModify: %t\nChange: %c\nFile: %p\n\n" ``` -------------------------------- ### Parallel Compression of Log Files (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Finds log files in the /logs directory that are older than 7 days and compresses them in parallel using gzip. It uses `find -print0` and `xargs -0` for safe handling of filenames. ```bash find /logs -name "*.log" -mtime +7 -print0 | xargs -0 -P 4 -I {} gzip {} ``` -------------------------------- ### Environment-Dependent Findutils Functions Source: https://github.com/jamesyoungman/findutils/blob/master/find/testsuite/excuses.txt Functions that rely on specific environmental features, such as user/group IDs or mount information, are listed here. Some are GNU extensions, while others are standard Unix features. ```C "gid", gid), "uid", uid), "user", user), "group", group), "mount", xdev), "nogroup", nogroup), "nouser", nouser), "ok", ok), "xdev", xdev), "ignore_readdir_race", ignore_race), "noignore_readdir_race", noignore_race), "noleaf", noleaf) ``` -------------------------------- ### Find Recently Accessed Files (Bash) Source: https://context7.com/jamesyoungman/findutils/llms.txt Finds files in the /home directory that were accessed within the last 24 hours, excluding files within any .cache directories. It prints the path of each found file. ```bash find /home -path '*/.cache' -prune -o -atime -1 -type f -print ``` -------------------------------- ### Time-Dependent Findutils Functions Source: https://github.com/jamesyoungman/findutils/blob/master/find/testsuite/excuses.txt These functions are related to file access and modification times and are considered hard to test due to their time-dependent nature. They are primarily used in GNU find utilities. ```C "amin", amin), "anewer", anewer), "atime", atime), "cmin", cmin), "cnewer", cnewer), "ctime", ctime), "daystart", daystart), "ls", ls), "mmin", mmin), "mtime", mtime), "newer", newer), "used", used) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.