rsync: specify SSH identity file (private key)

rsync -Pavx -e 'ssh -i /path/to/id_rsa' /local/file user@server:/remote/dir

Bash: check if number is odd or even

if [[ $((n % 2)) -eq 0 ]]; then
   echo "even"
   echo "odd"

Bash: loop through file names with spaces


IFS=$(echo -en "\n\b")
for f in $(ls)
  echo "$f"

bash: remove leading zeros

n=$(expr $s + 0)
echo $n

find without recursion

find . -maxdepth 1
find . -maxdepth 1 -type d

sed: delete all characters before matching word

echo "    1234567890abcdef" | sed 's/^*.abc/abc/'

sed: replace formatted numbers

Example remove date string:
s="1234 2007-12-21 11:22:30 1.txt"
s=$(echo $s | sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\s*//')
echo $s
# 1234 1.txt

Shell script: execute a string as command

eval is what we need:
string="echo \"Shell scripting is cool.\""
eval $string

Shell script: set delimiter and read line from file

while IFS=$'\t' read col1 col2; do
    echo "column 1: ${col1}"
done < /path/to/input.txt
The above code read from tab delimited text file.

Shell script: loop through lines from file or command output

You can pipe command output to a while loop
echo /path/to/1.txt | while IFS=$'\t' read a b; do
   echo "${b}, ${a}"

Or loop through a text file:
while IFS=$'\t' read a b; do
   echo "${b}, ${a}"
done < /path/to/1.txt

awk: re-order columns using printf

echo 1.txt | awk 'BEGIN{FS="\t"} {printf "%s\t%s\n", $2, $1}'

Shell script: get file size

stat -c %s /path/to/file

size=$(stat -c %s ${path})
if [[ $size -eq 0 ]]; then
    echo "${path} is a zero-size file."
Note: If path directs to a directory, it will not fail nor return 0. It returns the block size associated with the directory. See this for more details.

Shell script: remove leading and trailing character

s="\"Shell scripting is interesting.\""
echo $s

# ${s%\"} will remove the suffix " (escaped with a backslash to prevent shell interpretation)

# ${s#\"} will remove the prefix " (escaped with a backslash to prevent shell interpretation)
echo $s

Sed: multiple substitutes in one sed command

Delimit commands with semicolon:
echo "John like apples." | sed 's/apple/pear/;s/John/David/'
Another example, remove leading and trailing quotes:
echo "'Sed is cool.'" | sed 's/^'//;s/'$//'

A shell script to check files in SWIFT object storage