Find lost file
Find lost files
Have you ever saved a file, maybe a download, then been unable to find it? Maybe you saved it in a different directory or with an unusual name.
The find command comes in handy here:
find ~ -type f -mtime 0
will show all files in your home directory modified or created today. By default, find counts days from midnight, so an age of zero means today.
You may have used the -name option with find before, but it can do lots more. These options can be combined, so if that elusive download was an MP3 file, you could narrow the search with:
find ~ -type f -mtime 0 -iname '*.mp3'
The quotation marks are needed to stop the shell trying to expand the wildcard, and -iname makes the match case-insensitive.
Incorrect permissions can cause obscure errors sometimes. You may, for example, have created a file in your home directory while working as root. To find files and directories that are not owned by you, use:
find ~ ! -user ${USER}
The shell sets the environment variable USER to the current username, and a ! reverses the result of the next test, so this command finds anything in the current user's directory that is not owned by that user. You can even have find fix the permissions
find ~ ! -user $USER -exec sudo chown ${USER}:"{}" \;
The find man page explains the use of -exec and many other possibilities.
0 comments:
Post a Comment