Skip to content

Instantly share code, notes, and snippets.

@kevinmhadi
Last active June 27, 2025 18:18
Show Gist options
  • Save kevinmhadi/acf22568faeb85eb1010ce32fb630735 to your computer and use it in GitHub Desktop.
Save kevinmhadi/acf22568faeb85eb1010ce32fb630735 to your computer and use it in GitHub Desktop.
bash functions
set -a
sjless() {
squeue -o "%.18i %.9P %.150j %.8u %.2t %.10M %.6D %R %.20o %500Z" | awk 'NR==1 || $4 == ENVIRON["USER"]' | less -N -S
}
# tac() {
# if [ "$#" -gt 1 ]; then
# echo "Usage: $0 [file]" >&2
# return 1
# fi
# if [ -n "$1" ]; then
# cat "$1" | sed '1!G;h;$!d'
# else
# cat | sed '1!G;h;$!d'
# fi
# }
files_only() {
ls -p | grep -v /
}
timestamp_copy() {
if [ $# -ne 1 ]; then
echo "Usage: timestamp_copy <filename>" >&2
return 1
fi
file="$1"
if [ ! -f "$file" ]; then
echo "Error: '$file' is not a file." >&2
return 1
fi
# Get current timestamp
timestamp=$(date '+___%Y-%m-%d-%H-%M-%S')
# Extract base name and extension
base=$(basename "$file")
dir=$(dirname "$file")
case "$base" in
*.*)
name=${base%.*}
ext=.${base##*.}
;;
*)
name=$base
ext=""
;;
esac
newname="${name}${timestamp}${ext}"
cp "$file" "$dir/$newname"
printf "$newname\n"
}
stavefile () {
if [ $# -ne 1 ]; then
echo "Usage: timestamp_copy <filename>" >&2
return 1
fi
file="$1"
if [ ! -f "$file" ]; then
echo "Error: '$file' is not a file." >&2
return 1
fi
stamped_file=$(timestamp_copy "${file}")
ln -Tnfsr ./${stamped_file} $file
}
viewtable() {
if [ $# -ne 1 ]; then
echo "Usage: timestamp_copy <filename>" >&2
return 1
fi
file="$1"
delim=","
if [ ! ${2+set} = "set" ]; then
delim=${2}
fi
if [ ! -f "$file" ]; then
echo "Error: '$file' is not a file." >&2
return 1
fi
column -s${delim} -t < $file | less -#20 -N -S
}
path_append () { path_remove $1; export PATH="$PATH:$1"; }
path_prepend () { path_remove $1; export PATH="$1:$PATH"; }
path_remove () { export PATH=`echo -n $PATH | /usr/bin/awk -v RS=: -v ORS=: '$0 != "'$1'"' | /usr/bin/sed 's/:$//'`; }
cpaste () {
cat ~/clipboard
}
clp () {
cat ~/clipboard
}
getnextflowlog() {
nextflow log | \
awk -F'\t' '{print $3}' | \
tail -n+2 | \
xargs -I {} -P1 -L1 nextflow log {} -f container,hostname,module,time,queue,task_id,workdir,attempt,submit,start,complete,hash,name,native_id,status,process
}
gitprune () {
git fetch -p && git branch -vv | awk "/: gone]/{print \$1}" | xargs -I {} git branch -D {}
}
gitremote() {
git remote -v
}
gitseturl() {
# git remote set-url $1 $2
git remote set-url $@
}
gitsetremote() {
local remote=$1
if ! [ "$remote" = "origin" ]; then
local remote="origin"
else
local remote=""
fi
set -x
git remote set-url $remote $@
set +x
}
gitsetupstream() {
git push --set-upstream $@
}
gitgetbranches() {
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
}
gitfastforward() {
if [ $# -eq 0 ]; then
BRANCH="master"
else
BRANCH="$1"
fi
git merge --ff-only origin/${BRANCH}
}
calculate () {
echo "${1}" | bc -l
}
round () {
local in=$1; if [ -z "$in" ]; then read in; fi
# echo "${in}" | awk '{printf("%d\n",$1)}' `# Round`
echo "$in" | xargs printf "%.*f\n" 0 `# Round`
}
appenddirs () {
local dir_stack=$1 # Get the input argument
if [[ -z "$dir_stack" ]]; then
local dir_stack="~/.dir_stack"
dir_stack="${dir_stack/#\~/$HOME}"
fi
tmp=$(mktemp)
cat <(dirs -p) ${dir_stack} > ${tmp}
mv ${tmp} > ${dir_stack}
}
savedirs () {
local dir_stack=$1 # Get the input argument
if [[ -z "$dir_stack" ]]; then
local dir_stack="~/.dir_stack"
dir_stack="${dir_stack/#\~/$HOME}"
fi
dirs -p > ${dir_stack}
}
adddirs () {
local dir_stack=$1 # Get the input argument
if [[ -z "$dir_stack" ]]; then
local dir_stack="~/.dir_stack"
dir_stack="${dir_stack/#\~/$HOME}"
fi
while read -r dir; do
dir="${dir/#\~/$HOME}"
pushd "${dir}" > /dev/null
done < <(tac ${dir_stack})
}
recoverdirs () {
is_dir_stack_present=$(test "${DIR_STACK+set}" = "set" && echo true || echo false)
if ! $is_dir_stack_present; then
printf "No DIR_STACK variable present\n"
return
fi
cleardirs
while read -r dir; do
dir="${dir/#\~/$HOME}"
pushd "${dir}" > /dev/null
done < <(tac <(printf "$DIR_STACK\n"))
}
loaddirs() {
local dir_stack=$1 # Get the input argument
if [[ -z "$dir_stack" ]]; then
local dir_stack="~/.dir_stack"
dir_stack="${dir_stack/#\~/$HOME}"
fi
cleardirs && adddirs ${dir_stack}
}
cleardirs() {
while [ "$(dirs -p | wc -l)" -gt 1 ]; do
popd -n > /dev/null || break
done
}
dirsf() {
dirs -v
}
dirsr() {
dirs -v | tac | awk '{print NR-1, $2}'
}
pulld(){
local index=$1 # Get the input argument
# Get the total number of directories in the stack
local total_dirs
total_dirs=$(dirs -p | wc -l)
if [[ -z "$index" ]]; then
# No argument: Move item 0 to the bottom, shift items 1 and below
if (( total_dirs <= 1 )); then
echo "Not enough directories in the stack to perform this operation."
return 1
fi
pushd +1
else
if [[ -z "$index" || ! "$index" =~ ^[0-9]+$ ]]; then
echo "Usage: pulld <index>"
return 1
fi
# Get the total number of directories in the stack
local total_dirs
total_dirs=$(dirs -p | wc -l)
if (( index >= total_dirs )); then
echo "Index $index is out of range. Stack has $total_dirs directories."
return 1
fi
# Extract the directory to move
local dir_to_move
dir_to_move=$(dirs -v | awk -v idx="$index" '$1 == idx {print $2}')
dir_to_move="${dir_to_move/#\~/$HOME}"
if [[ -z "$dir_to_move" ]]; then
echo "Failed to locate directory at index $index."
return 1
fi
# Push the directory to the top
pushd "$dir_to_move" > /dev/null
# Remove the original entry from the stack
popd +"$((index + 1))" > /dev/null
fi
}
# currentJobs () {
# squeue --sort=+j -u $whoami --format="%.18i %.9P %.30j %.8u %.8T %.10M %.9l %.6D %R"
# }
# powerSlurm () {
# srun --cpus-per-task=4 --mem=32GB --time=8:00:00 --pty /bin/bash -l
# }
# SCRATCH () {
# cd /gpfs/scratch/$USER
# }
# sambamba () {
# /gpfs/share/apps/sambamba/0.6.8/sambamba-0.6.8 $@
# }
# mkdir () {
# /usr/bin/mkdir -m u=rwx,g=rxs,o=rx -p $@
# }
# IGV () {
# sh ~/IGV
# }
set +a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment