Find: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
Find things in a directory tree | Find things in a directory tree | ||
;<code>find <directory> -type f -name '< | ;<code>find <directory> -type f -name '<pattern>' </code> | ||
:Find | :Find files in and below <directory> that match <pattern>. Type f is for files, d for directory's, no type finds all. | ||
;<code>find <directory | ;<code>find -L <directory>' </code> | ||
: | :Do follow symbolic links, default behavior is not to follow. | ||
;<code>find <directory> -type f -name '< | ;<code>find <directory> -xdev -name <pattern>' </code> | ||
:Limit the search to the current file-system, do not go into mounted filesystems. | |||
;<code>find <directory> -type f -name '<pattern>' -execdir <command> {} \;</code> | |||
:For each file found execute <command> in the directory containing the file. {} represents the found file. NOTE!! Do not forget the \; at the end. | :For each file found execute <command> in the directory containing the file. {} represents the found file. NOTE!! Do not forget the \; at the end. | ||
: -exec works too but is less secure as mistakes main lead to execution in an unexpected place. | : -exec works too but is less secure as mistakes main lead to execution in an unexpected place. | ||
: If more commands need to be executed specify -exec <command> {} \; for each command. | |||
;<code>find <directory> -type f | ;<code>find <directory> -type f -mtime 1 </code> | ||
:Find files modified 1 day ago (between 24 and 48 hours). | :Find files modified 1 day ago (between 24 and 48 hours). | ||
:Negative numbers find all files modified max 1 day ago. | :Negative numbers find all files modified max 1 day ago. | ||
| Line 18: | Line 22: | ||
:<code>-mmin</code> counts minutes instead of days. | :<code>-mmin</code> counts minutes instead of days. | ||
;<code>find <directory> -type f - | ;<code>find <directory> -type f -newer <filename></code> | ||
:Find files modified after the modification time of <filename> | |||
:You can set the modification time of <filename> like <code>touch -t YYYYmmddHHMM <filename></code> | |||
;<code>find <directory> -type f -printf '%T@ %p\n'</code> | |||
:Print the files modification times in [https://www.epochconverter.com/ epoch] (seconds since Jan 1, 1970) and the filename. Can be used e.g. to find the last files modified (with sort -n and tail) | :Print the files modification times in [https://www.epochconverter.com/ epoch] (seconds since Jan 1, 1970) and the filename. Can be used e.g. to find the last files modified (with sort -n and tail) | ||
| Line 35: | Line 43: | ||
-group (group name) | -group (group name) | ||
-gid (group id) | -gid (group id) | ||
;<code>find <dir> -depth -type d -empty -delete</code> | |||
:Find empty directories and delete them | |||
Latest revision as of 13:10, 13 January 2026
Find things in a directory tree
find <directory> -type f -name '<pattern>'- Find files in and below <directory> that match <pattern>. Type f is for files, d for directory's, no type finds all.
find -L <directory>'- Do follow symbolic links, default behavior is not to follow.
find <directory> -xdev -name <pattern>'- Limit the search to the current file-system, do not go into mounted filesystems.
find <directory> -type f -name '<pattern>' -execdir <command> {} \;- For each file found execute <command> in the directory containing the file. {} represents the found file. NOTE!! Do not forget the \; at the end.
- -exec works too but is less secure as mistakes main lead to execution in an unexpected place.
- If more commands need to be executed specify -exec <command> {} \; for each command.
find <directory> -type f -mtime 1- Find files modified 1 day ago (between 24 and 48 hours).
- Negative numbers find all files modified max 1 day ago.
- Positive numbers (explicitly signed with +) find all files at modified at least 1 day ago.
-mmincounts minutes instead of days.
find <directory> -type f -newer <filename>- Find files modified after the modification time of <filename>
- You can set the modification time of <filename> like
touch -t YYYYmmddHHMM <filename>
find <directory> -type f -printf '%T@ %p\n'- Print the files modification times in epoch (seconds since Jan 1, 1970) and the filename. Can be used e.g. to find the last files modified (with sort -n and tail)
find <dir> -printf '%s %p\n'| sort -nr | head -10- Find the top 10 largest files under a directory
find <dir> -type f -mtime +30 -ls|awk 'BEGIN {} {totsize+=$7} END { print totsize/1000000}'- Find the total size of all files older than 30 days.
find <dir> -type f -inum <inode number> -delete- Find file by inode number and delete it. This can be used to removed files with weird names. This does not delete the inode, if files have more hardlinks all must be deleted.
Other options to select specific files are (among many others):
-user (username) -uid (user id) -group (group name) -gid (group id)
find <dir> -depth -type d -empty -delete- Find empty directories and delete them