Skip to content

Instantly share code, notes, and snippets.

@vigo
Created November 5, 2012 00:17
Show Gist options
  • Save vigo/4014521 to your computer and use it in GitHub Desktop.
Save vigo/4014521 to your computer and use it in GitHub Desktop.
Shell Tips

Shell Tips

Yazım Kuralları / Style Guide

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi # tek satırda if

Renkler

export RESET=$'\e[0m'
export BLACK=$'\e[0;30m'
export RED=$'\e[0;31m'
export GREEN=$'\e[0;32m'
export YELLOW=$'\e[0;33m'
export BLUE=$'\e[0;34m'
export PURPLE=$'\e[0;35m'
export CYAN=$'\e[0;36m'
export WHITE=$'\e[0;37m'

export BOLD_BLACK=$'\e[1;30m'
export BOLD_RED=$'\e[1;31m'
export BOLD_GREEN=$'\e[1;32m'
export BOLD_YELLOW=$'\e[1;33m'
export BOLD_BLUE=$'\e[1;34m'
export BOLD_PURPLE=$'\e[1;35m'
export BOLD_CYAN=$'\e[1;36m'
export BOLD_WHITE=$'\e[1;37m'

export UNDERLINE_BLACK=$'\e[4;30m'
export UNDERLINE_RED=$'\e[4;31m'
export UNDERLINE_GREEN=$'\e[4;32m'
export UNDERLINE_YELLOW=$'\e[4;33m'
export UNDERLINE_BLUE=$'\e[4;34m'
export UNDERLINE_PURPLE=$'\e[4;35m'
export UNDERLINE_CYAN=$'\e[4;36m'
export UNDERLINE_WHITE=$'\e[4;37m'

export FLASHING_BLACK=$'\e[5;30m'
export FLASHING_RED=$'\e[5;31m'
export FLASHING_GREEN=$'\e[5;32m'
export FLASHING_YELLOW=$'\e[5;33m'
export FLASHING_BLUE=$'\e[5;34m'
export FLASHING_PURPLE=$'\e[5;35m'
export FLASHING_CYAN=$'\e[5;36m'
export FLASHING_WHITE=$'\e[5;37m'
echo "${RED}Hello${RESET} World"
echo "${BOLD_GREEN}Hello${RESET} World"
echo "${UNDERLINE_CYAN}Hello${RESET} World"
echo "${FLASHING_WHITE}Hello${RESET} World"

export RED_ON_BLACK=$'\e[0;31;40m'
echo "${RED_ON_BLACK}Red text${RESET} on a black bg"

0;31;40'in Açıklaması

İlk rakam 0 stil oluyor.

  • 0 : Regular
  • 1 : Bold
  • 4 : Underline
  • 5 : Flashing Text

İkinci 31 text rengi yani kırmızı. 40 da arka plan rengi. Yani siyah.

Prompt Shel $PS1

Aynı renk değişkenlerini kullanmak için;

export PS1="\[${YELLOW}\]Yellow\[${RESET}\]"

Escape karakterleri \[ ve \]

LS_COLORS ile list renklendirmek

ls yerine gls kullanmak için; Homebrew'dan ya da MacPorts'dan coreutils i kurmuş olmak gerekiyor.

brew install coreutils      # Homebrew
sudo port install coreutils # MacPorts

Bu komutlar g prefix'i ile geliyor. Bunu da gsiz kullanmak için;

PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH" # Homebrew

PATH="/opt/local/libexec/gnubin/:$PATH" # MacPorts

Kullanım formatı:

export LS_COLORS="TYPE=STYLE;FG;BG"
no    Global default, although everything should be something
fi    Normal file
di    Directory
ln    Symbolic link. If you set this to ‘target’ instead of a numerical
      value, the color is as for the file pointed to.
pi    Named pipe (FIFO, PIPE)
do    Door
bd    Block device (BLOCK, BLK)
cd    Character device (CHAR, CHR)
or    Symbolic link pointing to a non-existent file (ORPHAN)
so    Socket (SOCK)
su    File that is setuid (u+s) (SETUID)
sg    File that is setgid (g+s) (SETGID)
tw    Directory that is sticky and other-writable (+t,o+w)
      (STICKY_OTHER_WRITABLE)
ow    Directory that is other-writable (o+w) and not sticky (OTHER_WRITABLE)
st    Directory with the sticky bit set (+t) and not other-writable (STICKY)
ex    Executable file (i.e. has ‘x’ set in permissions) (EXEC)
mi    Non-existent file pointed to by a symbolic link
      (visible when you type ls -l) (MISSING)
lc    Opening terminal code (LEFTCODE, LEFT)
rc    Closing terminal code (RIGHTCODE, RIGHT)
ec    Non-filename text (ENDCODE, END)

Directory rengi mavi, ve .jpg'leri siyah zemin üzerine bold mavi:

export LS_COLORS="di=00;34:*.jpg=01;34;40"

Aktif kullandığım LS_COLORS değişkenim:

export LS_COLORS="\
di=00;34\
:ow=00;97;41\
:ex=01;31\
:ln=04;32\
:or=05;31\
:*.py=32\
:*.rb=100\
:*.php=37\
:*.html=04;94\
:*.js=00;96\
:*.css=00;96\
:*.sass=00;96\
:*.scss=00;96\
:*.gitignore=04;31\
:*.gitmodules=04;31\
:*.gitattributes=04;31\
:*.localized=04;31\
:*.rbenv-version=04;31\
:*.rvmrc=04;31\
:*.nanorc=04;31\
:*.htoprc=04;31\
:*.netrc=04;31\
:*.md=30;106\
:*.markdown=30;106\
:*.DS_Store=07\
:*.tar=04;32\
:*.tgz=04;32\
:*.zip=04;32\
:*.rar=04;32\
:*.sfv=04;32\
:*.jpg=01;34;40\
:*.png=01;34;40\
:*.gif=01;34;40\
:*.mkv=01;34;44\
:*.avi=01;34;44\
:*.mov=01;34;44\
:*.mp4=01;34;44\
:*.flv=01;34;44\
:*.pdf=04;33\
:*.txt=37\
:*.csv=37\
:*.json=37\
:*.sh=37;40\
:*.bash_history=00;90\
:*.gdb_history=00;90\
:*.irb_history=00;90\
:*.mysql_history=00;90\
:*.php_history=00;90\
:*.psql_history=00;90\
:*.lesshst=00;90\
"

Bash Completion

 # defaults read|write NSGlobalDomain için tab-completion yapalım
complete -W "NSGlobalDomain" defaults

 # killall komutu için tab-completion yapalım
complete -o "nospace" -W "Finder Dock Mail Safari iTunes iCal Address\ Book SystemUIServer" killall

* Bu konu daha sonra detaylı olarak işlenecek...

File İşlemleri

basename, dirname, print

basename ~/Desktop/og-devpod.txt      # og-devpod.txt
basename ~/Desktop/og-devpod.txt .txt # og-devpod
dirname ~/Desktop/og-devpod.txt       # /Users/vigo/Desktop

lpr /etc/hosts # printer'a yolla

input, output ve pipe

$ some command < infile
ls > outfile               # Create/overwrite outfile
ls >> outfile              # Append to outfile
ls 2> errorfile            # Create/overwrite outfile
ls > outfile 2> errorfile  # Separate files
ls >& outfile              # Single file

ls -1 | cut -d. -f2 | sort # sort by file extension (pipe)

Komutların sırayla ya da bağlantılı çalışması olayı...

# eğer biri patlarsa diğeri devam eder...
$ command1 ; command2 ; command3

# eğer biri patlarsa diğeri çalışmaz...
$ command1 && command2 && command3

# komutlar bitince sekans durur...
$ command1 || command2 || command3

ls

ls -R # list recursivly
ls -t # order by last mod. date DESC
ls -X # group by extension
ls -S # sort by filesize

ls -al | sort -k 5     # 5.kolona (size) göre sort et
ls -al | sort -k 5 -r  # 5.kolona göre ters sort et
# drwxr-xr-x  5 vigo staff 170 Sep  2 13:30
# |           | |    |     ^   |    | |
# 1           2 3    4     5   6    7 8
 # bulunduğun folder'da e ile başlayan filelar...
 # ls e* gibi
echo e* 

cp, rm, mv, rename

 # - karekteriyle başlayan dosyayı silmek için
 # File: --inside.db
rm ./--inside.db

 # bulk file rename
 # tpz-entrg201.avi => 201.avi
for f in *; do
    n=${f//tpz\-entrg/};
    mv $f $n;
done;

 # tüm .html'leri .markdown yap
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1markdown/'` ; done

 # hızlı backup
cp file.txt{,.backup}

chmod, stat, strings

-rw-r--r-- 1 vigo wheel 0 Aug 30 19:19 file-01.txt
||||||||||
|||||||||+--- Others, Execute    (x) 1 = 2^0
||||||||+---- Others, Write      (w) 2 = 2^1
|||||||+----- Others, Read       (r) 4 = 2^2
||||||+------ Group, Execute     (x) 1 = 2^0
|||||+------- Group, Write       (w) 2 = 2^1
||||+-------- Group, Read        (r) 4 = 2^2
|||+--------- Owner/User Execute (e) 1 = 2^0
||+---------- Owner/User Write   (w) 2 = 2^1
|+----------- Owner/User Read    (r) 4 = 2^2
+------------ Is Directory?      (d)

file-01.txt için:

Owner/User  : Read, Write  => 4 + 2 = 6
Group       : Read         => 4     = 4
Others      : Read         => 4     = 4
----------------------------------------
644 unix file permission

stat

Unix permission'a bakmak:

stat -f '%A' file-01.txt # 664

Dosya hakkında detaylı bilgi:

stat -x file-01.txt

File: "file-01.txt"
Size: 0 FileType: Regular File
Mode: (0666/-rw-rw-rw-) Uid: ( 501/ vigo) Gid: ( 0/ wheel)
Device: 1,2 Inode: 1028284 Links: 1
Access: Sun Sep 2 11:02:30 2012
Modify: Sun Sep 2 11:02:30 2012
Change: Sun Sep 2 11:48:45 2012

strings

strings /bin/ls # obje / binary dosya ya da dosya içinde print edilebilir string'leri bul

chmod, umask

chmod u=rw,g-rx,o-rx file-01.txt
chmod a=w file-01.txt                   # herşeyi uçur, sadece a için set et.
                                        # --w--w--w- 1 vigo wheel 0 Sep  2 11:02 file-01.txt

chmod u+r,g+r,o+r file-01.txt           # ya da chmod +r file-01.txt
                                        # -rw--w-rw- 1 vigo wheel 0 Sep  2 11:02 file-01.txt

chmod -R u=rwx,g=rw,o=r FOLDER/         # recursive

umask                                   # 0022
umask -S                                # u=rwx,g=rx,o=rx

find

sudo find DIR -name '\.DS_Store' -print -delete            # tüm .DS_Store'ları bul print et ve sil!:
find DIR -type f -name .DS_Store -print0 | xargs -0 rm     # xargs'a parametre geçiyoruz -print0 ile

-print ile -print0 farkı

find . -name 'file*' -print

./file-01.txt
./file-02.txt
./file-03.txt
./file-04.txt
./file-05.txt
./file-06.txt
./file-07.txt
./file-08.txt
./file-09.txt

find . -name 'file*' -print0

./file-01.txt ./file-02.txt ./file-03.txt ./file-04.txt ./file-05.txt ./file-06.txt ./file-07.txt ./file-08.txt ./file-09.txt

find . -name '\.DS_Store' -exec svn rm {}                               # .DS_Store'ları sil
find . -type d -name '.svn' -print0 | xargs -0 rm -rdf                  # .svn'leri recursive sil:
find . -type f -name 'test*' -print0 | xargs -0 cat                     # test ile başlayan dosyaları bul ve bunları print et:

svn st | grep ? | awk '{print $2}' | xargs svn add                      # ekli olmayanları zaaaaaaaart diye ekle

find . -name 'test_*' -print0 | xargs -0 -I {} cp -v {} /tmp/tests/     # test_* bunları /tmp/tests/ altına kopyala
find . -name 'models*' -print0 | xargs -0 -I {} mv -v {} /tmp/models/   # find models* bunları /tmp/models/ altına mv et ...

find . -mtime -5                                                        # son 5 gün içinde modifiye olan dosyalar.
find . -mtime +5                                                        # 5 gün önceden önce [before 5 days ago]
find -type d                                                            # directory'leri bul
find -type l                                                            # symlink'leri bul

Programlama

Shell script'in başında mutlaka:

set -e

olmalı. Bu Silent Error olayını kapatıyor.

test=1                      # DEĞİŞKEN=DEĞER
echo $test
echo "${test}"
echo {a,b,c}1 # a1 b1 c1

echo "I live in \$HOME"     # I live in $HOME
                            # $'ı escape etmek
unset test                  # değişkeni uçur / yoket

deneme()
{
    echo "bu deneme fonksiyonu"
}
unset -f deneme             # fonksiyonu uçur, eğer ayn isimde
                            # değişken varsa onu da uçur

Execute Script

Test için dosya oluştur:

cd /tmp
touch test_script.sh
nano test_script.sh

İçine;

 #!/usr/bin/env bash
 set -e
 echo "Hello World"

kntrl+x ve y ve enter ile save et çık...

stat -f '%A' test_script.sh # 644
sh test_script.sh # run

Hello World

chmod +x test_script.sh
stat -f '%A' test_script.sh # 775

./test_script.sh # run

Hello World

For-Loop, While, Until

for FILE in *;do echo "file: ${FILE}"; done
for FILE in $(ls);do echo "file: ${FILE}"; done

 # dummy dosya oluştur.
for number in 1 2 3 4 5 6 7 8 9; do touch "file-0${number}.txt"; done

 # *.avi'leri /target/path/dir altına kopyala
for filename in `find . -name '*.avi' -print`; do cp $filename /target/path/dir; done

for NUMBER in 1 2; do echo "${NUMBER}"; done
while [ expression ]; do
    # statements
done

number=0 # 1'den 100'e
while [ $number -lt 100 ]; do
    number=$((number+1))
    echo $number
done

a=0 # until
until [ $a == 5 ]; do
    echo $a
    a=$((a+1))
done

Array

declare -a TEST_ARRAY
TEST_ARRAY[0]="ali"
TEST_ARRAY[2]="veli"
TEST_ARRAY[3]="vigo"
for item in ${TEST_ARRAY[@]}; do
    echo "${item}"
done
echo "length: ${#TEST_ARRAY}"

 # ali
 # veli
 # vigo
 # length: 3

files=( "/etc/passwd" "/etc/group" "/etc/hosts" )
printf "%s\n" "${files[@]}"

typeset -a test_array # C style key/value-ish loop
test_array=(
    "ali" "veli" 
    "x" "y"
)
test_array_count=${#test_array[*]}
for ((i=0; i<=$((($test_array_count/2)-1)); i++)); do
    echo "index: ${i}, key: ${test_array[(0+($i*2))]}, value: ${test_array[(1+($i*2))]}"
done

Functions

typeset -f # o session'daki tüm fonksiyonları listeler
typeset -F # o session'daki tüm fonksiyon adlarını

function_name(){
    # statements
}

function function_name(){
    # statements
}

Fonksiyonu; function_name arg1 arg2 ag3 şeklinde çağırıyoruz.

function deneme () {
    echo "ilk arg: ${1}"
    echo "last executed: ${?}"
}
deneme aa bb                  # ilk arg: aa
                              # last executed: 0
deneme "aa bb"                # ilk arg: aa bb
                              # last executed: 0
var="vigo"
deneme $var                   # ilk arg: vigo
                              # last executed: 0
deneme "$var"                 # ilk arg: vigo
                              # last executed: 0
deneme '$var'                 # ilk arg: $var
                              # last executed: 0

last executed 0 geldi yani; echo işlemi başarıyla yapıldı ve 0 döndü. Double Quotes içinde geçilen argüman, whitespace gözönüne alınmadan tek argüman olarak işlenir.

Single Quotes tüm özel karakterleri yok eder.

Back Tick içindeki de execute edilir.

files=`ls`
echo $files

Fonksiyonlarda;

  • $0 geçilen ilk komut, genelde komutun kendisi.
  • $1 - $N geçilen argümanlar.
  • $? execute edilen son komutun exit valuesu
  • $* gelen tüm argümanlar
  • $@ gelen tüm argümanlar array olarak

Örnek:

list_args()
{
    echo "Incomings: ${*}"
    echo "Args count: ${#@}"
}

list_args item1 "item 2" 3

Incomings: item1 item 2 3
Args count: 3

Argüman'ın default value'sünü set etmek.

t(){
    port="${1:-8000}"
    echo "port: ${port}"
}
t                         # argümansız 8000
t 1234                    # 1234

Error Handling

 # olmayan bir folder'ı listelemeye çalış ve oluşan hatayı error.txt'ye
 # yaz...
ls hodo 2> error.txt
cat error.txt # örnek
 # gls: cannot access hodo: No such file or directory

 # kara delik
ls hodo > /dev/null 2>&1

Input (stdin)

echo "enter number: "; # kullanıcıdan input al
read number;
echo "your entry is ${number}";

Matematik, 4 İşlem

typeset -i resulta # result integer olsun
typeset -i resultb
a=100;
b=200;
resulta=a+b;
resultb=b-a;
echo $resulta; # 300
echo $resultb; # 100
typeset -i resulta
typeset -i resultb
a=6;
b=3;
resulta=a*b;
resultb=a/b;
echo $resulta; # 18
echo $resultb; # 2

if, else, elif

if [[ condition ]]; then
    # statements
else
    # statements
fi

if [[ condition ]]; then
    # statements
elif [[ condition ]]; then
    # statements
fi

Koşullar: -gt, -lt, -ge, -le, -eq, -ne

if [[ 2 -gt 1 ]]; then # -gt
    echo "2 > 1"
fi
-gt : büyük
-lt : küçük
-ge : büyük eşit
-le : küçük eşit
-eq : eşit
-ne : eşit değil

Koşullar: test Array, -z, -n, ==, !=

TEST_ARRAY=()
if [[ -z $TEST_ARRAY ]]; then
    echo "TEST_ARRAY is empty"
fi
-z : boş dizi
-n : tanımlı dizi
== : eşit
!= : farklı

Koşullar: test File / Folder, -f, -s, -r, -w, -h, -c, -b, d

if [[ -f "/tmp/error.txt" ]]; then
    echo "error.txt exist"
fi

if [[ -x "/tmp/error.txt" ]]; then
    echo "error.txt is executable"
fi

if [[ ! -x "/tmp/error.txt" ]]; then
    echo "error.txt is not executable"
fi
-f : file exists
-s : dosya boş değil
-r : readable
-w : writable
-x : executable
-h : sembolik link
-c : char
-b : blok
-d : directory

Koşullar: bool, and, or

if [[ 1 == 1 && 2 == 2 ]]; then # and
    echo "1 == 1 and 2 == 2"
fi

if [[ 1 == 1 || 1 > 6 ]]; then # or, tek koşul true olsa yeter
    echo "ok"
fi

Koşullar: case

INPUT="h"
case $INPUT in
    h)
        echo "h geldi";;
    o)
        echo "o geldi";;
    *)
        echo "hiç biri..";
        exit 1;;
esac

 # file.sh bash
 # file.sh zsh
 # file.sh hodo

case "$1" in
    bash) echo "bash geldi"
    ;;
    zsh) echo "zsh geldi"
    ;;
    *) echo "non of them";
    exit 1
    ;;
esac

Prompt Shell (PS), ENV, Change Shell (chsh)

$PS1, $PS2 ve hatta bazı durumlarda $PS4'e kadar yolu var...

\t : saat
\d : tarih
\s : shell adı
\w : working directory
\u : kullanıcı adı
\h : makine adı
echo $SHELL    # aktif shell
chsh           # change shell
echo $HOME
env            # tüm environment variable'ları
type ls        # ls is aliased to `gls -h --color --group-directories-first'
printenv       # All variables and their values
printenv HOME  # One variable and its value

Homebrew'dan son versiyon bash kur ve default shell olarak set et:

brew install bash
cat /etc/shells
sudo nano /etc/shells
/usr/local/bin/bash # son satıra ekla
chsh -s /usr/local/bin/bash vigo

cd, push, popd, dirs

pushd FOLDERS    # stack'e at
dirs -v          # list

 # 0 ~/Desktop
 # 1 ~/Library/Application Support
 # 2 ~/Development/Repos/Github
 # 3 ~/Development

cd ~0            # listedeki 0.Folder'a git
cd ~1            # listedeki 0.Folder'a git

popd  # 0 ~/Desktop gitti...
dirs -v
 # 1 ~/Library/Application Support
 # 2 ~/Development/Repos/Github
 # 3 ~/Development

Current Folder herzaman listedeki 0. eleman...

patch, diff

patch -p1 < FILE.diff
diff -rq /path/dir1/ /path/dir2/ # iki folder arasında diff
diff -u file-orignal file-modified

du, df

du -d 0 -h # bulunduğun dizin kaç k?
df -h      # free disk space

man

man php | col -b > man-php.txt           # man to txt
man -t UNIX_COMMAND | open -f -a preview # man 2 pdf
man 5 -t nanorc | open -f -a preview     # preview'da aç...

iconv

iconv < default.asp -f CP1254 -t UTF-8                     # windows 1254 encoding'li eski asp sayfayı UTF-8'e çevir
iconv < default.asp -f CP1254 -t UTF-8 > default-utf8.asp  # ve çıkışı yeni dosya olarak kaydet

for f in *.txt; do iconv -f mac -t utf-8 "${f}" > "${f}.utf8"; done

History, Calendar, Tasks, Jobs

history
history 5         # son 5
history -c        # delete history *sakat*
!!                # son komutu run et
!N                # history'deki N numarayı run et
!-N               # -N komut öncekini run et
!$                # önceki run edilen komuttan parametreyi al
!*                # önceki run edilen komuttan tüm paramları al

cal apr 2012      # calendar Nisan 2012

!$, !*

ls a*             # a ile başlayanları listele
rm !$             # bir önceki param yani `a*`
                  # yani bu: `rm a*` oldu...

ls j* s*          # j ve s ile başlayan file'ları listele
wc !*             # `wc j* s*` oldu...

jobs, fg, bg, background process

jobs              # list jobs
&                 # run job in the background
^Z                # suspend the current (fg) job. kntrl + z

sleep 20          # kntrl+z'ye bas
suspend           # suspend a shell
fg                # unsuspend a job: bring it into foreground
bg                # make a suspended job run in the background

fg %2             # bg %jobnumber
fg %1             # fg %jobnumber 

sort big_file.txt > sorted_big_file.txt & # arka plana at...

Crontab

Eğer * olursa bu her anlamındadır... Örneğin saat kısmına * girilirse bu her saat anlamındadır. Ay için yine İngilizce january, february

# dakika (0-59)
# saat (0-23)
# gün (1-31)
# ay (1-12)
# haftanın günü (0-6)

# hergün 06:40'da updatedb'yi run et!
40 06 * * * updatedb 

# her pazartesi saat 1'de /tmp altındaki 3 günden eski dosyaları sil!
0 1 * * mon find /tmp -atime 3 -exec rm -f {} \;

# her ayın 9,19 ve 29'unda /tmp altındaki 3 günden eski dosyaları sil!
0 1 9,19,29 * * /tmp -atime 3 -exec rm -f {} \;

# iki ayda bir çalıştırma
# ay'daki */2 = iki ayda bir! yani zaman dilimi bölü 2
0 2 1 */2 * find /tmp -atime 3 -exec rm -f {} \;
sudo crontab -e

Text İşlemleri

sed, awk

İçinde imaj dosyları olan bir folder:

ls --color=no | sed 's/.*/<img src="&" alt="&"\/>/'

<img src="b_black.png" alt="b_black.png"/>
<img src="header.png" alt="header.png"/>
<img src="keyboard.png" alt="keyboard.png"/>
<img src="teaser.png" alt="teaser.png"/>
<img src="wallpaper.jpg" alt="wallpaper.jpg"/>

cat ~/.ssh/config | awk '/^Host [^*]/ {print $2}' # sadece Host'ları print et
echo hello.txt | awk -F "." '{ print $1 }'        # sadece file'ın adı... hello
sed /^$/d                                         # clear blank lines
ps -caxm -orss,comm | awk '$1 ~ /^[0-9]+$/ {print $1}{sum += $1} END {print sum}'

find, replace, cut, grep, xargs, fgrep

str="Hello World"                      # ${string//find/replace_width}
echo ${str//ello/acking the}           # Hacking the World

 # using perl
perl -pi -e 's/FIND/REPLACE/gi' *.txt  # ignore case
perl -pi -e 's/FIND/REPLACE/g' *.txt   # case-sens

 # -d delimiter
 # -f index... 1 indexed....
echo "user:[email protected]" | cut -d: -f1  # user
echo "user:[email protected]" | cut -d: -f2  # [email protected]

file-01.txt file-02.txt file-03.txt file-04.txt file-05.txt file-06.txt file-07.txt file-08.txt file-09.txt

ls | grep file-0[2-5]
ls | grep file-0[2-5] | xargs -n 1 echo "this is"
ls | grep file-0[2-5] | xargs -n 2 echo "these are"
ls | grep file-0[2-3] | xargs -n 1 rm

git st | grep -e 'by [[:digit:]]* commit'

git st | grep -oh 'by [[:digit:]]* commit' # sadece match döner...

fgrep -i -n --color "kelime" file.txt    # file.txt içinde "kelime" ara
fgrep -i -n -r "ffmpeg" .                # recursive tüm dosyalarda ara:
fgrep --include=*.php --exclude=*.html   # html ve php'lerden oluşan bir folder'da sadece php'lerde ara...

gem list --no-versions | xargs gem uninstall -aIx # tüm ruby gem'lerini sil

more, less

more < data.txt

read, wc, cat, head, tail, nl, od, xxd, wc, paste

cat -n file.txt        # line numbers
cat -nb file.txt       # line numbers, skip blanks

head file.txt          # first 10 lines
head -N file.txt       # first N lines
head -n N file.txt     # first N lines
head -c N file.txt     # first N bytes

tail file.txt          # last 10 lines
tail -N file.txt       # last N lines
tail -n N file.txt     # last N lines
tail -n +N file.txt    # all lines except the first N
tail -c N              # last N bytes
tail -f file.txt       # keep file open

nl -n ln file.txt      # line numbering filter
nl -n ln -v 5 file.txt
nl -n ln -v 5 -i 3 file.txt
nl -n ln -v 5 -i 3 -s "line: " file.txt


od file.txt            # octal dump
od -tc file.txt        # dump text
od -N 10 file.txt      # first 10 bytes
od -j 10 file.txt      # start from 10
od -A x file.txt       # hex

xxd file.txt           # hex dump, hex *süper*
xxd -l 2 file.txt      # ilk 2 byte
xxd -c 32 file.txt     # 32 bytes per row
xxd -g 1 file.txt      # c64 style... bytler'ı grupla...
xxd -i file.txt        # c array

wc file.txt            # word count, bytes, words and lines
wc -l file.txt         # lines
wc -w file.txt         # words
wc -c file.txt         # bytes

 # paste
 # iki dosya olsun: letters ve numbers
cat letters
 # A
 # B
 # C

cat numbers
 # 1
 # 2
 # 3
 # 4
 # 5

paste numbers letters
 # 1 A
 # 2 B
 # 3 C
 # 4
 # 5

paste letters numbers
 # A 1
 # B 2
 # C 3
 #   4
 #   5

paste -s numbers letter
 # 1  2  3  4  5
 # A  B  C

 # -d: delimiter
paste -d : numbers letter
 # 1:A
 # 2:B
 # 3:C
 # 4
 # 5

Dosyayı oku ve satırları say:

x=0; cat file.txt | while read line; do x=$(($x+1)); echo $x; done
wc -l file.txt

Dosyayı satır satır oku

cat file.txt | while read line; do echo $line; done
cat file.txt | while read line; do ping -c 2 $line; done # ip listesini pingle...

Compress, rar, unrar, zip, unzip, tar, gzip, split, join

rar a -m0 -ep -v2M deneme FILE.dmg                   # deneme.part şeklinde 2 Meglik rar'a böler:
rar a -m0 -ep -v2M -vn deneme FILE.dmg               # deneme.r00 şeklinde 2 Meglik rar'a böler:

unrar x FILE.rar ~/Desktop/                          # unrar
unrar x -ep -or FILE.RAR ~/Desktop/
zip -r TARGET.zip /path/to/folder                    # bir folder'ı komple zip'le
zip -r ~/Desktop/proje.zip .                         # bulunduğun folder'ı, Desktop'a "proje.zip" diye paketle:

zip ARCHIVE.zip file1.txt file2.txt                  # file1.txt ve file2.txt'yi ARCHIVE.zip olarak ziple

zip -e protected.zip /file/to/protect/               # şifreli zip

unzip -o \*.zip -d /tmp/deneme                       # tüm .zip'leri /tmp/deneme/ altına aç
tar czv big_file | split -b 3k - big_file_parts.     # dosyayı 3k'lık parçalara böl

-rw-r--r-- 1 vigo wheel 14K Sep 2 14:21 big_file
-rw-r--r-- 1 vigo wheel 3.0K Sep 2 14:29 big_file_parts.aa
-rw-r--r-- 1 vigo wheel 3.0K Sep 2 14:29 big_file_parts.ab
-rw-r--r-- 1 vigo wheel 3.0K Sep 2 14:29 big_file_parts.ac
-rw-r--r-- 1 vigo wheel 1.0K Sep 2 14:29 big_file_parts.ad

cat big_file_parts.* | tar xvz                             # parçalara böldüğünü join et
tar czv FOLDER | split -b 100m - FOLDER.tar.gz.            # FOLDER'ı 100 MB'lik parçalara böl
cat FOLDER.tar.gz.* | tar xvz                              # join
tar cvjf archive_name.tar.bz2 /path/to/compress/folder/
tar -cf output.tar folder/                                 # folder'ı tar'la
tar -xf output.tar                                         #
tar -rf output.tar ekleme.txt                              # ekleme.txt dosyasını paketlenen tar'a ekle
tar -tf output.tar                                         # tar'ın içini listele

 # mydir/'ı komple tar'la
 # -c: create
 # -v: verbose
 # -f: define archive filename > mydir.tar
tar -cvf mydir.tar mydir/
tar xvzf archive.tgz -C /path/to/extract                   # tar'ı istediğin bir yere aç
tar cvzf path/to/tgz -C source/folder .                    # path'i kullanmadan sadece source/folder altını tar'la
gzip FILENAME

gzip -d FILENAME                # unzip
tar -cvf ARCHIVE.tar FOLDER/    # tar | gzip
gzip ARCHIVE.TAR

cat FILE | wc -c                # dosyayı gzip'le sıkıştır ve size'ı gör...
                                # önce normal hali

gzip -c FILE | wc -c            # gzip'li hali

date

Format:

%%   a literal %
%a   locale's abbreviated weekday name (e.g., Sun)
%A   locale's full weekday name (e.g., Sunday)
%b   locale's abbreviated month name (e.g., Jan)
%B   locale's full month name (e.g., January)
%c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%C   century; like %Y, except omit last two digits (e.g., 21)
%d   day of month (e.g, 01)
%D   date; same as %m/%d/%y
%e   day of month, space padded; same as %_d
%F   full date; same as %Y-%m-%d
%g   last two digits of year of ISO week number (see %G)
%G   year of ISO week number (see %V); normally useful only with %V
%h   same as %b
%H   hour (00..23)
%I   hour (01..12)
%j   day of year (001..366)
%k   hour ( 0..23)
%l   hour ( 1..12)
%m   month (01..12)
%M   minute (00..59)
%n   a newline
%N   nanoseconds (000000000..999999999)
%p   locale's equivalent of either AM or PM; blank if not known
%P   like %p, but lower case
%r   locale's 12-hour clock time (e.g., 11:11:04 PM)
%R   24-hour hour and minute; same as %H:%M
%s   seconds since 1970-01-01 00:00:00 UTC
%S   second (00..60)
%t   a tab
%T   time; same as %H:%M:%S
%u   day of week (1..7); 1 is Monday
%U   week number of year, with Sunday as first day of week (00..53)
%V   ISO week number, with Monday as first day of week (01..53)
%w   day of week (0..6); 0 is Sunday
%W   week number of year, with Monday as first day of week (00..53)
%x   locale's date representation (e.g., 12/31/99)
%X   locale's time representation (e.g., 23:13:48)
%y   last two digits of year (00..99)
%Y   year
%z   +hhmm numeric timezone (e.g., -0400)
%:z  +hh:mm numeric timezone (e.g., -04:00)
%::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z   alphabetic time zone abbreviation (e.g., EDT)
date +%Y                                              # 2012
date +%a-%A                                           # Tue-Tuesday
date +%Y-%m-%d                                        # 2012-11-13
TZ='Europe/Istanbul' date +%Y-%m-%d-%H-%M             # 2013-02-28-12-47
TZ='America/Los_Angeles' date +%Y-%m-%d-%H-%M         # 2013-02-28-02-48
date --date='TZ="America/Los_Angeles" 09:00 next Fri' # Sadece Linux / Ubuntu

OSX'e Özel

dscacheutil -flushcache                          # flush DNS

X -query MAKİNENİN_IP_ADRESİ                     # Linux'a Remote Desktop Connection

screencapture -t jpg ~/Desktop/file_name.jpg     # grab screen

lsof -Pan -i tcp -i udp                          # OS'un dinlediği portlar

pkgutil --expand sample.pkg ~/Desktop/           # pkg'yi aç...

Unix'e Özel

mail -s "subject" unix_user < file.txt          # file.txt'yi unix_user'a yolla... 

Network / Remote

whois, dig

whois ozyilmazel.com

dig TXT vigodome.com   # TXT record 
dig A vigodome.com     # A record
dig MX vigodome.com    # MX record
dig example.com +nostats +nocomments +nocmd
dig www.example.com +nostats +nocomments +nocmd

ssh, ssh proxy, sftp, scp

ssh-keygen -t dsa                        # key oluştur
cat ~/.ssh/id_dsa.pub                    # bu public key... bunu uzakta ~/.ssh/authorized_keys içine yaz...

ssh-keygen -t rsa -C "[email protected]"

cat <(ssh user@host cat file.txt)        # uzaktaki makineden locale print
cat <(ssh user@host ls)                  # uzaktan ls

sftp user@host
ls
put UPLOAD
get DOWLOAD

scp -r SOURSE/ user@host:/target/path/to

proxy, tsocks

ssh -N HOST -D PORT # ssh proxy

brew tap adamv/alt  # Homebrew, tsocks'un kurulu olması lazım
brew install tsocks

sudo port install tsocks

 # tsocks.conf'da
 # tordns_enable = false olmalı

tsocks KOMUT

tsocks links http://ugur.ozyilmazel.com/test.php # tsocks proxy ile links üzerinden url call

rsync

rsync -vaP --delete user@host:/source/ ~/Desktop/source/ # uzaktaki /source/ ile ~/Desktop/source/'u sync'le

 # local'de, source'u targert'a sync et...
 # eğer target'da olup, source'da olmaz ise otomatik olarak
 # target'da ki o fazladan olan dosya silinir.
 # yani herzaman origin olan source oluyor -vaP --delete ile
rsync -vaP --delete ~/source/ ~/target/

tree rs_source/ rs_dest/
rs_source/
├── f1
└── f2
rs_dest/
├── f1
├── f2
└── f33
rsync -vaP --delete rs_source/ rs_dest/
tree rs_source/ rs_dest/
rs_source/
├── f1
└── f2
rs_dest/
├── f1
└── f2

# f33 gitti!
# gitmemesi için --delete dememek lazım ama bu da sync mantığına ters!

netcat

nc -l PORT_NUMBER           # terminal style peer2peer:
nc -l 1234                  # 1234.portu dinle

nc IP PORT_NUMBER
nc 10.20.6.10 1234

nc -l 127.0.0.1 4567        # sonra browser'ı açıp http://127.0.0.1:4567/

nc -v mail.bronxwhq.org 587 # 587.port'u dinle

Paket Yöneticiler

MacPorts

sudo port selfupdate
sudo port -v upgrade outdated

sudo port clean PAKET_ADI
sudo port install PAKET_ADI

port contents PAKET_ADI
sudo port deactivate PAKET_ADI

sudo port clean --all PAKET_ADI # bozuk install edilmiş olabilecek paketi komple sil

sudo port uninstall inactive

sudo port uninstall --follow-dependents PAKET_ADI

sudo port -fp uninstall --follow-dependents installed # Tüm paketleri sil

sudo port select --set python python27 # python 2.7'i default python olarak seç

sudo port install vim +python26 # vim kurulumu
sudo port clean vim
sudo port install vim +python31
sudo port echo installed | grep vim

 # [email protected]_0+python26
 # [email protected]_0+python31

sudo port activate vim @7.3.21_0+python26

 # ya da
sudo port activate vim @7.3.21_0+python31


port search sane # scanner install
sudo port install twain-sane

scanimage -L # kullanım
scanimage -h

scanimage -v -p --format tiff --mode color --resolution 150 --custom-gamma=yes > test150-2.tiff

scanimage -v -p --format tiff --mode color --resolution 300 > test300.tiff # güzeldi bu

Homebrew

brew link FORMULA

brew cleanup FORMULA              # if FORMULA has multiple installed versions error

brew cleanup                      # clear older versions

brew rm --force FORMULA
brew uninstall FORMULA
brew unlink FORMULA
brew prune

brew versions FORMULA

brew switch [formula] [version]   # switch to formulas version

brew tap                          # tap
brew tap homebrew/dupes
brew tap josegonzalez/php
brew install php54 --with-mysql

 # zlib
brew tap homebrew/dupes
brew install zlib

LDFLAGS  -L/usr/local/Cellar/zlib/1.2.7/lib # bu doc'u yazdığım sıradaki son versiyon zlib:
CPPFLAGS -I/usr/local/Cellar/zlib/1.2.7/include

Bazı Tool'lar ve Kullanımları

cloc

brew install cloc

Code Counter, kaç satır kod yazılmış saymak için...

cd ~/Dotfiles
cloc .

imagemagick

İlgili makale

brew install imagemagick
convert -resize 900x -loop 0 -delay 10 img01.jpg img02.jpg img03.jpg animated.gif

youtube-dl

Önce https://github.com/rg3/youtube-dl/downloads adresinden son versiyonu indir. brewdeki sıkıntılı... https://github.com/downloads/rg3/youtube-dl/youtube-dl.2012.12.11.tar.gz

tar xvzf youtube-dl.2012.12.11.tar.gz
python setup.py install # tüm sistemde kullanmak için...

youtube-dl --list-formats http://www.youtube.com/watch?v=GxL9MnWlCwo # formatları listeler

  # 22  :  mp4   [720x1280]
  # 45  :  webm  [720x1280]
  # 35  :  flv   [480x854]
  # 44  :  webm  [480x854]
  # 34  :  flv   [360x640]
  # 18  :  mp4   [360x640]
  # 43  :  webm  [360x640]
  # 5   :  flv   [240x400]
  # 17  :  mp4   [144x176]

youtube-dl -o "%(title)s.%(ext)s" --restrict-filenames -f 22 http://www.youtube.com/watch?v=GxL9MnWlCwo

mate

mate `find . -name 'models.py' -print | xargs` # open models.py in projects
find . -name '*.html' | xargs mate # proje içindeki tüm html'leri aç

bchunk

bchunk myinputfile.bin myinputfile.cue myoutputfile.iso # cue/bin'den iso

nano

nano +linenumber filename

apache

htpasswd -c /etc/apacheusers # basic auth için yeni kullanıcı oluştur

wget

    wget --http-user=USER --http-password=PASS http://URL
    wget -c http://URL # resume

curl

http://www.cs.sunysb.edu/documentation/curl/index.html
http://curl.haxx.se/docs/httpscripting.html
http://curl.haxx.se/
curl http://URL # outputs to shell
curl ftp://URL/FILE
curl URL URL # fetch 2 doc at once
curl -o localfile URL # curl -o mytext.txt http://google.com
curl -O URL # curl -O http://site.com/index.php
curl ftp://name:[email protected]:port/full/path/to/file
curl -u name:passwd ftp://machine.domain:port/full/path/to/file
curl -u name:passwd http://machine.domain/full/path/to/file
curl -x my-proxy:888 ftp://ftp.leachsite.com/README
curl -u user:passwd -x my-proxy:888 http://www.get.this/
curl -U user:passwd -x my-proxy:888 http://www.get.this/
curl -r 0-99 http://www.get.this/ # first 100bytes
curl -r -500 http://www.get.this/ # last 500bytes
curl -r 0-99 ftp://www.get.this/README # first 100bytes via ftp

curl -T - ftp://ftp.upload.com/myfile # upload all
curl -T uploadfile -u user:passwd ftp://ftp.upload.com/myfile
curl -T uploadfile -u user:passwd ftp://ftp.upload.com/
curl -T localfile -a ftp://ftp.upload.com/remotefile # append
curl --proxytunnel -x proxy:port -T localfile ftp ftp.upload.com
curl -T - http://www.upload.com/myfile # http upload

curl -v ftp://ftp.upload.com/ # verbose debug
curl --trace trace.txt www.haxx.se
curl --dump-header headers.txt curl.haxx.se/

curl -d "var1=data1&var2=data2" http://URL # post to html form

curl -F "[email protected];type=image/gif,file2.txt,file3.html" http://URL # post + upload

curl -F "[email protected]"
-F "yourname=Daniel"
-F "filedescription=Cool text file with cool text inside" http://URL

curl -F "[email protected],cat.gif"
curl -F "[email protected]" -F "[email protected]"

curl -A 'Mozilla/3.0 (Win95; I)' http://www.nationsbank.com/ # user agent

curl -b "name=Daniel" www.sillypage.com # cookie
curl -b headers www.example.com
curl -c cookies.txt www.example.com
curl -L -b empty.txt www.example.com

 # Download, extract and open TextMate Plugin
curl -s -L https://github.com/downloads/jezdez/textmate-missingdrawer/\
    MissingDrawer-0.4.0.tmplugin.tgz | 
    tar -xzf - && open MissingDrawer.tmplugin

mkvmerge

mkvmerge --split size:1024m file.mkv -o parts.mkv # mkv'yi split 1 gig'lik parçalara böl

flac

flac -c -d INPUT.flac | lame --preset insane - OUT.mp3 # flac'ı mp3'e çevir

Kısa Yollar (Tuş Kombinasyonları)

Terminal ekranında ve genel shell ortamında işe yarar

kntrl+x + kntrl+e : Text editör açar.

kntrl+a : Cursor satır başına

kntrl+e : Cursor satır sonuna

kntrl+p : History'de sondan başa doğru

Config / bash_profile

Ruby

export RUBY_HEAP_MIN_SLOTS=1000000
export RUBY_HEAP_SLOTS_INCREMENT=1000000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=1000000000
export RUBY_HEAP_FREE_MIN=500000

Kullanılabilitesi olan fonksiyonlar

Web'de dolaşırken bulduğum bana ait olmayan faydalı olabileceğini düşündüğüm fonksiyonlar.

Start an HTTP server from a directory, optionally specifying the port

function server() {
    local port="${1:-8000}"
    open "http://localhost:${port}/"
    # Set the default Content-Type to `text/plain` instead of `application/octet-stream`
    # And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
    python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}

Get gzipped file size

function gz() {
    echo "orig size (bytes): "
    cat "$1" | wc -c
    echo "gzipped size (bytes): "
    gzip -c "$1" | wc -c
}

Create a new directory and enter it

function md() {
    mkdir -p "$@" && cd "$@"
}

Create a data URL from an image (works for other file types too, if you tweak the Content-Type afterwards)

dataurl() {
    echo "data:image/${1##*.};base64,$(openssl base64 -in "$1")" | tr -d '\n'
}

Gzip-enabled curl

function gurl() {
    curl -sH "Accept-Encoding: gzip" "$@" | gunzip
}

Syntax-highlight JSON strings or files

function json() {
    if [ -p /dev/stdin ]; then
        # piping, e.g. `echo '{"foo":42}' | json`
        python -mjson.tool | pygmentize -l javascript
    else
        # e.g. `json '{"foo":42}'`
        python -mjson.tool <<< "$*" | pygmentize -l javascript
    fi
}

Escape UTF-8 characters into their 3-byte format

function escape() {
    printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
    echo # newline
}

Decode \x{ABCD}-style Unicode escape sequences

function unidecode() {
    perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
    echo # newline
}

Get a character’s Unicode code point

function codepoint() {
    perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
    echo # newline
}

Misc Tricks

echo `python --version 2>&1` | awk '{ print $2 }' # Python'un versiyonu göster

ruby -c file.rb # Ruby syntax'ı kontrol et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment