linux operators

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.

    bash
    echo "Hello, World!" > output.txt
  • >>: Çıktıyı bir dosyaya ekler. Eğer dosya varsa, mevcut içeriğin sonuna yazılır.

    bash
    echo "New Line" >> output.txt
  • <: Dosyadan giriş alır.

    bash
    sort < input.txt
  • <<: Here document, bir komut bloğu oluşturur. Komutlar, belirtilen bir sınıra kadar çalıştırılır.

    bash
    cat << EOF This is a multiline string. EOF

2. Pipes (|)

  • |: Komutların çıktısını bir sonraki komutun girişine yönlendirir.

    bash
    cat file.txt | grep "pattern"

    Bu komut, file.txt dosyasındaki tüm içeriği grep 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)

    bash
    if [ $a -eq $b ]; then echo "Equal"; fi
  • -ne: Farklı (!= veya -ne)

    bash
    if [ $a -ne $b ]; then echo "Not Equal"; fi
  • -gt: Büyük (>)

    bash
    if [ $a -gt $b ]; then echo "Greater"; fi
  • -lt: Küçük (<)

    bash
    if [ $a -lt $b ]; then echo "Lesser"; fi
  • -ge: Büyük ya da eşit (>=)

    bash
    if [ $a -ge $b ]; then echo "Greater or Equal"; fi
  • -le: Küçük ya da eşit (<=)

    bash
    if [ $a -le $b ]; then echo "Lesser or Equal"; fi

Dosya Karşılaştırma:

  • -e: Dosya var mı?

    bash
    if [ -e file.txt ]; then echo "File exists"; fi
  • -f: Dosya bir normal dosya mı?

    bash
    if [ -f file.txt ]; then echo "Regular file"; fi
  • -d: Dizin mi?

    bash
    if [ -d /home/user ]; then echo "Directory"; fi
  • -r: Dosya okunabilir mi?

    bash
    if [ -r file.txt ]; then echo "Readable"; fi
  • -w: Dosya yazılabilir mi?

    bash
    if [ -w file.txt ]; then echo "Writable"; fi
  • -x: Dosya çalıştırılabilir mi?

    bash
    if [ -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.

    bash
    mkdir newdir && cd newdir
  • ||: OR operatörü, ilk komut başarısız olursa ikinci komut çalıştırılır.

    bash
    mkdir newdir || echo "Directory creation failed"
  • !: NOT operatörü, bir komutun tersini kontrol eder.

    bash
    if ! [ -d /home/user ]; then echo "Directory doesn't exist"; fi

5. Mathematical (Matematiksel) Operatörler

Bunlar, sayısal hesaplamalar için kullanılır.

  • +: Toplama

    bash
    result=$((5 + 3))
  • -: Çıkarma

    bash
    result=$((5 - 3))
  • *: Çarpma

    bash
    result=$((5 * 3))
  • /: Bölme

    bash
    result=$((5 / 3))
  • %: Modül (kalan)

    bash
    result=$((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.

    bash
    rm -rf /path/to/directory
  • ~: Kullanıcı ev dizinine erişim.

    bash
    cd ~
  • /: Kök dizine erişim.

    bash
    cd /

7. Command Substitution (Komut Yerine Koyma)

Bir komutun çıktısını başka bir komutta kullanmak için kullanılır.

  • `command` veya $(command):

    bash
    result=$(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).

    bash
    ls *.txt
  • ?: Bir karakter.

    bash
    ls file?.txt
  • []: Karakter aralığı.

    bash
    ls 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.

***



If I wanted to replace the contents of a file named "passwords" with the word "password123", what would my command be?
ChatGPT:

To replace the contents of the file named passwords with the word password123, you can use the following command:

bash
echo "password123" > passwords

Explanation:

  • echo "password123" outputs the string password123.

  • > is the redirection operator, which writes the output to the specified file (passwords).

  • This will overwrite the contents of the passwords file with password123.

If you want to append password123 to the file without overwriting the existing content, use >> instead:

bash
echo "password123" >> passwords
Şunu dedin:
Now if I wanted to add "tryhackme" to this file named "passwords" but also keep "passwords123", what would my command be

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 string tryhackme.

  • >> appends the output to the file passwords 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