10 个开发人员的小助手脚本!

2025-05-27

10 个开发人员的小助手脚本!

笔记

  • 这是一些可以通过终端执行的小脚本的集合。
  • 这些是为 Ubuntu(使用 zsh)设计的,但可能适用于其他 unixy 系统
  • 有些是偷来的剧本,有些则是原创的!
  • 脚本有多种语言,例如;#!/usr/bin/env ruby
  • 这些是集合的一部分dotfiles

10 个小帮助脚本

(1)git-undo

#!/bin/sh

# Undo your last commit  (in current folder), but don't throw away your changes

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

(2)gclonecd https://github.com/reactjs/react-modal

#!/bin/zsh

# Clones git repo and cd's into it
gclonecd() {
  git_dir="$(basename "$1" .git)"
  git_dir_resolved=${2:-$git_dir}
  git clone "$@" && cd "$git_dir_resolved";
}
Enter fullscreen mode Exit fullscreen mode

(3)killport 3000

#!/bin/zsh

# Kills a process running on a specified tcp port
killport() {
  echo "Killing process on port: $1"
  fuser -n tcp -k $1;
}
Enter fullscreen mode Exit fullscreen mode

(4)mvp ./this-dir ./some/other/new/dir

#!/bin/zsh

# Move and make parents

function mvp () {
    source="$1"
    target="$2"
    target_dir="$(dirname "$target")"
    mkdir --parents $target_dir; mv $source $target
}
Enter fullscreen mode Exit fullscreen mode

(5)dos2unixd ./dir

#!/bin/zsh

# Dos2unix a whole directory (crlf to lf)
dos2unixd() {
  find $1 -type f -print0 | xargs -0 dos2unix
}
Enter fullscreen mode Exit fullscreen mode

(6)docker-kill-all

# Kills all docker containers running
alias docker-kill-all='docker kill `docker ps -aq`'
Enter fullscreen mode Exit fullscreen mode

(7)docker-rm-all

# Deletes all docker containers
alias docker-rm-all='docker rm `docker ps -aq`'
Enter fullscreen mode Exit fullscreen mode

(8)git-delete-local-merged

#!/bin/bash
#
# Delete all local branches that have been merged into HEAD. Stolen from
# our favorite @tekkub:
#
#   https://plus.google.com/115587336092124934674/posts/dXsagsvLakJ

branches=`git branch --merged master | grep -v '\*\|master\|unstable\|develop'`
[ -z "$branches" ] && printf '\nNo merged branches to delete...\n' && exit;
command="git branch -d $branches"

echo ''
printf '%s\n' "$branches"
echo ''
printf 'Delete merged branches locally? Press [Enter] to continue...'
read _

echo ''
echo 'Safely deleting merged local branches...'
echo 'Running command: '$command

`$command`
Enter fullscreen mode Exit fullscreen mode

(9)git-delete-remote-merged

#!/bin/bash

remote='origin'
echo "Will delete the following branches from [$remote]:"
branches=`git branch -r --merged master | grep -v '\*\|master\|unstable\|develop'`
[ -z "$branches" ] && printf '\nNo merged branches to delete...\n' && exit;

echo ''
printf '%s\n' "$branches"
echo ''
printf 'Delete merged remote branches from '$remote'? Press [Enter] to continue'
read _
echo ''
echo 'Safely deleting merged remote branches...'

echo $branches | sed -e "s:$remote/::g" | xargs -t git push $remote -d
Enter fullscreen mode Exit fullscreen mode

(10)github-open

#!/usr/bin/env ruby

# Opens the current github repository in the default browser
remotes = `git remote`
hasNoRemotes = remotes.size < 2
if hasNoRemotes then 
  return
end
repolink = `git remote get-url origin`
if repolink.include?("git@github.com:") then 
  # Converts from ssh format to https format if necessary

  # git@github.com:benwinding/dotfiles
  # https://github.com/benwinding/dotfiles
  repolink = repolink.gsub("git@github.com:", "https://github.com/")
end

`xdg-open #{repolink}`
Enter fullscreen mode Exit fullscreen mode

我强烈建议不熟悉 Shell 脚本的朋友们去看看Holman 的 dotfiles,它主要针对 make 用户。或者,我 fork 了Ben 的 dotfiles,它主要针对 Ubuntu 用户。

当您开始编写和维护自己的小型脚本和代码片段库时,您会对自己能够自动化和完成的工作感到惊讶。

文章来源:https://dev.to/benwinding/10-tiny-helper-scripts-for-devs-knb
PREV
Cursor AI 编辑器 — 它真的有用吗?
NEXT
什么是 REST API?