31 Mart 2022 Perşembe

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

23 Mart 2022 Çarşamba

command komutu

Giriş
Açıklaması şöyle
... you can use command -v to check for availability of a command. No need to check its output, command will tell you via its exit status if it succeeded to find the command or not (regardless of whether it's an external command found in $PATH, a builtin or a function)

command is a mandatory POSIX utility. The -v option to command used to be optional, but it's not any longer in the latest version of the POSIX specification
Örnek
Şöyle yaparız. "#!/usr/bin/sh -" kullanımının açıklaması burada
#!/usr/bin/sh -

main() {
    for mycommand do
        printf >&2 '%s\n' "Checking $mycommand"
        if ! command -v -- "$mycommand" > /dev/null 2>&1; then
            printf >&2 '%s\n' "I think I am missing $mycommand"
        fi
    done
}

main less asdf


20 Mart 2022 Pazar

xmlstarlet komutu

Giriş
XML'den veri çekmek içindir

select seçeneği
Örnek
Derinliği ne olursa olsun belirtilen tag'e ait veriyi çekmek için şöyle yaparız
xmlstarlet select --template --value-of '//minSeaLevelPres' -n weatherdata.kml

14 Mart 2022 Pazartesi

xclip komutu

selection seçeneği
Örnek
Ekranda seçip kopyaladığımız metin içinde grep ile FAIL kelimesini aramak için şöyle yaparız
xclip -selection clipboard -o | grep FAIL
Aynı şeyi şöyle de yapabiliriz
xclip -sel c -o | grep FAIL

15 Şubat 2022 Salı

awk komutu - Dosyayı Sütün Haline Getirir

Giriş
awk ile dosyayı sütunlardan ibaretmiş gibi düşünmek gerekir. Sütun numaraları 1'den başlar. 
- Birinci "/" karakterinden sonra aradığımız şeyi belirtiriz. 
- İkinci "/" karakterinden sonra da eşleşme varsa ne yapmak istediğimizi belirtiriz.

Regular Expression
Açıklaması şöyle. awk her zaman Posix extended regex kullanır
The awk utility shall make use of the extended regular expression notation [...]
...
The sed utility always uses POSIX basic regular expressions unless you use it with its (so far) non-standard but fairly commonly available -E option. 

The grep utility also defaults to POSIX basic regular expressions, but the -E option that enables it to use POSIX extended regular expressions is actually standard (while -P for Perl-compatible regular expressions, PCRE, is non-standard and not commonly implemented on non-GNU systems). 

The grep utility can additionally interpret the given expressions as plain strings with its standard -F option.
awk vs grep
Awk ile içinde bir kelime geçen satırın 4. alanını bulmak grep ile bulmaya göre çok daha kolay

Örnek
Şöyle yaparız
$ awk '/received/{print $4}'
vs
$ grep -oP '\d+(?= received,)'
awk ve Sütün Değerini Karşılaştırma
Açıklaması şöyle. Yani sütun değeri string ise ve karşılaştırılan değer sayı ise her şey önce string'e çevrilir.
When comparing operands of mixed types, numeric operands are converted to strings using the value of CONVFMT
Örnek
Elimizde şöyle bir veri olsun
Chen Cho 5/19/63 203-344-1234 $76
Tom Billy 4/12/45 913-972-4536 $102
Larry White 11/2/54 908-657-2389 $54
Bill Clinton 1/14/60 654-576-4114 $201
Steve Ann 9/15/71 202-545-8899 $58
Şöyle yaparız
awk '$4 < 40' employees
Çıktı olarak şunu alırız. Çünkü sütun değeri bir string, 40 ise bir sayı. 40 önce string'e çevrilir, daha sonra karşılaştırma yapılır. Bu durumda 9 le başlayan değerler 4'ten küçük olduğu için çıktıya dahil olmazlar.
203-344-1234
202-545-8899
Örnek
Şöyle yaparız. Burada birinci sütun Uptime kelimesi olan, üçüncü sütunun değeri de 100'den büyük olan satırları görebiliriz
awk '$1 == "Uptime" && $3 > 100 {print}' file1
Dosya ve çıktısı şöyledir
Uptime is 172 days
Uptime is 562 days
Uptime is 30 days
downtime is 197 days

# Çıktı
Uptime is 172 days
Uptime is 562 days
Sütun Numarasına Göre - NF Değişkeni
Örnek
Şöyle yaparız
#Print all lines in file with more or less than three fields. 
awk -F'\t' 'NF!=3' file

#To limit to only more than three fields, use:
awk -F'\t' 'NF>3' file
Satır Numarasına Göre - FNR Değişkeni 
Örnek - Belli Bir Satıra Bakmak
İkinci satırda "Category X" geçen dosyaları bulmak için şöyle yaparız
awk 'FNR == 2 && /Category X/ {print FILENAME}' *
Eğer dosyanın devamına bakmak istemiyorsak ki bu durumda gerek yok ve de awk sürümümüz destekliyorsa "nextfile" kullanılabilir. Şöyle yaparız
awk 'FNR==2{ if (/Category X/) print FILENAME; nextfile }' *
Açıklaması şöyle
... the use of nextfile (if your awk supports it - many do, some don't) will cause awk to stop reading the current file and move on to the next one once the 2nd line is read.
NR (number of records ) Değişkeni 
O ana kadar işlenen toplam satır sayısını verir.
Örnek
Şöyle yaparız. Burada dosya değişince FNR tekrar başa dönüyor ama NR artmaya devam ediyor.
$ cat file1
a
b
c

$ cat file2
d
e

$ awk '{print FILENAME, NR, FNR, $0}' file1 file2
file1 1 1 a
file1 2 2 b
file1 3 3 c
file2 4 1 d
file2 5 2 e
Örnek
Şöyle yaparız. FNR ile satır numarası 1 ise ve ilk dosya değilse "---" string'i basılıyor
awk 'FNR==1 && NR!=1  {print "---"}{print}' /<my_directory>/*.yaml | ...
BeforeAfterMatch
Bir eşleşmeden sonra gelen N'inci satıra erişmek içindir. Örnekler burada

Örnek
Şöyle yaparız. Burada phrase ile aranılan şey belirtiliyor. Eşleşmeden sonra gelen 3. satırdaki karakter değiştiriliyor.
awk 'c&&!--c {gsub(/r/,"ɹ")} /\\phrase/ {c=3} 1' file > newfile