gokayburuc.dev

Bash ile Dosya Oluşturma Fonksiyonu

Aşağıdaki Bash fonksiyonu, tarih formatıyla birlikte adlandırılmış bir Markdown dosyasını, belirli bir dizinde oluşturmayı sağlar. Her bir satırının işlevini ayrıntılı olarak açıklayalım:


nn () {
  # Generate the current date in YYYY-MM-DD format
  create_date="$(date +%Y-%m-%d)"

  # Convert the input argument ($1) to lowercase and replace spaces with underscores
  filename=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')

  # Combine date and filename to create the new file name
  newfile="${create_date}_${filename}.md"

  # Navigate to the target directory and create the new file
  cd ~/Documents/vault/one-ring-to-rule-em-all/00-inbox/ || return
  touch "$newfile"

  # Print a success message
  echo "File created: $newfile"
}

Satır Satır Teknik Açıklama

1. Tarih Oluşturma

create_date="$(date +%Y-%m-%d)"

2. Dosya Adını Düzenleme

filename=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')

3. Dosya Adını Birleştirme

newfile="${create_date}_${filename}.md"

4. Hedef Dizine Geçiş

cd ~/Documents/vault/one-ring-to-rule-em-all/00-inbox/ || return

5. Dosyayı Oluşturma

touch "$newfile"

6. Başarı Mesajı Yazdırma

echo "File created: $newfile"

Kullanım Örneği

Fonksiyonu çalıştırmak için aşağıdaki komutu terminale yazabilirsiniz:

nn "My Notes"

İpuçları ve Geliştirmeler