Search Ali's Blog

Linux Handy Command

Below some handy Unix/Linux commands,
I will keep update them in this post as a reference


* Show machine platform:
# uname -i
* Clean trailing white spaces in source code $dir:
# find $dir -iname *.[ch] -type f -exec sed -i 's/ *$//' '{}' ';'
* Split line, and print all fields except of the first one:
# echo $1 | awk ' { for (i=2; i<=NF; i++) printf("%s ", $i); printf("\n") } '

* Get IP address of interface eth0:
# ifconfig $1 |grep Bcast| cut -d":" -f2 | cut -d" " -f1
* Log message to stdout and /var/log/messages:
# logger -s $message
* Print absolute path of $path:
# readlink -f $path
* Test console (ttyS0) echo test > /dev/ttyS0
* Extract rpm $file:
# rpm2cpio $file | cpio -idmv &> /dev/null
* Extract tar.bz2 $file:
# tar xvjf $file
* Mount iso $file:
# mount -o ro,loop $file /mnt
* Print $file suffix (remove extension)
# echo ${file%.*} OR echo $file | sed 's/\(.*\)\..*/\1/'
* show memory info:
# cat /proc/meminfo

* show cpu info:
# cat /proc/cpuinfo
* Check OS name:
# cat /ets/issue
* Get file list from patch:
# grep diff $patch | awk '{print $3}' | sed 's/a\//''/g' | sort -u
* strip string:
# echo $string | sed -e 's,^\ ,'',g'
* Generate random number:
# expr $RANDOM
* Generate random file of specific size in MB:
# dd if=/dev/urandom of=$file bs=${size}M count=$2 &> /dev/null
* Check eth0 link status:
# ethtool eth0 | grep Link

* Check time it takes to complete a command:
# time $command
* Find command path:
# which $command
* print current username:
# whoami
* Go to previous directory
# cd -
* SSH with graphics (X11) using username user and IP 10.10.10.10:
# ssh -X root@10.10.10.10
* Get file from the web
# wget $file (check man wget for more details)
* Replace string1 with string2
# sed 's/string1/string2/g'
* Modify anystring1 to anystring2
# sed 's/\(.*\)1/\12/g'
* Remove comments and blank lines
# sed '/ *#/d; /^ *$/d'
* Replace character in string
# echo $string | tr a b (use -d to delete character)
* Math 1 + 1
# echo 1 + 1 | bc
* Show free space on mounted filesystems
# df -vh
* Show file sizes
# du -sh *
* Bash loop
# for i in 1 2 3 4 5; do echo "Welcome $i times"; done
* Change bash field separator (to comma):
# _IFS=$IFS; IFS=","; CODE_HERE; IFS=$_IFS

1 comment:

BRULE Herman said...

It can be usefull