Skip to content

Instantly share code, notes, and snippets.

@nbommakanti
Last active January 5, 2017 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbommakanti/e244fcda86fa77f4e780674e444a071c to your computer and use it in GitHub Desktop.
Save nbommakanti/e244fcda86fa77f4e780674e444a071c to your computer and use it in GitHub Desktop.
A collection of useful commands
## Batch rename files
to_replace="REPLACE ME"
replace_with=""
find . -type f -iname "*PATTERN TO MATCH*" -print0 | while IFS= read -r -d $'\0' file; do
mv "$file" "${file/$to_replace/$replace_with}";
done
# http://stackoverflow.com/questions/15065010/how-to-perform-a-for-each-file-loop-by-using-find-in-shell-bash
# option 2
to_replace="REPLACE ME"
replace_with=""
for file in "*.PATTERN"; do
mv "$file" "${file/$to_replace/$replace_with}";
done
#-------------------------------------------------------------------------------------------------------------
## Delete
# files of size 0
find . -type f -size 0 -delete
# symlinks
find . -type l -delete
#-------------------------------------------------------------------------------------------------------------
# b) function cd_func
# This function defines a 'cd' replacement function capable of keeping,
# displaying and accessing history of visited directories, up to 10 entries.
# To use it, uncomment it, source this file and try 'cd --'.
# acd_func 1.0.5, 10-nov-2004
# Petar Marinov, http:/geocities.com/h2428, this is public domain
######### Usage
# cd --
# Shows the history list of visited directories. The list shows the most recently visited names on the top.
#
# cd -NUM
# Changes the current directory with the one at position NUM in the history list. The directory is also moved from this position to the top of the list
cd_func ()
{
local x2 the_new_dir adir index
local -i cnt
if [[ $1 == "--" ]]; then
dirs -v
return 0
fi
the_new_dir=$1
[[ -z $1 ]] && the_new_dir=$HOME
if [[ ${the_new_dir:0:1} == '-' ]]; then
#
# Extract dir N from dirs
index=${the_new_dir:1}
[[ -z $index ]] && index=1
adir=$(dirs +$index)
[[ -z $adir ]] && return 1
the_new_dir=$adir
fi
#
# '~' has to be substituted by ${HOME}
[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"
#
# Now change to the new dir and add to the top of the stack
pushd "${the_new_dir}" > /dev/null
[[ $? -ne 0 ]] && return 1
the_new_dir=$(pwd)
#
# Trim down everything beyond 11th entry
popd -n +11 2>/dev/null 1>/dev/null
#
# Remove any other occurence of this dir, skipping the top of the stack
for ((cnt=1; cnt <= 10; cnt++)); do
x2=$(dirs +${cnt} 2>/dev/null)
[[ $? -ne 0 ]] && return 0
[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
if [[ "${x2}" == "${the_new_dir}" ]]; then
popd -n +$cnt 2>/dev/null 1>/dev/null
cnt=cnt-1
fi
done
return 0
}
alias cd=cd_func
#-------------------------------------------------------------------------------------------------------------
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
#-------------------------------------------------------------------------------------------------------------
## Create animations using ImageMagick
# convert *.JPG -coalesce -duplicate 1,-2-1 -quiet -layers OptimizePlus -loop 0 -rotate 90 patrol.gif
# convert *.JPG -rotate 90 animation.gif
for d in ./*/ ;
do
(cd "$d" && convert *.JPG -coalesce -duplicate 1,-2-1 -quiet -layers OptimizePlus -loop 0 -rotate 90 patrol.gif && convert *.JPG -rotate 90 animation.gif);
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment