Find: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 29: Line 29:
;<code>find <dir> -type f -inum <inode number> -delete</code>
;<code>find <dir> -type f -inum <inode number> -delete</code>
: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.
: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)

Revision as of 08:27, 2 October 2024

Find things in a directory tree

find <directory> -type f -name '<spec>'
Find file in and below <directory> that match <spec>. Type f is for files, d for directory's, no type finds all.
find <directory> -xdev -name <spec>'
Limit the search to the current file-system, to not go into mounted filesystems.
find <directory> -type f -name '<spec>' -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.
find <directory> -type f -name '<spec>' -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.
-mmin counts minutes instead of days.
find <directory> -type f -name '<spec>' -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)