find etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
find etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

31 Mart 2022 Perşembe

find komutu permission'a göre arama seçenekleri

Giriş
Açıklaması şöyle
Using -perm, we can find all files with a particular permission combination.
Örnek
Şöyle yaparız.
find ./test -perm 0755
Örnek
Group permission olmayan dosyaları bulmak için şöyle yaparız.
find . \! -perm /g+rwx

find komutu last accessed/modified time'a göre arama seçenekleri

Giriş
Açıklaması şöyle. a ile başlayanlar accessed, c ile başlayanlar changed, m ile başlayanlar modified içindir
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, 
Burada şu açıklama önemli
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.

Modified Seçenekleri
-mmin seçeneği
Son x dakika değiştirmiş dosyaları listeler. Açıklaması şöyle.
-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
Örnek
Gün başından en fazla 5 dakika önce değiştirilmiş dosyaları bulmak için şöyle yaparız
find . -name "*.txt" -mmin 5 -daystart
-mtime seçeneği
Son x günde değiştirilmiş dosyaları listeler. Birim olarak gün kullandığı için 1.5 gün diye bir şey bilmez. Ya 1 gün ya 2 gün, ya da 3 gün şeklindedir. Açıklaması şöyle
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
Şöyle yaparız. Son 6 günden eski dosyaları silmek için şöyle yaparız.
find "$DESDIR" -mtime +6 -type f -delete

find komutu büyüklüğe göre arama seçenekleri

Giriş
Açıklaması şöyle
... 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.
Örnek
10M'den küçük dosyaları bulmak için şöyle yaparız
find ./test -size -10M

find komutu owner ve group'a göre arama seçenekleri

Giriş
Açıklaması şöyle
Using the -group or -user options, we can also find a directory or file by who owns it, or which group it belongs to.
-group seçeneği
Örnek
Şöyle yaparız
find ./test -group someGroup
- user seçeneği
Örnek
Şöyle yaparız
find ./test -user someOwner
Örnek
Belirtilen kullanıcının sahibi olmadığı dosyaları görmek için şöyle yaparız.
find /directory ! -user myusername -printf "%u %p\n" 

find komutu dosya tipine göre arama seçenekleri

Giriş
Açıklaması şöyle
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.
Dosya tipleri şöyle
A full list of all types can be found below:

b - block devices
c - character devices
d - directory
p - named pipe (FIFO)
f - regular file
l - symbolic link - never used if we use the symbolic link -L option.
s - socket
Örnek - regular file
Şöyle yaparız
find ./test -type f -name "*.txt"
Örnek - symbolic link
Bir dizindeki symbolic linkleri silmek için şöyle yaparız
find /path/to/directory -maxdepth 1 -type l -delete

15 Kasım 2021 Pazartesi

find komutu symbolic link arama seçenekleri

Giriş
Açıklaması şöyle
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.
Açıklaması şöyle
  -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).
-H seçeneği
Şöyle yaparız. Ubuntu'da /bin artık symbolic link. Ancak komut satırından girince find komutu takip etsin istiyoruz.
find -H /bin -iname 'sh*'
Eğer -H seçeneğini kullamazsak şöyle yapınca bir sonuç alamayız
$ find /bin -iname 'sh*'

30 Ağustos 2021 Pazartesi

find komutu printf seçeneği

Specifier Olmayan Şeyler
Örnek
Boş dizinleri saymak için şöyle yaparızz
find . -maxdepth 1 -type d -empty -printf '\n' | wc -l
%h specifier - dirname
Örnek
*.c uzantılı dosyaların bulunduğu dizini yazdırmak için şöyle yaparız
find . -type f -iname '*.c' -printf '%h\0' |
  sort -z |
  uniq -zc |
%g specifier - group name
group bilgisini verir.

%s specifier - File's size in bytes
Örnek
Boyu en büyük dosyaları bulmak için şöyle yaparız
find . -mindepth 2 -printf "%s\t%p\n" | sort -n | cut -f 2- | tail -n $n
%p specifier - file's name
Örnek
Şöyle yaparız. %y ile d veya f karakteri dizin veya dosya olduğunu belirtir. %p ile dosya/dizin ismi gösterilir.
$ 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 İsmi
Açıklaması şöyle. Yani en son dizinin ismini verir.
File's name with the name of the starting-point under which it was found removed.
Örnek
Şöyle yaparız
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
Örnek
Şöyle yaparız
$ find /usr/local/bin/ /bin/ -printf '%P\n'
%t - File's last modification time
Örnek ver

%u specifier - File's user name
user yani dosya sahibini (owner) verir. Şöyle yaparız.
$ 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


23 Haziran 2021 Çarşamba

find komutu delete seçeneği - Kabul Edilen Dosya ve Dizinleri Siler

Giriş
Açıklaması şöyle. Yani - delete find komutunun sonunda veya başında olursa farklı davranır
... 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.
Açıklaması şöyle. Bu seçenek standart POSIX seçeneği değil ancak çoğu sistemde bulunuyor.
-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.
Örnek
Şöyle yaparız.
find . -name "*.txt" -delete
Örnek
Şöyle yaparız.
find -type f -name '*abc*' -delete
delete ve empty birlikte kullanımı
Elimizde şöyle bir dizin olsun. dir4 hariç her dizin doludur, çünkü içinde alt dizin var
mkdir -p dir1/dir2/dir3/dir4
Şöyle yaparız ve tüm dizinlerin silindiğini görürüz. Çünkü delete en derinden (depth first) işleme başlar ve daha sonra yukarıya çıkar.
find dir1 -empty -delete
Açıklaması şöyle
That'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 

5 Nisan 2021 Pazartesi

find komutu regex seçeneği

Giriş
Açıklaması şöyle
Test: -regex expr
Test: -iregex expr
True if the entire file name matches regular expression expr. This is a match on the whole path, not a search. […]
-iregex seçeneği
Örnek
Şöyle yaparız
find . -type f -iregex '.*\.tiff?'
regex seçeneği
Örnek
Elimizde şöyle bir komut olsun. Bu komut büyük harli dosyaları seçmeye çalışıyor ancak çıktısı boştur. Çünkü regex tüm path için uygulanıyr.
find /tmp/txz/httperf/httperf-20210403git/ -regextype posix-egrep -regex [A-Z][A-Z]$
Düzeltmek için şöyle yaparız
find /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/README

31 Ocak 2021 Pazar

find komutu name seçeneği - Belirtilen glob/wildcard'a Uyanları Kabul Eder

Giriş
name seçeneği düzenli ifade (regular expression) değil glob/wildcard kullanır. dolayısıyla "*" karakterini kullanabiliriz. Açıklaması şöyle
The -name predicate takes a glob, not a regular expression.
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.
Wildcard dilediğimiz yerde kullanılabilir. Wildcard not ile birlikte kullanılabilir. Not için istersek harfle "not" veya "!" yazabiliriz.

2. -regex Alternatifi
Açıklaması şöyle. glob/wildcard yerine regex kullanmak mümkün ancak ben bu yöntemi hiç sevmedim.
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 d
Or:

find . -regextype posix-extended -regex '.*/machine[^/]*' -type d
-name yerine - regex ne zaman lazım olur?
Elimizde şöyle bir komut olsun. Bu komut çalışmaz. Çünkü -name ile globbing yerine brace expansion yapılmaya çalışılıyor
find $directory -name '*.[hc]' -exec clang-format -i {} \;
 Bu  gibi durumlarda regex daha iyi. Şöyle yaparız
find . -regextype posix-extended -regex '.*\.(c|h|cpp)'
3. Kullanım
Örnek - karakter sayısı
Şöyle yaparız
find "$HOME" -type d -name 'bowtie*1.[!2]*'
Açıklaması şöyle
The pattern matches names starting with bowtie and then contains 1.n, where n is not 2.
Örnek - not name kullanımı
Şöyle yaparız. Bu sefer not için harfle "not" kullanılıyor.
$ find -not -name mongodb.log -name "*.log"
Örnek - not extension kullanımı
Şöyle yaparız.
find src -type f -name '*.ts' ! -name '*.d.ts'
Örnek - not name kullanımı
İsmi belirtilen örüntüye uymayanları listelemek için not kullanılır. İsmi "." ve ".." olmayan dizinleri bulmak için şöyle yaparız.
find . -maxdepth 1 -type d ! -name "\.*"
Örnek - and name ve not name kullanımı
İsmi master ile başlayan ancak master-2018 ile başlamayanları silmek için şöyle yaparız.
$ find -name 'master*' \! -name 'master-2018*' -print0 |
     xargs -0 echo rm -fr
Örnek - extension
mp3 dosyalarını bir üst dizine taşımak için şöyle yaparız.
find . -path "*/flac/*" -name '*.mp3' -execdir mv -t ../ {} +
Örnek - name + extension
unl uzantılı dosyaları bulmak için şöyle yaparız.
find . -type f -name 'cbs_cdr_vou_20180615*.unl' -exec cat {} + >/home/fifa/cbs/test.txt

1 Aralık 2020 Salı

find komutu path seçeneği - glob/wildcard Kullanır

Giriş
Aranacak dizin ismini belirtir. Wildcard kullanılabilir. Başlanılan dizin çıktıya dahil edilir. Açıklaması şöyle
find includes the directories from which it starts
Örnek
Çıktıda . karakteri olduğunu görmek için şöyle yaparız
$ find .
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txt
Örnek
Çıktıda . yani başlangıç dizinini görmek istemiyorsak "-not -path ." veya "! -path ." şeklinde kullanırız. Şöyle yaparız
find . -maxdepth 1 -not -path . | wc -l
find $HOME -maxdepth 1 -not -path $HOME | wc -l
Örnek
Elimizde şöyle bir dizin yapısı olsun
a
├── b
│   └── e
├── c
│   └── e
└── d
Sadece c dizini içindeki e dizinlerini bulmak için şöyle yaparız
$ 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 ../ {} +

9 Nisan 2020 Perşembe

find komutu exec seçeneği

Giriş
Açıklaması şöyle
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.
exec seçeneğini kullanırken önce prova yapmak isteyebiliriz. Bu durumda komutun başına echo yazmamız yeterli.

Örnek
Şöyle yaparız. Böylece rm komutunun nasıl çalışacağı görülebilir.
find . type f -exec echo rm {} \;
1. \+ veya 
2. \; karakteri 

ile bitebilir. Açıklaması şöyle
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 
Açıklaması şöyle. -exec seçeneğinden sonra gelen komut mümkün olduğunca çok dosya ismi verilerek çalıştırılır yani her dosya için bir kere çalıştırılmaz.
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.
Örnek - yanlış kullanım
{} \+ arasında sadece boşluk (space) karakteri olabilir. Başka karakter giremez. Açıklaması şöyle
... there may be nothing between {} and + apart from whitespace. There is no way around that.
Dolayısıyla şu komut hata verir.
find . -name '*.png' -exec cp {} /tmp/dest \+
Örnek
exec ile çalıştırılan uygulamanın çıktısı başka uygulamaya geçilebilir. Şöyle yaparız,
Aslında cat a.txt b.txt c.txt | wc -l komutu ile aynıdır
find . -type f -exec cat {} \+ | wc -l
2. \; ile bitmesi - her parametre için komut bir kere daha çalıştırılır
-exec seçeneğinden sonra gelen komut için ayrı bir process yaratılır.
Örnek
Şöyle yaparız
find . -name "*.txt" -exec chown someOwner {} \;
Örnek
Bu komutun şu şekilde çalıştırılması riskli deniyor.
# 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 {} \;
Örnek
copy için şöyle yaparız
find . -type f -newermt '16 july 2018' -exec cp {} /share/test \;
Örnek
move için şöyle yaparız
find 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/ \;
Örnek
Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;
Örnek
Dizin ismi ile dosya ismi aynı ise bunları bulmak için şöyle yaparız.
find . -type d -exec sh -c '
    for dirpath do
        filepath="$dirpath/${dirpath##*/}"
        [ -f "$filepath" ] && printf "%s\n" "$filepath"
    done' sh {} +
3. {} Kullanımı
 \+ veya \; ile geçilen parametreye erişmek için kullanılır

Örnek
Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;
4. $0 Kullanımı
Örnek - bash ile $0
Tüm results.txt dosyalarının ana dizinini ve dosyanın içini ekrana yazdırmak için şöyle yaparız.
$ find */ -name "result.txt" -exec bash -c 
  'printf "%s,%s\n" "${0%%/*}" "$(cat $0)"' {} \;
5. $1 Kullanımı
Örnek - 
bash ile $1
bash ile $1 kullanılabilir. Şöyle yaparız.
find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec bash -c
  'cjpeg -quality 80 "$1" > "$(dirname "$1")/optimized_$(basename "$1")"' sh {} \;
Örnek
Şöyle yaparız. Burada file komutuna $1 ile dosya ismi geçiliyor. Dosya tipi PDF olanlar grep ile seçiliyor. 
find . -type f \
    -exec sh -c 'file "$1" | grep -q PDF' _ {} \;  \
    -exec sh -c 'echo mv -- "$1" "${1%%jpg}pdf"' _ {} \;
"_ {}" dizisinin açıklaması şöyle
_ {} is needed so that the contents of {} is assigned to $1 not $0
6. Komutun Sonucu
Başarısız olan uygulamaları görmek için şöyle yaparız
find . \! -exec yourscript {} \; -print