Problem Statement
Explain the find command in Linux. How do you search for files by name, size, modification time, and permissions?
Explanation
The find command searches for files and directories based on various criteria. Basic syntax is find [path] [options] [tests] [actions]. For example, find /home -name "*.txt" searches for .txt files in /home. Use -iname for case-insensitive name search. The -name option uses shell patterns (* for any characters, ? for single character).
Search by size with -size: find / -size +100M finds files larger than 100MB. Use +n for greater than, -n for less than, and n for exactly. Units include c (bytes), k (kilobytes), M (megabytes), G (gigabytes). Find by time with -mtime (modified), -atime (accessed), or -ctime (changed): find /var/log -mtime -7 finds files modified in last 7 days.
Search by permissions with -perm: find /home -perm 777 finds exactly 777 permissions, find /home -perm -644 finds files with at least 644 permissions. Search by type with -type: f (file), d (directory), l (symlink), b (block device), c (character device). Combine criteria with -and (implicit), -or, and -not.
Execute actions on found files with -exec: find /tmp -type f -mtime +30 -exec rm {} \; deletes files older than 30 days. Use -delete for simpler deletion. The {} placeholder represents found files, and \; terminates the command. Use -print (default) to display results. Understanding find is essential for system administration, cleanup tasks, and file management.
