Gar*_*eth 141 linux bash command-line-interface dotfiles bashrc
有什么是你不能没有的,会让我的生活变得更轻松吗?以下是我使用的一些(“磁盘空间”和“文件夹”特别方便)。
# 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"
Run Code Online (Sandbox Code Playgroud)
Ger*_*t M 81
我有一个提取档案的小脚本,我在网上找到了它:
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
}
Run Code Online (Sandbox Code Playgroud)
Edd*_*die 39
由于我使用了这么多不同的机器,我.bashrc总是将命令提示符设置为包括我当前登录的服务器的名称等。这样,当我在 telnet/ssh 中深入三层时,我不会在错误的窗口中输入错误的内容。rm -rf .在错误的窗口中真的很糟糕!(注意:在家里,所有机器上都禁用了 telnet。在工作中,ssh 并不总是启用,而且我没有对很多机器的 root 访问权限。)
我有一个~/bin/setprompt由 my 执行的脚本.bashrc,其中包含:
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} "
Run Code Online (Sandbox Code Playgroud)
此脚本将提示设置为主机名,然后:)是最后一个命令是否成功以及:(最后一个命令是否失败。
小智 25
联机帮助页的颜色使联机帮助页更易于阅读:
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'
Run Code Online (Sandbox Code Playgroud)
也可以通过安装 most 并将其用作 MANPAGER 环境变量来获得彩色联机帮助页。如果您不仅要为 man 使用此寻呼机,请使用 PAGER 变量,如下所示:
export PAGER="/usr/bin/most -s"
Run Code Online (Sandbox Code Playgroud)
小智 24
随着作为参数传递的数字上升许多目录,如果默认情况下没有上升 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
}
Run Code Online (Sandbox Code Playgroud)
Dre*_*ens 19
我处理很多不同的机器,所以我最喜欢的一个是我需要经常 SSH 到的每台机器的别名:
alias claudius="ssh dinomite@claudius"
Run Code Online (Sandbox Code Playgroud)
设置好密钥.ssh/config和ssh 密钥也很有用,可以更轻松地在机器之间跳转。
我最喜欢的另一个别名是用于向上移动目录:
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
Run Code Online (Sandbox Code Playgroud)
还有一些用于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"
Run Code Online (Sandbox Code Playgroud)
历史记录可能非常有用,但默认情况下,在大多数发行版中,您的历史记录会被每个 shell 退出所震撼,而且一开始并没有多少内容。我喜欢有 10,000 行历史记录:
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"
Run Code Online (Sandbox Code Playgroud)
这样,如果我知道我以前做过一些事情但不记得具体细节,快速history | grep foo将有助于我的记忆。
我经常发现自己通过管道输出awk以获得输出的特定列,例如df -h | awk '{print $2}'找到我的每个磁盘的大小。为了更容易,我fawk在我的 .bashrc 中创建了一个函数:
function fawk {
first="awk '{print "
last="}'"
cmd="${first}\$${1}${last}"
eval $cmd
}
Run Code Online (Sandbox Code Playgroud)
我现在可以运行df -h|fawk 2它节省了大量的打字时间。
如果您需要指定分隔符(例如,awk -F:for /etc/passwd),该函数显然无法处理。本要点中稍加修改的版本可以处理awk字段编号之前的任意参数(但仍需要来自 stdin 的输入)。
Bru*_*sky 15
我相信我们都有想要放入 bashrc 的东西,我们不希望 sudoers 容易阅读这些东西。我对此的解决方案是:
if [ -f ~/.bash_private.gpg ]; then
eval "$(gpg --decrypt ~/.bash_private.gpg 2>/dev/null)"
fi
Run Code Online (Sandbox Code Playgroud)
我有一个 GPG 代理,所以我只需要每隔几个小时输入一次我的私钥密码。您仍然必须对系统用户有一定的信任,因为您定义的变量、函数和别名可以从 RAM 中提取。但是,我主要将它用于我的笔记本电脑。如果它被盗,我不希望有人轻易看到以下内容:
alias MYsql='mysql -uadmin -psecret'
wglatest(){ wget -O https://admin:secret@server.com/latest; }
Run Code Online (Sandbox Code Playgroud)
pjz*_*pjz 12
我曾经在所有地方设置这些,但后来意识到最好记住如何“手动”执行它们,因为这意味着我将 1) 完全了解正在发生的事情和 2) 可以访问这些功能,即使我的自定义 .bashrc 没有安装。
这些天我唯一使用别名的是减少重复输入非常长的行(例如。alias myhost='ssh -T user@my.remote.host screen -dAr')
那里的单衬和小脚本可以永远持续下去。我推荐 man bash 并自己写东西。http://www.commandlinefu.com 上的一些不错的简短 bash 内容。这里有一些事情。
#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
}
Run Code Online (Sandbox Code Playgroud)
小智 9
如果您是系统管理员并且经常使用 root 权限,给 Bash 的一个小技巧:
shopt -o noclobber
Run Code Online (Sandbox Code Playgroud)
如果重定向输出 (>filename),这将防止您意外破坏现有文件的内容。您始终可以使用 >|filename 强制覆盖。
小智 8
我的 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
Run Code Online (Sandbox Code Playgroud)
在本地机器上,它看起来像:
[17:57][user:~]$
Run Code Online (Sandbox Code Playgroud)
但在远程(通过 ssh)上是:
[17:57][user@machine:~]$
Run Code Online (Sandbox Code Playgroud)
我在我的 .bashrc 中已经有一段时间了,我发现它很有帮助。如果您使用 sshing,登录时会自动启动屏幕,这样当您的网络连接中断或其他任何情况时,您都不会丢失正在执行的任何操作。它应该放在最后。
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
Run Code Online (Sandbox Code Playgroud)
fortune无论如何,您需要多少别名?
我喜欢创建一个cdd别名,将我带到我目前最有可能在该服务器上工作的任何地方。
PATH重新定义真正属于.bash_profile,不属于.bashrc。
在我经常使用大量screens的服务器上,我.bashrc将拥有:
alias s1="screen -dr chaos1"
alias s2="screen -dr chaos2"
alias s3="screen -dr chaos3"
# ... and so on
Run Code Online (Sandbox Code Playgroud)
(screen例如,设置了 s screen -U -S chaos1。)
除此之外,我设置了一些默认值,防止意外关闭我的终端并启用历史记录向前导航:
# 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
Run Code Online (Sandbox Code Playgroud)
我有几点:
# 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"
Run Code Online (Sandbox Code Playgroud)
小智 5
跟踪 /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"
Run Code Online (Sandbox Code Playgroud)