7/22/2016

Shell/Bash frequently/commonly used commands

Bash Commands:
==============
# Find all cpp files with debug in it.
$find . -name \*.cpp -exec grep -q "debug" '{}' \; -print

# Import Bash script tips
http://www.davidpashley.com/articles/writing-robust-shell-scripts/

if [ -z "${ZONE-}" ]  # if ZONE is null
if [ -n "${ZONE-}" ]  # if ZONE is non-zero
if [ -f "/cluster/env.sh" ] then  # if file exists
  echo "haha"
fi

RSRC=${1:-"/var/www"}  # default to /var/www if the first arg is null.

# Get group members
getent group docker


status (an init conf file in /etc/init/*.conf)

# Rename file extensions
for i in `ls --color=never`; do j=`echo $i | awk -F "." '{print $1}'`; echo $j; mv $i $j.bin; done
# Rename a file with leading 0 prefix
for i in `ls *.jpg`; do j=`echo $i | awk -F "." '{print $1}'`; k=$(printf "%06d" $j); mv $i $k.jpg; done

# To find and replace
sed -i 's/haha/xixi/g' $input
use -i to change the input file directly


for i in `grep -r "2009 Google" * -l`; do sed -i 's/2009\ Google/2010\ Google/g' $i; done
for i in `find . -name BUILD`; do echo $i; sed -i 's/\"\/\/third_party/\"@deepmap_base\/\/third_party/g' $i; done
for i in `find . -name BUILD`; do for j in common point_cloud protos third_party public anga_clients; do sed -i "s/\"\/\/$j/\"@deepmap_base\/\/$j/g" $i; rm -rf $j; done; buildifier $i; done
for i in `find . -iname "SCon*"`; do if grep -q "\\.a" $i; then echo $i; fi done

# Loop a bash array
ids=(1 2 3)

for i in ${ids[@]} do echo $i; done;

for i in {0..10}; do echo $i; done;

# Loop file content: each line
cat run.txt | while read line; do echo $line; done

# Change file name
for i in `cat files.txt`; do echo $i; j=`echo $i | awk -F hgt '{print $1}'`; mv $i ${j}.hgt.zip; done

# Change files names based on pages
j=0
for i in `ls *.pdf`; do mv $i "第${j}页.pdf"; j=$(($j+1)); done

# Sort by size
du -sh * | gsort -h

# cd to real path of a link
cd -P

# List files with full absolute path
ls -d -1 $PWD/*

# tar filter
find . -name '*.php' | tar -cvjf my.tar.bz2 --files-from -

http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O - | tar -xz
The wget option -O specifies a file to which the documents is written, and here we use -, meaning it will written to standard output and piped to tar and the tar flag -x enables extraction of archive files and -z decompresses, compressed archive files created by gzip.

# To get computer hardware info
inxi

No comments: