Linux TIPS

Sed & Awk About

Sed delete dos CR/LF line

sed 's/.$//' file > out 
sed 's/\x0D$//' file > out

To remove all whitespace (including tabs) from left to first word

echo "     This is a test" | sed -e 's/^[ \t]*//'
sed  '/\#/d;/^$/d' /etc/nginx/nginx.conf

sed replace spaces with single space

sed -e "s/ \+/ /g;/^$/d" < ml.txt  > mll.txt
sed 's/ \{1,\}/ /g' 

http://superuser.com/questions/241018/how-to-replace-multiple-spaces-by-one-tab

remove all unwanted blank space then SORT it

sed 's/ /\n/g' < list  | sort

add/replace within <<<&>>>

# & replaces whatever matches with the given REGEXP.
$ sed 's@^.*$@<<<&>>>@g' path.txt
<<</usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin:/usr/sas/bin>>>
<<</usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin:>>>
<<</opt/omni/lbin:/opt/omni/sbin:/root/bin>>>
$ sed 's@/usr/bin@&/local@g' path.txt
/usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin/local:/opt/omni/bin:
/opt/omni/lbin:/opt/omni/sbin:/root/bin

Find About

把找到的文件复制到另一个地方

find /mnt/sda7/software_linux/ -name "*.exe" | xargs -i cp {} /home/ftp/incoming/
find /mnt/sda7/software_linux/ -name "*.exe" -exec cp '{}' /home/ftp/ ';'
find cacti-xxxx -type d -exec mkdir -p utf/{} ';'
find cacti-xxxx -type f -exec iconv -f GB2312 -t UTF-8 {} -o utf/{} ';'
find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents 

Find out all the jpg images and archive it

find /var/log -name '*.log' | tar cv --files-from=- | bzip2 -9 > log.tar.bz2 
find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

find /mnt/data/media/* -maxdepth 0 -type d -exec ln -sv '{}' . \;

把目录下的包含大写字母的文件列出来

find /usr/share/ -type f | xargs ls -l | grep '[:upper:]' | less -r
find . -maxdepth 2 -size +100M -exec ls -lh "{}" \;

把目录下所有iso文件找出来并计算总大小(GB)

find /srv/ /mnt/media/ -name "*.iso" | xargs ls -lk | echo `awk '{a+=$5}END {print a}'`/1024/1024 | bc
find /srv/ /mnt/media/ -name "*.iso" | xargs ls -lk | awk '{a+=$5}END {print "Total sum is", a/1024/1024"GB"}'
find /srv/ /mnt/media/ -name "*.iso" -printf "%s\n" 2>/dev/null | awk '{a+=$1}END { print "Total sum is", a/1024/1024/1024"GB\nTotal time is :"}'

== Find files out then Copy files with sequence number ===

find . -name "*tgz" -type f -exec ls -Sr '{}' \; |awk '{system("cp -v "$0" "NR)}'

查找20100401时更改过的文件

cat >> 20100401.sh <<"EOF"
FILE=`find ./ -name "*.tex"`
ls -l --full-time $FILE 2>/dev/null
grep "2010-04-01"
EOF

Niubility Tool Netcat

Transfer file(s)

server: $ tar -cf - /data | nc -l -p 6666
client: $ nc 192.168.2.111 6666 | tar -xf -

server: $ cat file | nc -l -p 6666
client: $ nc 192.168.2.111 6666 > file

server: $ cat /dev/sda | nc -l -p 6666
cliend: $ nc 192.168.2.111 6666 > /dev/sda

As Port Scanner

nc -vv -z 127.0.0.1 8079-8081

history how to use

$ history | grep 'ipt'
2    iptables -L -n -v -t nat

$ !2 # will execute the iptables command * MarkS, yes I know a way to make history contain only uniques. Do the following:

export HISTIGNORE="&"
That will make history not to save duplicate consecutive entries!

some keycodes err will resolve

setkeycodes e016 235
setkeycodes e01e 158
setkeycodes e012 146
setkeycodes e014 148
setkeycodes e015 149
setkeycodes e02d 173
setkeycodes e018 236
setkeycodes e026 238
setkeycodes e017 227
setkeycodes e01f 159
setkeycodes e025 239
setkeycodes e023 237

For my HASEE-F4200

atkbd.c: Unknown key released (translated set 2, code 0xd8 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e058 <keycode>' to make it known.
getkeycodes will show you codes!

then

setkeycodes e058 125

Safe Copying

if [ -f $2 ]
then
        echo "$2 is exists. Do you want to overwrite it? (y/n)"
        read yn
        if [ $yn = "N" || $yn = "n" ]
        then
                exit 0
        fi
fi
cp $1 $2

SMB About

smbmount //192.168.2.51/D memory/ -o iocharset=cp936,user=xxxx,pass= --verbose
smbclient -U user -I 192.168.16.229 -L //smbshare/    # List the shares
mount -t smbfs -o username=winuser //smbserver/myshare /mnt/smbshare
mount -t cifs -o username=winuser,password=winpwd //192.168.16.229/myshare /mnt/share
sudo mount -t cifs -o username=${USER},password=${PASSWORD},uid=<user>,gid=<group> //server-address/folder /mount/path/on/ubuntu
[[http://unix.stackexchange.com/questions/68079/mount-cifs-network-drive-write-permissions-and-chown]]

ss AND netstat && route && ip

ss -r
ip -s
ip maddr
netstat -lptu
netstat -antup

Add route gateway

route add a.b.c.d dev wlan0
ifconfig eth0 128.100.75.111 broadcast 128.100.75.0 netmask 255.255.255.0
route add -net 128.100.75.0 netmask 255.255.255.0
route add default gw 10.7.84.1 dev wlan0

hdparm

hdparm -a 256 -d 1 -r 0 -u 0 -m 2 -c 1 -A 1 -K 0 -P 0 -X 0 -W 0 -S 0 /dev/sda 

will enable DMA ,32bit support, Sector count for multiple sector I/O to 2

If your swapdisk is less than a 1GB, then

dd if=/dev/zero of=/directory/with/much/free/space/tempswap bs=1k count=1000000
chmod 600 tempswap
mke2fs tempswap
mkswap tempswap
swapon tempswap

Writing the actual CD

List content of ISO file

isoinfo -f -R -i cflinux-1.0.iso

type:

cdrecord --scanbus
cdrecord -v speed=<desired writing speed> dev=<path to your writers generic scsi device> boot.iso

Sort about

find src -type f -print0 | sort -z -f | xargs -0 etags --append

Shuffle a list of directories, but preserve the order of files within each directory. For instance, one could use this to generate a music playlist in which albums are shuffled but the songs of each album are played in order.

ls */*.mp3 | sort -t / -k 1,1R -k 2,2
sort -n -k 2 -t : facebook.txt
sort: ls -l | sort -nr +4
cut: cut -f1 -d':' < /etc/passwd        

Shuf about

shuf <<EOF
x
xx
xxx
EOF
shuf -i 1-100
shuf -e clubs hearts diamonds spades

Paste about

     $ cat num2
     1
     2
     $ cat let3
     a
     b
     c
     $ paste num2 let3
     1       a
     2       b
             c
$ paste -d '%_' num2 let3 num2
          1%a_1
          2%b_2
          %c_

Join about

     $ cat file1
     a a1
     c c1
     b b1
     $ cat file2
     a a2
     c c2
     b b2
     $ join file1 file2
     a a1 a2
     c c1 c2
     b b1 b2

urxvt run console application

urxvt -e bash -c geany_run_script.sh
urxvt -hold -e bash -c "./application"