Showing posts with label bash tips. Show all posts
Showing posts with label bash tips. Show all posts

Linux Commad Quick Reference

Command Description
cd .. Move up one level to upper directory.
cd /root Move /root directory.
pwd Show working directory (your current position).
ls -lah Show all files and directory in working dir with format as list, all (show hidden files) and human readable size (eg. with Mb). I use it as general listing.
ls -ltr ls with order by date.
ls | wc -l Count files on a directory.
ls -lahS ls with order by file size.
df -h Check disk space.
du -hd 1 /root Check disk usage for every directories under /root directory. Useful for tracking disk space eater. Add | sort -h to sort the result.
wget -c -url- Use -c option to resume download.
grep -Rnw '/path/to/search' -e 'pattern' find in files, use --include=\*.{c,h} or --exclude=\*.o param to include or exclude spesific file extensions. Use --exclude-dir={dir1,dir2,*.dst} param to exclude dirs.

Get Current Month As Number in BASH

Use bc to convert month from current date to eliminate character zero ('0') when returned month is under 10.
This useful when you need to do some numeric operation using month.

sample :

# retrieve month ( assume current date is September, 17 2012 )
M1=`date -%m` # result: 09
M2=`date -%m|bc` # result: 9

# numeric operation
let "M1=$M1-1" # error: -bash: let: M1 = 09: value too great for base (error token is "09")

let "M2=$M2-1" # result: $M2 is 8