BASH TIPS & TRICKS Compiled by campmany since 2001 Latest version of this file available at: http://www.ecampmany.com/txt/help.txt SYSTEM ADMINISTRATION: # Display Linux version cat /proc/version # Display Linux distribution cat /etc/issue # Display IP hostname -I # Display 32/64 bits file /sbin/init # List printers available lpstat -a # Execution time time ./script # Install MSI file in Linux wine msiexec /i file.msi # Check if file exists if [ -f $file ]; then # List directories only ls -d */ # Find matching files find . -exec grep -Hn hello {} \; # List files absolute path find `pwd` . -type f # Trash folder in Ubuntu /home/username/.local/share/Trash TEXT MANIPULATION: # Delete return carriage awk '{sub(/\x0d/,"");print}' # Delete empty lines sed '/^$/d' # Delete blank lines with spaces sed '/^ *$/d' # Delete duplicate lines awk '!seen[$0]++' $file # Delete N first characters cut -c N- $file # Delete 1st col with diff length (tabs) cut -f 2-$NF # Skip header with n lines sed 1,nd # Delete last line sed '$d' # Replace multiple spaces by tabs tr -s " " "\t" # Replace only line n sed -i 'n s/match/new/' file # Echo with no return echo -e "Hi"\c # Use value of variable file=${!num} # Substring substring=${string:0:6} # String length ${#string} # Uppercase to lowercase awk {'print tolower($_)'} # Translate from Latin-1 to ascii iconv -f ISO-8859-1 -t ascii//TRANSLIT # Join two files with same 1st column join 1.txt 2.txt # Join two files by columns paste 1.txt 2.txt # Check if "string" is in $list list="This is Mystring" if [[ $var == *"string"* ]] # Check if $var is in $list list="11 22 33" if [[ $list =~(^| )$var($| ) ]] # Print matching line and previous grep -B1 string file NUMERICAL CALCULATION: # 2 digit number loop (bash) for i in {01..99} # N increment loop for i in `seq 0 n 100`; # N increment index num=$[$num + n] # Sum all values in a row awk '{for (i=1; i<=NF; i++) sum[NR]+= $i; print sum[NR]}' # Sum and average of 1st col: awk '{ s += $1 } END { print s, s/NR }' # Sum 3rd col every k rows: awk '{s+=$3}NR%k==0 { print s; s=0 }' # Find max and min 1st col awk 'NR == 1 {m=$1 ; p=$1} $1 >= m {m = $1} $1 <= p {p = $1} END { print "Max = " m, " Min = " p }' # Round or truncate echo .6 | awk '{printf "%0.f %d\n",$1,$1}' # Degrees.minutes to decimal coordinates awk '{split($1,a,".");print a[1]+a[2]/60} # Read (H)H:MM, print HHMM awk 'sub(/:/,"") {printf "%04d \n",$2}' # Read YYYY/(M)M/(D)D (H)H:MM awk '{split($1,a,"/")split($2,b,":"); printf "%4d%02d%02d %04d\n", a[3],a[2],a[1],b[1]b[2]}' # Transform YYYYMMDD to JJJ date -d "YYYYMMDD" +%j # Loop over time startdate=YYYYMMDD finaldate=yyyymmdd tday=$startdate while [ $tday -lt $finaldate ]; do tday=$(date +%Y%m%d -d "$tday 1 day") done OTHER: # Display help/error message if [ $# -lt 3 ]; then echo "3 inputs expected" >&2; exit 1 fi # Check Max file size allowed in UNIX system dd if=/dev/zero bs=1000000 count=3000 of=./largefile ls -l largefile # Join images vertically convert o.png o.png -append 8.png # Join images horizontally convert o.png o.png +append oo.png # Merge multiple PDF: gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=out.pdf -dBATCH 1.pdf 2.pdf convert -density 150 pdf1.pdf pdf2.pdf out.pdf # Split PDF into pages: pdftk in.pdf burst # Reduce PDF size: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf (screen/ebook/printer/prepress) # Hardprint ASCII file double page: enscript -2rG -Pprinter filename a2ps -P printer filename # Select by columns ctrl + mouse # Some vi commands Join: J Redo: . Delete everything below: +,$d