Linux Sysadmin Test Prep
Finding Files
Finding Files
find
In folder: find ./test -name "filename.txt"
With wildcard: find ./test -name "*.txt"
Ignore case: find ./test -iname "*.txt"
Recursion level: find ./test -maxdepth 2 -name "*.txt"
Invert: find ./test -not -name "*.txt"
Invert: find ./test ! -name "*.txt"
Combine: find ./test -name 'filename*' ! -name '*.txt'
OR: find -name '*.txt' -o -name '*.txt'
Only files: find ./test -type f -name "filename*"
Only directories: find ./test -type d -name "filename*"
Multiple directories: find ./test ./dir2 -type f -name "filename*"
Hidden files: find ~ -type f -name ".*"
Permissions: find . -type f -perm 0664
sgid/suid nubers: find / -perm 2644
sgid/suid symbols: find / -maxdepth 2 -perm /u=s
User read permissisions: find /etc -maxdepth 1 -perm /u=r
Executable files: find /bin -maxdepth 2 -perm /a=x
By user: find . -user username -name '*.txt'
By group: find /var/www -group groupname
In home: find ~ -name "*.txt"
All 50 mebibyte files: find / -size 50M
Unit | Meaning |
---|---|
b | for 512-byte blocks (this is the default if no suffix is used) |
c | for bytes |
w | for two-byte words |
k | for kibibytes (KiB, units of 1024 bytes) |
M | for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes) |
G | for gibibytes (GiB, units of 1024 1024 1024 = 1073741824 bytes) |
greater than 50 mebibyte and less than 100MB: find / -size +50M -size -100M
List the largest file in the current directory and its subdirectory: find . -type f -exec ls -s {} \; | sort -n -r | head -1
List the 5 smallest file in the current directory and its subdirectory: find . -type f -exec ls -s {} \; | sort -n | head -5
Find all files that are empty: find /tmp -type f -empty
All empty directories: find ~/ -type d -empty
List finds as ls: find . -exec ls -ld {} \;
Remove all text files in the tmp directory: find /tmp -type f -name "*.txt" -exec rm -f {} \;
Delete files larger than 100MB: find /home/username/dir -type f -name *.log -size +10M -exec
rm -f \;
Delete empty directories: find <DIR> -type d -empty -delete
Find .tar files, and write it's contents one item per line, with relative paths: cd <DIR> && find . -name '*.tar' > toBeCompressed.txt
Delete all executable files: find <DIR> -type f -executable -delete
Change 777 files to 644: find <DIR> -type f -perm 777 -print -exec chmod 644 {} \;
Change read only directories to 755: find <DIR> -type d -perm 444 -print -exec chmod 755 {} \;
Change ownership of all group developer files and directories to group and user admin: find <DIR> -group developer -print -exec chown admin:admin {} \;
Accessed (read), Modified (data), and Changed (metadata like chmod)
Modified 50 days ago: find / -mtime 50
Accessed in the last 50 days: find / -atime -50
Modified between 50 to 100 days ago: find / -mtime +50 –mtime -100
Changed within the last 1 hour: find /home/username -cmin -60
Modified in last 1 hour: find / -mmin -60
Accessed Files in Last 1 Hour: find / -amin -60
quiz
- Execute
quiz find