Bash Shell

Table of Contents


Bash Manual

https://www.gnu.org/software/bash/manual/html_node/index.html#SEC_Contents

Sync two directories using rsync from remote to local

Sync two directories using rsync from remote to local, Keep same directory structure but only copy files having pattern *hexaboard*.png from directory following pattern M6*-VScan to local directory Plots/

rsync -avz --prune-empty-dirs --include '*/' --include 'M6*-VScan/*hexaboard*.png' --exclude '*' rasharma@lxplus.cern.ch:/afs/cern.ch/user/r/rasharma/work/HGCAL/hgcalplots/ Plots/

Command details:

  • -a: archive mode
  • -v: verbose
  • -z: compress file data during the transfer
  • --prune-empty-dirs: exclude empty directories
  • --include '*/': include all directories
  • --include 'M6*-VScan/*hexaboard*.png': include all files having pattern *hexaboard*.png from directory following pattern M6*-VScan
  • --exclude '*': exclude all other files

Reference: https://unix.stackexchange.com/questions/2161/rsync-filter-copying-one-pattern-only

Strip directory and suffix from a file name

https://www.gnu.org/software/coreutils/manual/html_node/basename-invocation.html#basename-invocation

Check if two files are different sizes

if [ $(stat -c %s FILE1.txt) -ne $(stat -c %s FILE2.txt) ]; then
   echo "They're different."
fi

or

# SYNOPSIS
#   fileSize file ...
# DESCRIPTION
#   Returns the size of the specified file(s) in bytes, with each file's
#   size on a separate output line.
fileSize() {
  local optChar='c' fmtString='%s'
  [[ $(uname) =~ Darwin|BSD ]] && { optChar='f'; fmtString='%z'; }
  stat -$optChar "$fmtString" "$@"
}

if (( $(fileSize FILE1.txt) != $(fileSize FILE2.txt) )); then
  echo "They're different."
fi

Reference: https://stackoverflow.com/q/23331864/2302094

Comparison Operators

Reference: http://tldp.org/LDP/abs/html/comparison-ops.html

Convert number from decimal to hexadecimal

 echo "ibase=10; obase=16; 29" | bc

output:

1D

Check the running time of bash script

start=`date +%s`
stuff1
end=`date +%s`

runtime=$((end-start))

Reference: https://unix.stackexchange.com/a/52347




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Vi-Editor
  • GitLab workflow for CMS-AN
  • Condor Jobs
  • EOS uses
  • find command
  • sed command
  • Grep command
  • Git CheatSheet
  • awk command
  • ROOT CheatSheet
  • PPT with plots in one click