Giriş
Açıklaması şöyle
Using -perm, we can find all files with a particular permission combination.
Here are all the options for checking access/modify time:-amin n - the file was last accessed within n minutes ago.-atime n - the file was last accessed within n days ago.-cmin n - the file status changed within n minutes ago.-ctime n - the file status changed within n days ago.-mmin n - the file was last modified within n minutes ago.-mtime n - the file was last modified within n days ago.-anewer file - the searched file was modified more recently than [FILE]. If using -L or -H, and [FILE] is a symbolic link, then the file it points to is used.-cnewer file - the search for file status changed more recently than [FILE]. If using -L or -H, and [FILE] is a symbolic link, then the file it points to is used. If you add -daystart to the end of any of these, the measurement will be taken from the start of the current day, rather than the current time,
Finally, + and - modifications are also available. For instance, -amin -5 will find anything modified in the last 5 minutes, but -amin +5 will find anything modified more than 5 minutes ago.
-mmin on the other hand can count in minutes. So, if strict accuracy is vital, then -mmin +1440 ( 1440 minutes = 1 day ) could be used instead of -mtime +1
find . -name "*.txt" -mmin 5 -daystart
In other words, -mtime can count only in units of 24 hours or one day each so as far as -mtime +1 goes, this means exactly more than one day by at least one day ( ie. two days+ )Örnek
find "$DESDIR" -mtime +6 -type f -delete
... the full list of units can be found below:
b - 512-byte blocks (default).
c - just bytes.
w - two-byte words.
k - Kilobytes.
M - Megabytes.
G - Gigabytes.
find ./test -size -10M
Using the -group or -user options, we can also find a directory or file by who owns it, or which group it belongs to.
find ./test -group someGroupfind /directory ! -user myusername -printf "%u %p\n" Since there are many different types of files and directories, it is not always useful to just search for a specific string. If we want to mention the type of file or directory we are looking for, we can specify it with the -type option. To remove ambiguity, we also add the -name option before our search string. You can also just remove the search string entirely, and search for a particular file type.
A full list of all types can be found below:b - block devicesc - character devicesd - directoryp - named pipe (FIFO)f - regular filel - symbolic link - never used if we use the symbolic link -L option.s - socket
find ./test -type f -name "*.txt"
find /path/to/directory -maxdepth 1 -type l -delete
Some folders may contain symbolic links that link to another part of your file system. As such, you might unknowingly search a much larger set of folders, if symbolic links do exist. As such, there are a number of options that control what happens when find comes across a symbolic link:
- -P - when used, symbolic links are never followed.
- -L - when used, symbolic links are always followed.
- -H - when used, symbolic links are only followed if mentioned somewhere on the command line, i.e. if you search for a link, and there is a symbolic link called link, find will return a link along with anything found when following that symbolic link.
-P Never follow symbolic links. This is the default behaviour.
When find examines or prints information a file, and the file is
a symbolic link, the information used shall be taken from the
properties of the symbolic link itself.
-L Follow symbolic links. [...]
-H Do not follow symbolic links, except while processing the com‐
mand line arguments. When find examines or prints information
about files, the information used shall be taken from the prop‐
erties of the symbolic link itself. The only exception to this
behaviour is when a file specified on the command line is a sym‐
bolic link, and the link can be resolved. For that situation,
the information used is taken from whatever the link points to
(that is, the link is followed). The information about the link
itself is used as a fallback if the file pointed to by the sym‐
bolic link cannot be examined. If -H is in effect and one of
the paths specified on the command line is a symbolic link to a
directory, the contents of that directory will be examined
(though of course -maxdepth 0 would prevent this).find -H /bin -iname 'sh*'
Eğer -H seçeneğini kullamazsak şöyle yapınca bir sonuç alamayız$ find /bin -iname 'sh*'find . -maxdepth 1 -type d -empty -printf '\n' | wc -l
find . -type f -iname '*.c' -printf '%h\0' |
sort -z |
uniq -zc |%g specifier - group namefind . -mindepth 2 -printf "%s\t%p\n" | sort -n | cut -f 2- | tail -n $n%p specifier - file's name$ sudo find . -name 'example.com*' -printf "%y %p\n"
d ./archive/example.com
f ./renewal/example.com.conf
d ./live/example.com%P specifier - Dosyanın Bulunduğu Dizin İsmiFile's name with the name of the starting-point under which it was found removed.
find /Volumes/SpeedyG -type d >> file.txt
# Çıktı şöyle
/Volumes/SpeedyG/folder1
/Volumes/SpeedyG/folder2
/Volumes/SpeedyG/folder2
find /Volumes/SpeedyG -type d -printf '%P\n' >> file.txt
# Çıktı şöyle
folder1
folder2
folder3$ find /usr/local/bin/ /bin/ -printf '%P\n'$ sudo find /var -printf '%u:%g\n' | sort -t: -u
_apt:root
clamav:adm
clamav:clamav
colord:colord
daemon:daemon
lightdm:lightdm
lp:lp
man:root
speech-dispatcher:root
statd:nogroup
steeldriver:crontab
steeldriver:lightdm
steeldriver:steeldriver
syslog:adm
systemd-timesync:systemd-timesync
testuser:crontab
... if you want to delete files you find with the find command, you can use the -delete flag at the end of your find code. If you put it first, it will delete everything in the folder you specify. So be careful.
-delete Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option.
find -type f -name '*abc*' -delete
delete ve empty birlikte kullanımı
mkdir -p dir1/dir2/dir3/dir4find dir1 -empty -deleteThat's possibly they've considered that it's more commonly useful to be able to delete some files and then possibly the directory containing those files if it was emptied at the same time
Test: -regex exprTest: -iregex exprTrue if the entire file name matches regular expression expr. This is a match on the whole path, not a search. […]
find . -type f -iregex '.*\.tiff?'find /tmp/txz/httperf/httperf-20210403git/ -regextype posix-egrep -regex [A-Z][A-Z]$
Düzeltmek için şöyle yaparızfind /tmp/txz/httperf/httperf-20210403git/ |egrep [A-Z][A-Z]$
/tmp/txz/httperf/httperf-20210403git/.git/refs/remotes/origin/HEAD
/tmp/txz/httperf/httperf-20210403git/.git/logs/refs/remotes/origin/HEAD
/tmp/txz/httperf/httperf-20210403git/.git/logs/HEAD
/tmp/txz/httperf/httperf-20210403git/.git/HEAD
/tmp/txz/httperf/httperf-20210403git/NEWS
/tmp/txz/httperf/httperf-20210403git/COPYRIGHT
/tmp/txz/httperf/httperf-20210403git/AUTHORS
/tmp/txz/httperf/httperf-20210403git/TODO
/tmp/txz/httperf/httperf-20210403git/src/lib/READMEThe -name predicate takes a glob, not a regular expression.Wildcard dilediğimiz yerde kullanılabilir. Wildcard not ile birlikte kullanılabilir. Not için istersek harfle "not" veya "!" yazabiliriz.
In a regular expression, * would match zero or more of the preceding item. But since this is a glob, it matches zero or more of any character. That is, * in a glob means the same thing as .* in a regular expression.
GNU find's -regex non-standard extension does take a regexp (old style emacs type by default), but that's applied on the full path (like the standard -path which also takes wildcards), not just the file name (and are anchored implicitly)So to find files of type directory whose name starts with machine, you'd need:find . -name 'machine*' -type dOr:find . -regextype posix-extended -regex '.*/machine[^/]*' -type d
find $directory -name '*.[hc]' -exec clang-format -i {} \;find . -regextype posix-extended -regex '.*\.(c|h|cpp)'find "$HOME" -type d -name 'bowtie*1.[!2]*'The pattern matches names starting with bowtie and then contains 1.n, where n is not 2.
$ find -not -name mongodb.log -name "*.log"Örnek - not extension kullanımıfind src -type f -name '*.ts' ! -name '*.d.ts'Örnek - not name kullanımıfind . -maxdepth 1 -type d ! -name "\.*"Örnek - and name ve not name kullanımı$ find -name 'master*' \! -name 'master-2018*' -print0 |
xargs -0 echo rm -frÖrnek - extensionfind . -path "*/flac/*" -name '*.mp3' -execdir mv -t ../ {} +Örnek - name + extensionfind . -type f -name 'cbs_cdr_vou_20180615*.unl' -exec cat {} + >/home/fifa/cbs/test.txtfind includes the directories from which it starts
$ find .
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txtfind . -maxdepth 1 -not -path . | wc -l
find $HOME -maxdepth 1 -not -path $HOME | wc -la
├── b
│ └── e
├── c
│ └── e
└── d$ find . -path '*/c/e'
./a/c/eÖrnek
mp3 dosyalarını bir üst dizine taşımak için şöyle yaparız.
find . -path "*/flac/*" -name '*.mp3' -execdir mv -t ../ {} +
Once you find some files or directories with the find command, you might want to do something for them. Thankfully, we can use the -exec option to run commands on each of the files or directories found. Any code after -exec will run on every file found.
find . type f -exec echo rm {} \;Exec allows us to either pass all arguments at once with {} + or to pass them one by one with {} \;1. \+ ile bitmesi - komuta çoklu parametre geçilir
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines.
... there may be nothing between {} and + apart from whitespace. There is no way around that.
find . -name '*.png' -exec cp {} /tmp/dest \+find . -type f -exec cat {} \+ | wc -l
find . -name "*.txt" -exec chown someOwner {} \;# risky
find -exec sh -c "something {}" \;
find -execdir sh -c "something {}" \;
Şu şekilde çalıştırmak daha güvenli deniyor ancak bu yöntemin pek kullanıldığını görmedim# safer
find -exec sh -c 'something "$@"' sh {} \;
find -execdir sh -c 'something "$@"' sh {} \;
Örnekfind . -type f -newermt '16 july 2018' -exec cp {} /share/test \;
Örnekfind src -name 'abc*' -type f -exec mv -nv {} dest/ \;
Eğer işlemden önce kullanılacak komutu görüp emin olmak istersek şöyle yaparız.find src -name 'abc*' -type f -exec echo mv -nv {} dest/ \;
Örnekfind . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;
Örnekfind . -type d -exec sh -c '
for dirpath do
filepath="$dirpath/${dirpath##*/}"
[ -f "$filepath" ] && printf "%s\n" "$filepath"
done' sh {} +
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;$ find */ -name "result.txt" -exec bash -c
'printf "%s,%s\n" "${0%%/*}" "$(cat $0)"' {} \;
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec bash -c
'cjpeg -quality 80 "$1" > "$(dirname "$1")/optimized_$(basename "$1")"' sh {} \;
find . -type f \
-exec sh -c 'file "$1" | grep -q PDF' _ {} \; \
-exec sh -c 'echo mv -- "$1" "${1%%jpg}pdf"' _ {} \;6. Komutun Sonucu_ {} is needed so that the contents of {} is assigned to $1 not $0