Posts

Showing posts from 2011
df -k | sed 1d | awk '$5>70 {print}'

umask/getfacl

bash-3.00# umask 222 -r--r--r-- 1 root root 0 Apr 28 00:58 ff3 bash-3.00# umask 555 --w--w--w- 1 root root 0 Apr 28 00:58 ff3 getfacl file2

profile

one of the main places for putting your personalized tweaks is the ~/.profile When a new interactive shell is started, /etc/profile , followed by /etc/bash.bashrc (if bash shell), ~/.profile , and finally ~/.bashrc are executed in that order. It is best to put generic settings in ~/.profile and then bash-specific settings in ~/.bashrc SOLARIS: $HOME/.profile - primary user initialization files read at login The login shell executes the .profile file when you log in. You can customize environment variables and terminal settings in the .profile file to modify your working environment ~/.kshrc file contains shell variables and aliases - it is ENV var /bin/sh - shell path name /bin/ksh - shell path name /etc/.login file is a separate. This file sets up tasks that the Korn shell executes for every user who logs in If you want the changes to take effect immediately, you can source the .cshrc file and the .login file by using the source command

bash

bash-3.00# cat sss for i in {1..6} do touch $i.txt done -rw-r--r-- 1 root root 0 Apr 17 01:56 1.txt -rw-r--r-- 1 root root 0 Apr 17 01:56 2.txt -rw-r--r-- 1 root root 0 Apr 17 01:56 3.txt -rw-r--r-- 1 root root 0 Apr 17 01:56 4.txt -rw-r--r-- 1 root root 0 Apr 17 01:56 5.txt -rw-r--r-- 1 root root 0 Apr 17 01:56 6.txt

AWK

awk 'BEGIN {statements} {statements} END {statements}' bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt 15 bash-3.00# wc -l a.txt 14 a.txt bash-3.00# who root console Apr 10 15:17 (:0) root pts/3 Apr 10 15:20 (:0.0) bash-3.00# who >> a.txt bash-3.00# wc -l a.txt 16 a.txt bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt 17 ------------------ Skip First Two Fields and Print the Rest of Line echo 'This is a test' | awk '{print substr($0, index($0,$3))}' a test ------------------- Select columns 1 and 5: cat a | awk '{print $1" "$5}' select line 3 cat a | awk 'FNR==3{print}' Print column with awk  ps aeu | awk '{print $2}' ----------------------- bash-3.00# echo -e 'test1\ntest2' | awk 'BEGIN{print"START"}{print} END{print"End"}' START test1 test2 End ---------------- Select 2 lines

sed

bash-3.00# test=hello bash-3.00# echo hello world | sed 's/$test/HELLO/' hello world bash-3.00# echo hello world | sed "s/$test/HELLO/" HELLO world bash-3.00# echo '$test' $test bash-3.00# echo $test hello bash-3.00# echo "$test" hello ----------------- bash-3.00# sed 's/root/ROOT/' a.txt ROOT console Apr 10 15:17 (:0) ROOT pts/3 Apr 10 15:20 (:0.0) bash-3.00# sed 's:root:ROOT:' a.txt ROOT console Apr 10 15:17 (:0) ROOT pts/3 Apr 10 15:20 (:0.0) ------------------- [user@8126ef369b1b test]$ sed 's/:/\t/g' /etc/passwd > out                                                                 [user@8126ef369b1b test]$ cat out                                                                                          root    x       0       0       root    /root   /bin/bash                                                                  bin     

echo

bash-3.00# echo -e '123\n456' 123 456 bash-3.00# echo -e 'test1\ntest2\n\ntest3\n' test1 test2 test3 bash-3.00# echo -e 'test1\ntest2\n\ntest3\n' | sed '/^$/d' test1 test2 test3 --------------- bash-3.00# echo -e '123\n456' 123 456 bash-3.00# echo '123\n456' 123\n456

grep -A -B | ggrep | grep -r

greo -A -B line after before grep -- -1000 # grep negative value ---------------- bash-3.00# grep 15:[ 12 ][ 70 ] f[ 123 ].txt f1.txt:root console Apr 10 15:17 (:0) f1.txt:root pts/3 Apr 10 15:20 (:0.0) f2.txt:root console Apr 10 15:17 (:0) f2.txt:root pts/3 Apr 10 15:20 (:0.0) f3.txt:root console Apr 10 15:17 (:0) f3.txt:root pts/3 Apr 10 15:20 (:0.0) ---------- bash-3.00# grep -l 'text' f* f.txt f1.txt f2.txt f3.txt ----------- * e - go to the end of the current word. * E - go to the end of the current WORD. * b - go to the previous (before) word. * B - go to the previous (before) WORD. * w - go to the next word. * W - go to the next WORD. grep -A <N> "string" FILENAME The following example prints the matched line, along with the 3 lines after it. ================ $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8&
[jJ][pP][Gg] = JPG, jpg, jPg..

sort uniq

sort us -rnk1 reverse sorting for 1 column Sorted output without duplicates cat names | sort | uniq  ------------------- bash-3.00# sort -n s.txt 1.Apple 2.Orange 3.TOmato 4.Potatoes 5.Carrot bash-3.00# sort -r s.txt 5.Carrot 4.Potatoes 3.TOmato 2.Orange 1.Apple bash-3.00# cat s.txt 1.Apple 2.Orange 3.TOmato 4.Potatoes 5.Carrot 1.Applie 2.Orange 5.Carrot bash-3.00# sort s.txt | uniq 1.Apple 1.Applie 2.Orange 3.TOmato 4.Potatoes 5.Carrot bash-3.00# sort s.txt | uniq -u 1.Apple 1.Applie 3.TOmato 4.Potatoes ---------------- sort file1 | uniq -d найти 2 одинаковые строки ---------------------- $ cat happybirthday.txt Happy Birthday to You! Happy Birthday to You! Happy Birthday Dear Tux! Happy Birthday to You! $ sort happybirthday.txt Happy Birthday Dear Tux! Happy Birthday to You! Happy Birthday to You! Happy Birthday to You! $ sort happybirthday.txt | uniq Happy Birthday Dear Tux! Happy Birthday to You! $ sort happ

xargs

bash-3.00# cat f.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 bash-3.00# cat f.txt | xargs -n 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ---------------- echo "helloXhelloXhello" helloXhelloXhello ---------------- echo "helloXhelloXhello" | xargs -d X -n 2 hello hello hello ---------------- echo 12345 | tr '0-9' '987654321' 87654 echo 87654 | tr '987654321' '0-9' 12345

bash

A shell script is a text file that typically begins with a shebang , as follows: #!/bin/bash For any scripting language in a Linux environment, a script starts with a special line called shebang. Shebang is a line for which #! # id uid=0(root) gid=0(root) # su oracle # id uid=100(oracle) gid=100(oinstall) # # # su - oracle Password: Sun Microsystems Inc. SunOS 5.10 Generic January 2005 -bash-3.00 $ --------------------- bash-3.00# echo this is a test line > input.txt bash-3.00# exec 3< input.txt #now we can use descriptor bash-3.00# cat <&3 this is a test line ------------------- bash-3.00# echo $0 bash bash-3.00# ls + 2 >out.txt bash-3.00# ls out.txt bash-3.00# cat out.txt +: No such file or directory bash-3.00# ls + +: No such file or directory bash: ./sc.sh: Permission denied bash-3.00# bash ./sc.sh Sun Apr 10 16:11:29 MSD 2011 test bash-3.00# bash -x ./sc.sh + date Sun Apr 10 16:11:41 MSD 2011 + sleep 2 + ech
Printing colored output Producing colored output on the terminal is very interesting stuff. We produce colored output using escape sequences. Color codes are used to represent each color. For example, reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, and white=37. In order to print colored text, enter the following: echo -e "\e[1;31m This is red text \e[0m" Here \e[1;31 is the escape string that sets the color to red and \e[0m resets the color back. Replace 31 with the required color code. For a colored background, reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47, are the color code that are commonly used. In order to print a colored background, enter the following: echo -e "\e[1;42m Green Background \e[0m"
Cat –n filename | grep rownumber

vi

Vi show column number :set number :set nonumber :%s/old/new/gc  - replace all occurences in file with confirmation /w search forward for w :s/(/[/  # replace only the current line from ( to [ r  filename   -  insert content of  filename  below cursor ----------- Vi + file – open at last line Vi +N file – open at N line 0, $ - first, last position of the file w,b forward, backward word HML – top, middle, last line of the screen Ctrl+U, Ctrl+D - scroll down, up one half of screen /pattern – search forward for pattern ?pattern – search backward for pattern N,n – repeat last search in same, opposite direction :11 – move to line 11 in file Insert: Insert text after and before the cursor a, i o,O – open new line for text below, above cursor cc – change current line dd – delete line dw – delete word p,P – put deleted text after, before cursor u – undo last edit J – join 2 lines :30,60w newfile - write from lines 30 to 60