rsync -Pavx -e 'ssh -i /path/to/id_rsa' /local/file user@server:/remote/dir
Search This Blog
rsync: specify SSH identity file (private key)
Bash: check if number is odd or even
n=12 if [[ $((n % 2)) -eq 0 ]]; then echo "even" else echo "odd" fi
Bash: loop through file names with spaces
#!/bin/bash IFS_BAK=$IFS IFS=$(echo -en "\n\b") for f in $(ls) do echo "$f" done IFS=$IFS_BAK
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.txtThe 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
Or loop through a text file:
echo /path/to/1.txt | while IFS=$'\t' read a b; do echo "${b}, ${a}" done
Or loop through a text file:
while IFS=$'\t' read a b; do echo "${b}, ${a}" done < /path/to/1.txt
Shell script: get file size
stat -c %s /path/to/fileor
#!/bin/bash path=/path/to/file size=$(stat -c %s ${path}) if [[ $size -eq 0 ]]; then echo "${path} is a zero-size file." fiNote: 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=${s%\"} # ${s#\"} will remove the prefix " (escaped with a backslash to prevent shell interpretation) s=${s#\"} 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/'$//'
Subscribe to:
Posts (Atom)