find [path][options][tests][actioins]
Grand, we can now find files based on a subset of criteria. What would be even better is to apply some actions on those files. Action can be done with the use of -exec switch.
We can now find .avi file that are newer that 15 days, in this example, we are going to move those file to another location: /my/new/movies . I consider that this directory already exist on your system.
Moving .avi files bigger than 700M and younger than 15 days to /my/new/movies can be done with:
ex:- find /home/ -name '*.avi' -a -size +700M -mtime -15 -exec mv '{}' /my/new/movies/ \;
ex:- find /home -name '*.pl' -ls
ex:- search in root directory downwards all files which have exactly 2 links
find / -links 2 -print
ex:- search in root directory downwards all files which have less than 2 links
find / -links -2 -print
ex:- search in root directory downwards all files which have more than 2 links
find / -links +2 -print
ex:-
search all the directories from / downwards for files whose inode number is 4014896 and print them
find / -inum 4014896 -print
ex:- search in root directory and downwards all files which have permission 777
find / -perm 777 -print
ex:- search in root directory and downwards all files whose owner is guest and group is guest
find / \( -user guest -a -group guest \) -print
ex:- search in root directory and downwards all files whose owner is guest or whose name is xyz
find / \( -user guest -o -name xyz \) -print
search in current directory downwards all files whose size is 10 bytes
find . -size 10c -print
Suffix Meaning
b 512-byte blocks (the default)
c Bytes
k Kilobytes (KB)
M Megabytes (MB)
G Gigabytes (GB)
Ex: - search in current directory downwards all files which were accessed exactly 7 days back
find . -atime 7 -print
Mind the use of '{}' and \; (there is a space before \;).
'{}' matches the file that was found, while \; terminate the exec statement.
Ex:
find the files on the current folder owned by scott? Use find with the -user option, followed by the user name (or the user number, which you can find in /etc/passwd):
$ find . -user scott
find -type
One of the most useful options for find is -type, which allows you to specify the type of object you wish to look for. Remember that everything on a UNIX system is a file (covered back in Chapter 1, "Things to Know About Your Command Line," in the "Everything Is a File" section), so what you're actually indicating is the type of file you want find to ferret out for you. Table 10.2 lists the file types you can use with find.
File Type Letter Meaning
f Regular file
d Directory
l Symbolic (soft) link
b Block special file
c Character special file
p FIFO (First In First Out)
s Socket
Ex:
$ find Steely_Dan/ -type d
$ find Steely_Dan/ -type d | sort
find -a
A key feature of find is the ability to join several options to more tightly focus your searches. You can link together as many options as you'd like with -a (or -and).
$ find . -name "Rolling_Stones*" -a -type f
$ find . -name " Rolling_Stones* " -a -type f | wc -l
find -o, we can also utilize -o (or -or) to combine options using OR.
Ex:
find . -size +10M -o -size 10M
find . \( -size +10M -o -size 10M \) ! -name "*25*"
find . \( -name "*mp3*" -o -name "*.ogg*" \) -a -type f | wc -l
find . \( -name "*mp3*" -o -name "*.ogg*" -o -name "*.flac*" \) -a -type f | wc -l
Execute a Command on Every Found File, find -exec
Ex:
find . -name " *MP3 " -exec rename's/MP3/mp3/g' {} \;
The rename command is followed with instructions for the name change in this format: s/old/new/g. (The s stands for "substitute," while the g stands for "global.")
ex: find . -name "* *m3u" -exec rename 's/\ /_/g' {}
Print Find Results into a File, find -fprint
Ex: find . ! \( -name "*mp*" -o -name "*ogg" -o -name "*flac" -o -type d \) -fprint non_music_files.txt

No comments:
Post a Comment