Skip to content

Instantly share code, notes, and snippets.

@x86-39
Created August 17, 2021 14:34
Show Gist options
  • Save x86-39/36f6a5b76d3d5dd1fdb694fc6b2a3a9f to your computer and use it in GitHub Desktop.
Save x86-39/36f6a5b76d3d5dd1fdb694fc6b2a3a9f to your computer and use it in GitHub Desktop.
Useful bash/zsh functions
# Create a directory and enter it
# Usage:
# mkcd dirname
mkcd ()
{
mkdir -p -- "$1" && cd -P -- "$1"
}
# Resursively run sed on files
# Usage:
# rsed 's/foo/bar/g' dir1 dir2 file3
rsed ()
{
find ${@:2} -type f -exec sed -i "$1" {} +
}
# Change file extension in bulk
# Usage:
# chext oldext newext file1 file2 file3
chext ()
{
for f in ${@:3}; do
if [[ $f == *.${1} ]]; then mv -- "$f" "${f%.${1}}.${2}"; fi
done
}
# Run a command on (multiple) hosts
# Usage:
# sshc "ls -lah" host1 host2
sshc ()
{
for host in ${@:2}; do
printf "\033[0;34mRunning on \033[0;31m${host}\033[0m\n";
ssh -q -t $host "$1";
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment