Replace contents by using sed
# Replace allsed -i "" "s|pattern|replace|g" "file.ext" # osxsed -i"" "s|pattern|replace|g" "file.ext" # linux# Replace the first line stringsed -i "" "1s|pattern|replace|g" "file.ext" # osxsed -i"" "1s|pattern|replace|g" "file.ext" # linux# Replace with \tsed -i "" $'1s|pattern|\t|g' "file.ext" # osxsed -i"" $'1s|pattern|\t|g' "file.ext" # linux
Delete files or folders except the specified one
rm -rf !("Except 1"|"Except 2"|"Except N")
'=' and '==' in if statement
#!/bin/bashif [ "foo" = "foo" ]; then echo expression evaluated as trueelse echo expression evaluated as falsefi# or #!/bin/bashif [[ "foo" == "foo" ]]; then echo expression evaluated as trueelse echo expression evaluated as falsefi
Get the modified time of file
# osxstat -f "%Sm" -t "%Y%m%dT%H%M%S" filename # datetimestat -f "%Sm" -t "%s" filename # timestamp in seconds# linuxstat -c %y filename # datetimestat -c %Y filename # timestamp in seconds
Datetime and timestamp
# Datetimeecho `date '+%Y-%m-%d %H:%M:%S.%N'`# Timestamp in secondsecho `date '+%s'`# Elapsed timeSTARTs=`date '+%s'`STARTn=`date '+%N' | sed 's/^0*//'`echo "Do something"ENDs=`date '+%s'`ENDn=`date '+%N' | sed 's/^0*//'`SECS=$(($ENDs - $STARTs))NANO=$(($ENDn - $STARTn))if (( $NANO < 0 )); then NANO=$((1000000000 + $NANO)) SECS=$(($SECS - 1))fiecho "Cost $SECS.$NANO seconds."
Check os type
if [[ "$OSTYPE" == "linux-gnu" ]]; then # ...elif [[ "$OSTYPE" == "darwin"* ]]; then # Mac OSXelif [[ "$OSTYPE" == "cygwin" ]]; then # POSIX compatibility layer and Linux environment emulation for Windowselif [[ "$OSTYPE" == "msys" ]]; then # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)elif [[ "$OSTYPE" == "win32" ]]; then # I'm not sure this can happen.elif [[ "$OSTYPE" == "freebsd"* ]]; then # ...else # Unknown.fi
Read file from arguments or stdin
content=""while read linedo content="${content}\n${line}"done < "${1:-/dev/stdin}"echo $content#or file=${1--}content=$(cat $file)echo $content
The substitution ${1:-...}
takes $1
if defined,
otherwise the file name of the standard input of the own process is used.
Function return value
# Define a functionmyfunc() { return 1}# Call the functionmyfunc# Get the result from the last command, # here is the result of `myfunc`result=$?
Remove duplicate string(no comma contains) from list
for string in $list; do unique[$string]=1; doneecho ${!unique[@]}
Read file
# entire filecontent=`cat filename.txt`echo $content# read line by linewhile IFS='' read -r line || [[ -n "$line" ]]; do echo "Text read from file: $line"done < filename.txt
Batch rename as numeric
n=1for i in $(ls | sort -n);do newfile=$(printf "%03d%s" $n .${i##*.})mv "${i}" "${newfile}"echo "${i} -> ${newfile}"((++n))done
Format number
printf "%05d\n" 52
Remove string chars
original="Hello World"world="${original:6}"hell="${original::4}"hello="${original::-6}"lo_wo="${original:3:-3}"
For loop
# For loopfor (( i=0; i<10; i++ )); do echo "$i"; done# For loop range# {START..END..INCREMENT}for i in {1..10..2}; do echo "$i"; done# For loop through files with spacesfor i in *; do echo "$i"; done;for i in *.ext; do echo "$i"; done;# For loop through ls results only show extensionfor i in $(ls); do echo "${i##*.}"; done;# For loop through ls results only show filenamefor i in $(ls); do echo "${i%.*}"; done;# For loop through ls results sort as numericfor i in $(ls | sort -n); do echo "$i"; done;
List files as numeric sort
ls | sort -n
Batch rename
for i in *.ext; do mv "$i" "${i%.ext}.newext";done
Modify permission
sudo chown -R root:wheel [file|dir]
Lock & Unlock (macOS)
# lockchflags -R uchg *# unlockchflags -R nouchg *
Delete unversioned files under svn
svn status --no-ignore | grep '^\?' | sed 's/^\? //'svn status --no-ignore | grep '^\?' | sed 's/^\? //' | xargs -Ixx rm -rf xx
Find
# Find and delete empty foldersfind . -type d -empty -delete# Find over 100MB files and sortfind / -type f -size +100M -exec ls -lh {} \; | sort -n -r# Find files between the specified dates and copy them to the specified folderfind . -type f -newermt '2018-05-25' ! -newermt '2018-05-26' | xargs -I {} cp {} "folder/"