Linux operatörleri, komut satırında ya da shell scriptlerinde kullanılan, belirli işlemleri gerçekleştiren özel sembollerdir. Bu operatörler, dosya işlemleri, veri yönetimi, mantıksal ve matematiksel işlemler gibi pek çok farklı işlevi yerine getirmek için kullanılır. Linux'taki operatörler genellikle şu kategorilerde incelenebilir:
1. Redirection (Yönlendirme) Operatörleri
Bu operatörler, komutların çıktısını bir dosyaya yönlendirmek ya da bir dosyadan veri almak için kullanılır.
-
>
: Çıktıyı bir dosyaya yazar. Eğer dosya varsa, içeriğini siler ve yeni veri ekler.bashecho "Hello, World!" > output.txt
-
>>
: Çıktıyı bir dosyaya ekler. Eğer dosya varsa, mevcut içeriğin sonuna yazılır.bashecho "New Line" >> output.txt
-
<
: Dosyadan giriş alır.bashsort < input.txt
-
<<
: Here document, bir komut bloğu oluşturur. Komutlar, belirtilen bir sınıra kadar çalıştırılır.bashcat << EOF This is a multiline string. EOF
2. Pipes (|
)
-
|
: Komutların çıktısını bir sonraki komutun girişine yönlendirir.bashcat file.txt | grep "pattern"
Bu komut,
file.txt
dosyasındaki tüm içeriğigrep
komutuna gönderir ve belirtilen "pattern"ı arar.
3. Comparison (Karşılaştırma) Operatörleri
Bu operatörler, dosya ve sayısal verileri karşılaştırmak için kullanılır.
Sayısal Karşılaştırma:
-
-eq
: Eşit (==
veya-eq
)bashif [ $a -eq $b ]; then echo "Equal"; fi
-
-ne
: Farklı (!=
veya-ne
)bashif [ $a -ne $b ]; then echo "Not Equal"; fi
-
-gt
: Büyük (>
)bashif [ $a -gt $b ]; then echo "Greater"; fi
-
-lt
: Küçük (<
)bashif [ $a -lt $b ]; then echo "Lesser"; fi
-
-ge
: Büyük ya da eşit (>=
)bashif [ $a -ge $b ]; then echo "Greater or Equal"; fi
-
-le
: Küçük ya da eşit (<=
)bashif [ $a -le $b ]; then echo "Lesser or Equal"; fi
Dosya Karşılaştırma:
-
-e
: Dosya var mı?bashif [ -e file.txt ]; then echo "File exists"; fi
-
-f
: Dosya bir normal dosya mı?bashif [ -f file.txt ]; then echo "Regular file"; fi
-
-d
: Dizin mi?bashif [ -d /home/user ]; then echo "Directory"; fi
-
-r
: Dosya okunabilir mi?bashif [ -r file.txt ]; then echo "Readable"; fi
-
-w
: Dosya yazılabilir mi?bashif [ -w file.txt ]; then echo "Writable"; fi
-
-x
: Dosya çalıştırılabilir mi?bashif [ -x file.sh ]; then echo "Executable"; fi
4. Logical (Mantıksal) Operatörler
Mantıksal işlemleri gerçekleştiren operatörler.
-
&&
: AND operatörü, her iki komut başarılı olursa ikinci komut çalıştırılır.bashmkdir newdir && cd newdir
-
||
: OR operatörü, ilk komut başarısız olursa ikinci komut çalıştırılır.bashmkdir newdir || echo "Directory creation failed"
-
!
: NOT operatörü, bir komutun tersini kontrol eder.bashif ! [ -d /home/user ]; then echo "Directory doesn't exist"; fi
5. Mathematical (Matematiksel) Operatörler
Bunlar, sayısal hesaplamalar için kullanılır.
-
+
: Toplamabashresult=$((5 + 3))
-
-
: Çıkarmabashresult=$((5 - 3))
-
*
: Çarpmabashresult=$((5 * 3))
-
/
: Bölmebashresult=$((5 / 3))
-
%
: Modül (kalan)bashresult=$((5 % 3))
6. File Manipulation (Dosya Manipülasyonu) Operatörleri
Dosya ve dizin işlemleri için kullanılır.
-
-
: Bir dosya ya da dizinle işlem yapmak için kullanılan bir genelleme sembolüdür.bashrm -rf /path/to/directory
-
~
: Kullanıcı ev dizinine erişim.bashcd ~
-
/
: Kök dizine erişim.bashcd /
7. Command Substitution (Komut Yerine Koyma)
Bir komutun çıktısını başka bir komutta kullanmak için kullanılır.
-
`command`
veya$(command)
:bashresult=$(ls -l) echo "$result"
8. Ternary (Kısa Koşul) Operatörü
-
?:
: Koşullu işlem yapılır.bash[ $a -gt $b ] && echo "a is greater" || echo "b is greater"
9. Wildcard (Yıldız) Operatörleri
Dosya ve dizin aramak için kullanılır.
-
*
: Herhangi bir karakter sayısı (sıfır dahil).bashls *.txt
-
?
: Bir karakter.bashls file?.txt
-
[]
: Karakter aralığı.bashls file[1-3].txt
Özet
Linux operatörleri, sistemdeki dosyalarla çalışırken, programları birbirine bağlarken ve matematiksel işlemleri gerçekleştirirken oldukça önemlidir. Bu operatörleri doğru kullanmak, Linux komut satırında daha verimli çalışmanıza yardımcı olur.
***
ChatGPT:
To replace the contents of the file named passwords
with the word password123
, you can use the following command:
bashecho "password123" > passwords
Explanation:
-
echo "password123"
outputs the stringpassword123
. -
>
is the redirection operator, which writes the output to the specified file (passwords
). -
This will overwrite the contents of the
passwords
file withpassword123
.
If you want to append password123
to the file without overwriting the existing content, use >>
instead:
bashecho "password123" >> passwords
Şunu dedin:
ChatGPT:
To add "tryhackme" to the passwords
file while keeping the existing content ("password123"), you would use the >>
append operator like this:
bash
echo "tryhackme" >> passwords
Explanation:
-
echo "tryhackme"
outputs the stringtryhackme
. -
>>
appends the output to the filepasswords
without overwriting its existing contents.
This way, the file will contain both password123
and tryhackme
on separate lines or as a single line, depending on how the file was formatted. If you want them on separate lines, ensure there's a newline at the end of the file or include it explicitly:
bash
echo -e "tryhackme\n" >> passwords
Labels: linux fundamentals |