有什么你不能没有生活,将使我的生活更容易? 这里有一些我使用('盘空间'和'文件夹'特别方便)。
# some more ls aliases alias ll='ls -alh' alias la='ls -A' alias l='ls -CFlh' alias woo='fortune' alias lsd="ls -alF | grep /$" # This is GOLD for finding out what is taking so much space on your drives! alias diskspace="du -S | sort -n -r |more" # Command line mplayer movie watching for the win. alias mp="mplayer -fs" # Show me the size (sorted) of only the folders in this directory alias folders="find . -maxdepth 1 -type d -print | xargs du -sk | sort -rn" # This will keep you sane when you're about to smash the keyboard again. alias frak="fortune" # This is where you put your hand rolled scripts (remember to chmod them) PATH="$HOME/bin:$PATH"
我有一个脚本提取档案,我发现它在网上的某个地方:
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 }
由于我使用了许多不同的机器,因此我的.bashrc
总是设置命令提示符,以包含当前login的服务器的名称等等。 这样,当我在telnet / ssh三层时,我不会在错误的窗口中input错误的东西。 这真的很烂rm -rf .
在错误的窗口! (注意:在家里,所有机器上的telnet都是禁用的。在工作中,ssh并不总是启用,我没有root权限访问很多机器。)
我有一个由我的.bashrc
执行的脚本~/bin/setprompt
,其中包含:
RESET="\[\017\]" NORMAL="\[\033[0m\]" RED="\[\033[31;1m\]" YELLOW="\[\033[33;1m\]" WHITE="\[\033[37;1m\]" SMILEY="${WHITE}:)${NORMAL}" FROWNY="${RED}:(${NORMAL}" SELECT="if [ \$? = 0 ]; then echo \"${SMILEY}\"; else echo \"${FROWNY}\"; fi" # Throw it all together PS1="${RESET}${YELLOW}\h${NORMAL} \`${SELECT}\` ${YELLOW}>${NORMAL} "
如果最后一条命令成功,则该脚本将提示符设置为主机名后跟:)
如果最后一条命令失败。
较less的联机帮助页面的颜色使得联机帮助文件更容易阅读:
export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;31m' export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_so=$'\E[01;44;33m' export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_us=$'\E[01;32m'
彩色的手册页也可以通过安装大多数并使用它作为MANPAGER envvariables来获得。 如果你想使用这个寻呼机不仅仅是为了人,使用PAGERvariables,像这样:
export PAGER="/usr/bin/most -s"
当数字作为parameter passing时,会出现许多dirs,如果默认情况下没有增加1(在stackoverflow.com的评论中发现并修改了一下)
up(){ local d="" limit=$1 for ((i=1 ; i <= limit ; i++)) do d=$d/.. done d=$(echo $d | sed 's/^\///') if [ -z "$d" ]; then d=.. fi cd $d }
我处理了很多不同的机器,所以我最喜欢的是我需要经常SSH连接的每台机器的别名。
alias claudius="ssh dinomite@claudius"
设置一个好的.ssh/config
和ssh密钥以使机器间的跳跃更容易,这也是非常有用的。
另一个我最喜欢的别名是移动目录:
alias ..="cd .." alias ...="cd ../.." alias ....="cd ../../.." alias .....="cd ../../../.."
还有一些针对ls
(和拼写错误)的常用变体:
alias ll="ls -l" alias lo="ls -o" alias lh="ls -lh" alias la="ls -la" alias sl="ls" alias l="ls" alias s="ls"
历史可以是非常有用的,但是在大多数发行版中默认情况下,每个shell退出的历史都会被吹走,而且它并没有太多的开始。 我喜欢有一万行的历史:
export HISTFILESIZE=20000 export HISTSIZE=10000 shopt -s histappend # Combine multiline commands into one in history shopt -s cmdhist # Ignore duplicates, ls without options and builtin commands HISTCONTROL=ignoredups export HISTIGNORE="&:ls:[bf]g:exit"
这样,如果我知道我已经做了一些事情,但是不记得具体细节,那么一个快速的history | grep foo
history | grep foo
将有助于缓慢的记忆。
我经常发现自己通过awk
输出pipe道以获得输出的某一列,如在df -h | awk '{print $2}'
df -h | awk '{print $2}'
来查找每个磁盘的大小。 为了更容易,我在.bashrc中创build了一个函数fawk
:
function fawk { first="awk '{print " last="}'" cmd="${first}\$${1}${last}" eval $cmd }
我现在可以运行df -h|fawk 2
,这样可以节省很多打字的时间。
如果你需要指定一个分隔符( 例如 , awk -F:
for /etc/passwd
),这个函数显然不能处理这个。 在这个要点稍微彻底的版本可以处理字段号前的任意awk
参数(但仍然需要从stdininput)。
我敢肯定,我们都有我们想要放在我们的bashrc中的东西,我们不想让sudoer轻易读取。 我的解决scheme是:
if [ -f ~/.bash_private.gpg ]; then eval "$(gpg --decrypt ~/.bash_private.gpg 2>/dev/null)" fi
我有一个GPG代理,所以我只需要每隔几个小时input一次我的私钥的密码。 您仍然需要对系统的用户有一定的信任,因为您定义的variables,函数和别名可以从RAM中提取。 但是,我主要用于我的笔记本电脑。 如果被盗,我不希望有人轻易看到像这样的东西:
alias MYsql='mysql -uadmin -psecret' wglatest(){ wget -O https://admin:[email protected]/latest; }
这是一个很棒的资源:
告诉我们你的.bashrc
我曾经把这些东西都放在了这个地方,但是后来才意识到,最好是记住如何做到“手动”,因为这意味着我将完全理解正在发生的事情,并且2)即使我的自定义.bashrc没有安装。
我今天唯一使用别名的方法是减less重复input真正长的行(例如, alias myhost='ssh -T [email protected] screen -dAr'
)
那里的一行和小脚本可以永远持续下去。 我build议你自己写东西。 在http://www.commandlinefu.com一些很好的短打的东西。 这里有一些东西。
#use extra globing features. See man bash, search extglob. shopt -s extglob #include .files when globbing. shopt -s dotglob #When a glob expands to nothing, make it an empty string instead of the literal characters. shopt -s nullglob # fix spelling errors for cd, only in interactive shell shopt -s cdspell # vi mode set -o vi s() { # do sudo, or sudo the last command if no argument given if [[ $# == 0 ]]; then sudo $(history -p '!!') else sudo "$@" fi } prompt_command() { p=$PWD # p is much easier to type in interactive shells # a special IFS should be limited to 1 liners or inside scripts. # Otherwise it only causes mistakes. unset IFS } PROMPT_COMMAND=prompt_command # smart advanced completion, download from # http://bash-completion.alioth.debian.org/ if [[ -f $HOME/local/bin/bash_completion ]]; then . $HOME/local/bin/bash_completion fi extract () { # extract files. Ignore files with improper extensions. local x ee() { # echo and execute echo "$@" $1 "$2" } for x in "$@"; do [[ -f $x ]] || continue case "$x" in *.tar.bz2 | *.tbz2 ) ee "tar xvjf" "$x" ;; *.tar.gz | *.tgz ) ee "tar xvzf" "$x" ;; *.bz2 ) ee "bunzip2" "$x" ;; *.rar ) ee "unrar x" "$x" ;; *.gz ) ee "gunzip" "$x" ;; *.tar ) ee "tar xvf" "$x" ;; *.zip ) ee "unzip" "$x" ;; *.Z ) ee "uncompress" "$x" ;; *.7z ) ee "7z x" "$x" ;; esac done }
Bash的小技巧如果你是一个系统pipe理员并且使用root权限很多:
shopt -o noclobber
这将防止你意外地销毁已经存在的文件的内容,如果你redirect输出(>文件名)。 你总是可以用> |文件名强制覆盖。
我在我的bashrc中有以下内容
function __setprompt { local BLUE="\[\033[0;34m\]" local NO_COLOUR="\[\033[0m\]" local SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'` local SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'` if [ $SSH2_IP ] || [ $SSH_IP ] ; then local SSH_FLAG="@\h" fi PS1="$BLUE[\$(date +%H:%M)][\u$SSH_FLAG:\w]\\$ $NO_COLOUR" PS2="$BLUE>$NO_COLOUR " PS4='$BLUE+$NO_COLOUR ' } __setprompt
在本地机器上,它看起来像:
[17:57][user:~]$
但在远程(通过SSH)是:
[17:57][user@machine:~]$
我已经在我的.bashrc有一段时间,我发现它有帮助。 如果你打开盒子,当你login时自动启动屏幕,这样当你的networking连接中断或任何其他的,你不会失去你在做什么。 它应该放在最后。
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ] then STARTED_SCREEN=1 ; export STARTED_SCREEN [ -d $HOME/lib/screen-logs ] || mkdir -p $HOME/lib/screen-logs sleep 1 screen -U -RR && exit 0 echo "Screen failed! continuing with normal bash startup" fi
无论如何,你需要多less个别名?
我喜欢做一个cdd
别名,带我到目前最有可能在该服务器上工作的任何地方。
PATH
定义确实属于.bash_profile
,而不是.bashrc
。
在我经常使用大量screen
的服务器screen
,我的.bashrc
将具有:
alias s1="screen -dr chaos1" alias s2="screen -dr chaos2" alias s3="screen -dr chaos3" # ... and so on
( screen
是用例如screen -U -S chaos1
。)
除此之外,我设置了一些较less的默认值,防止意外closures我的terminal并启用向前导航历史logging:
# ignore case, long prompt, exit if it fits on one screen, allow colors for ls and grep colors export LESS="-iMFXR" # must press ctrl-D 2+1 times to exit shell export IGNOREEOF="2" # allow ctrl-S for history navigation (with ctrl-R) stty -ixon
我有几点:
# stop the pc speaker ever annoying me :) setterm -bfreq 0 # don't put duplicate lines in the history. See bash(1) for more options HISTCONTROL=ignoredups # ... and ignore same sucessive entries. HISTCONTROL=ignoreboth # Expand the history size HISTFILESIZE=10000 HISTSIZE=100 # commands with leading space do not get added to history HISTCONTROL=ignorespace # am I on the internet? alias p4='ping 4.2.2.2 -c 4' # pwsafe alias pw='pwsafe -p' # ls aliases alias ll='ls -l' alias la='ls -A' alias l='ls -CF' alias lt='ls -laptr' #oldest first sort alias labc='ls -lap' #alphabetical sort # cd aliases alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' # cd into the old directory alias bd='cd "$OLDPWD"' # install a package and automatically respond yes to confirmation prompt alias ins="sudo aptitude install" # remove a package and its configuration files alias remp="sudo aptitude purge" # search for a package - apt-cache and aptitude search in different ways # so have both alias searchc="apt-cache search" alias search="aptitude search" alias show="aptitude show"
尾巴所有日志在/ var / log
alias logs="find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f"
要所有的grep命令,如grep,egrep和zgrep的颜色,我有以下在我的.bashrc
export GREP_OPTIONS='--color=auto'
'文件夹'别名是伟大的! 我稍微修改它,使具有空格的目录不会导致错误。
alias folders='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn'
我会回应@ pjz关于手动了解事情的评论,而不是设置它们。 特别是如果你访问大量的机器,就像我总是这样做。
所以我肯定知道的是set -o vi
因为我知道在bash中的vi编辑命令,我不知道emacs的(除此之外,Ctrl + A干扰screen
)。 在我自己的盒子里,我把它放在.bashrc
我还发现我必须包含export EDITOR=vim
因为最近的一些发行版默认为nano,而当我期待vi的时候,这是最需要编辑的东西。 : – /
我也改变我的提示。 很久很久以前,我发现添加最后一个错误代码是非常有用的,我喜欢它。 我喜欢提示中的完整path名。 而当前的screen
号码也是如此。 包含当前用户和主机名是有道理的。 我的提示是PS1='\u@\h $PWD $WINDOW [$?] \$ '
有bash检查是否窗口大小已经改变(防止行编辑变得奇怪,如果你调整你的terminal窗口)
shopt -s checkwinsize
这是我最喜欢的。 导致bash 追加到历史logging而不是覆盖它。 通常,当你启动bash时,它会将历史载入内存中,当你closures它时会写出它。 这意味着如果你加载两个shell,使用两个shell,然后closures两个shell,最后closures的shell将覆盖所有的更改。
这段代码使得它首先追加更改(而不是覆盖整个缓冲区),然后在每个命令后写出更改。 实际上,您可以实时更新.bash_history,因此,如果您启动了一个新的terminal,则您拥有其他正在运行的会话历史logging中的所有命令。
shopt -s histappend PROMPT_COMMAND='history -a'
Shell-fu.org的.bashrc 集合
这里是地雷:
export HISTCONTROL=ignoredups export HISTIGNORE="&:ls:bg:fg" # set variable identifying the chroot you work in (used in the prompt below) if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # shows you if you are in a chroot or in a git repository PS1='${debian_chroot:+($debian_chroot)}\[\033[01;30m\]\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]$(__git_ps1)\$ ' if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi # two handy single-letter aliases alias u='ls -hltr' alias e='du * -cs | sort -nr | head' alias g='grep -C5 --color=auto' # creates a temp dir and cds into it alias td='pushd $(mktemp -d)' # find <dir> <file name regexp> <file contents regexp> function fing { find "$1" -name "$2" -exec grep -H "$3" "{}" \; } # shows "git diff" across any project in any subdirectory alias git-differ='for g in $(find . -name ".git"); do g=${g%%.git};printf "$g\t\t\t";pu $g >/dev/null && git diff |wc -l; p >/dev/null; done' # does git house keeping across any project in any subdirectory alias git-housekeep='for g in $(find . -name ".git"); do g=${g%%.git};echo $g;pu $g && git repack && git gc --auto && p;done' # Debian update alias apg='aptitude update && aptitude dist-upgrade && aptitude clean' # Quick way to serve files in HTTP from the local dir alias webs='python -m SimpleHTTPServer'
这些是我的最爱:
export HISTFILESIZE=1000000000 export HISTSIZE=1000000
我喜欢有一个永远不会忘记的命令行历史。
不幸的是,一段时间之后,我从cron发布了一个shell,它并没有读取.bashrc,并且把所有东西都切成500行,破坏了一个多年的历史。 所以我build议把这些放在/ etc / bashrc中。
这里有一些我的最爱:
alias ls='ls -F --color=auto' alias l='ls' alias ll='ls -ahl' alias ..='cd ..' alias ...='cd ../..' alias mv='mv -i' mkcd() { if [ $# != 1 ]; then echo "Usage: mkcd <dir>" else mkdir -p $1 && cd $1 fi } # Git related alias gs='git status' alias gc='git commit' alias ga='git add' alias gd='git diff' alias gb='git branch' alias gl='git log' alias gsb='git show-branch' alias gco='git checkout' alias gg='git grep' alias gk='gitk --all' alias gr='git rebase' alias gri='git rebase --interactive' alias gcp='git cherry-pick' alias grm='git rm'
我每天大约使用20次,进入最后更改的目录:
cl() { last_dir="$(ls -Frt | grep '/$' | tail -n1)" if [ -d "$last_dir" ]; then cd "$last_dir" fi }
这两个保持经常使用的目录的永久书签:
rd(){ pwd > "$HOME/.lastdir_$1" } crd(){ lastdir="$(cat "$HOME/.lastdir_$1")">/dev/null 2>&1 if [ -d "$lastdir" ]; then cd "$lastdir" else echo "no existing directory stored in buffer $1">&2 fi }
从自动Linux和Unixpipe理柯克·鲍尔(伟大的书!)
PS1='\n[\u@\h]: \w\n$?> '
在开始的换行符是我的,我喜欢在之前的输出和提示符之间有一个清晰的界限。 其余的是:
\ u =用户名
\ h =主机
\ w =工作目录
$? =最后一个返回码
我手动编译了一些东西到$ HOME / local,所以我有这个小片段:
for prog in $HOME/local/* do if [ -d "$prog/bin" ]; then export PATH=$prog/bin:$PATH fi if [ -d "$prog/include" ]; then export C_INCLUDE_PATH=$prog/include:$C_INCLUDE_PATH fi if [ -d "$prog/lib" ]; then export LD_LIBRARY_PATH=$prog/lib:$LD_LIBRARY_PATH export LIBRARY_PATH=$prog/lib:$LIBRARY_PATH fi if [ -d "$prog/man" ]; then export MANPATH=$prog/man:$MANPATH fi if [ -d "$prog/share/man" ]; then export MANPATH=$prog/share/man:$MANPATH fi done
我也有我的服务器上运行我的IRC客户端在屏幕上,所以我有(不.bashrc的东西,但仍然有用)
#!/usr/bin/env bash RUNNING=`screen -ls | grep irc` if [ "" = "$RUNNING" ]; then screen -S irc irssi else screen -dr irc fi
我在很多机器上使用我的bashrc,所以我有这个小片段来确保LS是着色的。 这将修复它在OSX机器,甚至可能* BSD,如果你调整uname线。
if [ "$TERM" != "dumb" ]; then if [ `uname` == "Darwin" ]; then alias ls='ls -G' else eval "`dircolors -b`" alias ls='ls --color=auto' fi fi
另外,我有一个命令来备份一个文件,如果你想改变一个configuration文件并且想在手边做一个快速复制,那么这个命令很有用。
bu () { cp $1 ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup ; }
这是我的最爱之一:
alias ssh='if [ "$(ssh-add -l)" = "The agent has no identities." ]; then ssh-add; fi; /usr/bin/ssh "$@"'
如果我忘记了身份validation,它可以让我这样做,而不必浪费我的input在ssh会话之后做ssh-add。
几个好的
让SSH自动完成你ssh的主机名(如果它在你的configuration或历史logging中)
complete -o default -o nospace -W "$(/usr/bin/env ruby -ne 'puts $_.split(/[,\s]+/)[1..-1].reject{|host| host.match(/\*|\?/)} if $_.match(/^\s*Host\s+/);' < $HOME/.ssh/config)" scp sftp ssh
一些有用的bash完成设置
bind "set completion-ignore-case on" # note: bind used instead of sticking these in .inputrc bind "set bell-style none" # no bell bind "set show-all-if-ambiguous On" # show list automatically, without double tab
一些有用的Mac OS X的
alias nospotlight='sudo mdutil -a -i off' alias cleardnscache='sudo killall -HUP mDNSResponder' alias ldd='otool -L' alias seq='jot - ' alias eject='drutil eject'