よく使うのはcdsで作業ディレクトリを保存しておくコマンド。cdwで保存したディレクトリに飛ぶ。
cdlでcdsで保存したディレクトリが見れる。
他のでも使えると思うけど使用しているのはCentOS 7.0.1406。
bashの設定ファイル
.bashrc
# .bashrc
############################################
# 共通bashrcの読み込み
############################################
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
############################################
# PS設定
############################################
# PS初期変数
PS_USER="\[\e[33m\]\u\[\e[0m\]"
PS_HOST="\[\e[32m\]\h\[\e[0m\]"
PS_WORK="\[\e[37;1m\]\W\[\e[0m\]"
PS_SCREEN=""
PS_SSH=""
PS_ATODE=""
PS_HIST=""
# Parttyセッション中ならホスト名をセッション名+"!"にする
if [ -n "${PARTTY_SESSION}" ] ; then
PS_HOST="\[\e[32m\]${PARTTY_SESSION}!\[\e[0m\]"
fi
# screenを張っているときはWINDOW番号
if [ -n "${WINDOW}" ] ; then
PS_SCREEN="(\[\e[35m\]${WINDOW}\[\e[0m\])"
fi
# SSHで繋いでいるときは接続元IPを表示
if [ -n "${SSH_CLIENT}" ] ; then
# PS_SSH="(\[\e[36m\]`echo ${SSH_CLIENT} | sed 's/ [0-9]\+ [0-9]\+$//g'`\[\e[0m\])"
PS_SSH=""
fi
# atodeの件数
if [ -f ~/.atode ]; then
PS_ATODE="(\`cat ~/.atode | wc -l\`)"
fi
# ヒストリ番号を明るい青で
# PS_HIST="\[$(echo -en '\033[34;1m')\]\!\[$(echo -en '\033[m')\]:"
# プロンプト記号を、終了ステータスが非0なら赤、0なら白で
function ps_exit()
{
a=$?
if [ $a -ne 0 ]; then
echo -n "$(echo -ne '\033')[31m";
else
echo -n "$(echo -ne '\033')[m";
fi
exit $a;
}
PS_EXIT="\[\$(ps_exit)\]\\$\[$(echo -ne '\033')[m\]"
export PS1="[${PS_USER}${PS_SCREEN}@${PS_HOST}${PS_SSH}${PS_ATODE} ${PS_HIST}${PS_WORK}]${PS_EXIT} "
function PS1_su_set() { # suした時にps_exitが走らなくなるので対応
export PS1="[${PS_USER}${PS_SCREEN}@${PS_HOST}${PS_SSH}${PS_ATODE} ${PS_HIST}${PS_WORK}]\$ "
}
# screenの時ディレクトリを表示するようにする
if [ -n "${WINDOW}" ]; then
PS1=${PS1}'\[\033k\W\033\\\]'
fi
############################################
############################################
# エイリアス
############################################
alias su=PS1_su_set'; su'
alias cp='cp -ip'
alias mv='mv -i'
alias rm='rm -i'
alias listusers="cut -d: -f1 /etc/passwd" # ユーザ名リスト表示
alias crontab='crontab -i'
alias awka="awk 'NR==1'" # 最初の行を表示する
alias awke="awk 'END{print}'" # 最後の行を表示する
alias awkd="awk 'NF'" # 空行を削除する
alias awkl="awk 'END{print NR}'" # 行数をカウントする
alias sortuni='sort | uniq -c | sort -nr' # データの出現語をランキング
alias vimpr='vim ~/.profile'
alias vimba='vim ~/.bashrc'
alias vimrc='vim ~/.vimrc'
alias vimlo='vim ~/.bash_logout'
alias vimsc='vim ~/.screenrc'
alias vimma='vim $MAIL'
alias soba='source ~/.bashrc'
# pushdをタブファイラーのような使い方をする
export LOGOUT_TAB_FILER_SAVED="TRUE" # ログアウトした時にタブの内容を保存する
alias tab='dirs' # タブ一覧を表示(保存したディレクトリ一覧)
alias tabnew='pushd `pwd`' # タブ新規作成(現在のディレクトリを保存)
alias gt='pushd +1 > /dev/null 2>&1; dirs' # 右のタブへ移動(タブ一覧から見て右のディレクトリへ移動)
alias gT='gTfunc; dirs' # 左のタブへ移動(タブ一覧から見て最後のディレクトリへ移動)
alias tabc='popd' # 現在のタブを閉じる(保存した現在のディレクトリを削除して右のディレクトリへ移動)
alias tabo='delete_pushd' # 現在のタブ以外を閉じる(現在のディレクトリ以外の保存したディレクトリを削除)
alias tabdel='echo ~ > $HOME/.dirs' # ~/.dirsの中身を初期化する
alias tabsave='save_dirs' # タブの内容をセーブする
alias tabload='delete_pushd;read_dirs' # タブの内容をロードする
alias tabyes='export LOGOUT_TAB_FILER_SAVED="TRUE";echo ログアウト時のタブ保存を有効' # ログアウトした時にタブ内容を保存する
alias tabno='export LOGOUT_TAB_FILER_SAVED="FALSE";echo ログアウト時のタブ保存を無効' # ログアウトした時にタブ内容を保存しない
function tabl(){ # タブを数値を指定して移動
pushd +0$1
}
function gTfunc() {
lastNum=`dirs -v | awk 'END{print}' | awk '{print substr($0, 1, 3)}'`
pushd +"${lastNum}"
}
# 作業ディレクトリコマンド
alias cdw='pwd; cd /home; pwd'
# 作業ディレクトリのエイリアス確認
alias cdl='alias | grep "^alias cdw=.*$" | sed -e "s/^alias cdw=//" | sed -e "s/^.*; cd /cdw保存パス:/" | sed -e "s/;.*$//"'
# 現在のディレクトリを作業用ディレクトリとしてcdwコマンドに保存する
function cds() {
sed -i -e "s|^alias cdw=.*$|alias cdw='pwd; cd `pwd`; pwd'|" ~/.bashrc
alias cdw='pwd; cd '`pwd`'; pwd'
echo 「`pwd`」をcdwコマンドに保存
}
# 指定行を表示
function awkn() {
if [ -z $1 ]; then # 行数の指定がない場合
command awk '$0 = NR OFS $0' # 行番号を付与して表示
elif expr "$1" : '^[0-9]*$' > /dev/null ; then # 指定行の表示
command awk 'NR=='$1''
else
echo "$1:数値を入力してください"
fi
}
# 指定したカラム位置の列を表示する
function csv() {
if [ -z $1 ]; then # ファイル名の指定がない場合
echo "ファイル名を指定してください"
elif [ -z $2 ]; then # カラム数の指定がない場合は全てを整形して表示
command column -s , -t $1
elif expr "$2" : '^[0-9]*$' > /dev/null ; then # カラム数に応じた列を表示
command cat $1 | awk 'BEGIN {FS=",";OFS=","} {print $'$2'}'
else
echo "$2:数値を入力してください"
fi
}
# mysqlサーバ起動/終了コマンド
function mysql_stend() {
pcnt=`ps -ef | grep mysqld | grep -v grep | wc -l`
if [ $pcnt = 0 ]; then
echo "MySQLサーバ停止中"
while :
do
printf "起動しますか?(y/n):"
read input
if [ -z "${input}" ]; then # 空欄の場合ループするためデフォ値を入れる
input="c"
fi
if [ ${input} = "y" ]; then
/etc/init.d/mysqld start
break
elif [ ${input} = "n" ]; then
break
fi
done
else
echo "MySQLサーバ起動中"
while :
do
printf "停止しますか?(y/n):"
read input
if [ -z "${input}" ]; then # 空欄の場合ループするためデフォ値を入れる
input="c"
fi
if [ ${input} = "y" ]; then
/etc/init.d/mysqld stop
break
elif [ ${input} = "n" ]; then
break
fi
done
fi
}
# 現在のディレクトリ以外のpushdを消す
function delete_pushd(){
now_pwd=`pwd`
for d in `dirs`
do
popd > /dev/null 2>&1
done
cd $now_pwd > /dev/null 2>&1
}
# ~/.dirsを読み込み
function read_dirs() {
if [ -f ~/.dirs ]; then
dirs=`cat ~/.dirs | awk 'NR==1'`
pushd_dirs ${dirs}
fi
}
function pushd_dirs() { # 読み込んだファイルに対してpushdする
now_pwd=`pwd`
dirs=()
# 絶対パスにしたものを配列に格納
for OPT in "$@"
do
after_sed=`echo $1 | sed "s#~#${HOME}#g"`
dirs+=("$after_sed")
shift 1
done
# 配列の中身を逆順にする
tacdirs=`echo ${dirs[@]} | sed "s/ /\n/g" | tac | tr '\n' ' '`
# 配列分pushdしていく
for d in $tacdirs
do
pushd $d > /dev/null 2>&1
done
lastNum=`dirs -v | awk 'END{print}' | awk '{print substr($0, 1, 3)}'`
pushd +"${lastNum}" > /dev/null 2>&1
popd > /dev/null 2>&1
}
# これを実行するとログアウトした後に再ログインする場合前回のタブを復元する
# 実行したくない場合はコメントアウトする
read_dirs
# dirsの内容を保存
function save_dirs() {
file_name="$HOME/.dirs"
if [ ! -f ${file_name} ]; then
echo ~ > ${file_name}
fi
file_size=`wc -c ${file_name} | awk '{print $1}'`
if [ ${file_size} -gt 4096 ] ; then
sed -i -e '$d' ${file_name}
elif [ ${file_size} -eq 0 ] ; then
echo ~ > ${file_name}
fi
sed -i -e "1i `dirs`" ${file_name}
}
############################################
############################################
# PATH、export設定など
############################################
# Ctrl+Sを無効にする
stty stop undef
# パス設定
PATH=/usr/local/java/bin:/usr/local/scala/bin:/usr/local/mysql/bin:$PATH
export PATH
# コマンド履歴
export HISTSIZE=100000 # 現在使用中のbashが保持するコマンド履歴数
export HISTFILESIZE=100000 # 履歴ファイルに保存される履歴数
# export HISTCONTROL=ignoredups # 同じコマンドが連続する場合は1回だけ記録する
# export HISTCONTROL=ignorespace # コマンドの頭にスペースを付けて実行すると記録しない
export HISTCONTROL=ignoreboth # ignoredupsとignorespaceどちらも設定する
export HISTTIMEFORMAT='%Y/%m/%d %H:%M:%S' # historyコマンドで表示された際のコマンド履歴に日付を追加
# 標準エディタ
export EDITOR=/usr/bin/vim # Vimを標準エディタとして使う
############################################
おまけ
.screenrc
# 現在の背景色で消去する
defbce "on"
# 太字には明るい色を使う(^[はvimでC-v ESCで入力する)
attrcolor b "^["
# 256色表示設定
term xterm-256color
# スクロールバッファは最大10000行にする
defscrollback 10000
# 別画面サポート。Vimやless使うときに便利
altscreen on
# コマンドキーをCtrl+Aにする
escape ^Aa
# ビジュアルベル(画面全体が点滅する)を切る
vbell off
# ベルの時はメッセージ(+音)を出す(^GはvimでC-v C-gで入力する)
# bell_msg "Bell in window %^G"
# 端末が落ちたら自動的にデタッチする
autodetach on
# 新しいウィンドウを生成するときのコマンド
shell $SHELL
# スタートアップメッセージを消す
startup_message off
# ログファイルのファイル名
# logfile "$HOME/.screen/screen-%Y%m%d-%n.log"
# 標準でログをとる
# deflog on
# ウィンドウのタイトル指定
shelltitle '$ |bash'
# メッセージを表示する時間(秒単位)
msgwait 5
# 次のメッセージを表示するまでの遅延時間(秒単位)
msgminwait 1
# ステータスバー
hardstatus on
hardstatus alwayslastline '%{= .b}%H%{-} %L=%-w%45L>%{=u b.}%n %t%{-}%+w %-20<%=%y/%m/%d(%D) %02c:%s'
caption always '%?%F%{= bw}%:%{= wk}%?%2n%f%07=%t%='
sorendition '+r .b'
# エンコード
defkanji utf-8
defencoding utf-8
encoding utf-8 utf-8
# キーバインド
bind b prev
bind n next
bind l windowlist
bind q remove
bind s split
# Shift+Tabで逆順にタブを移動
bindkey -k kB focus up
# ウィンドウリサイズモード
bind e eval 'command -c resize' 'echo "Resize window"'
bind ^e eval 'command -c resize' 'echo "Resize window"'
bind -c resize h eval 'command -c resize' 'resize -h -1'
bind -c resize l eval 'command -c resize' 'resize -h +1'
bind -c resize j eval 'command -c resize' 'resize -v +1'
bind -c resize k eval 'command -c resize' 'resize -v -1'
bind -c resize ^h eval 'command -c resize' 'resize -h -1'
bind -c resize ^l eval 'command -c resize' 'resize -h +1'
bind -c resize ^j eval 'command -c resize' 'resize -v +1'
bind -c resize ^k eval 'command -c resize' 'resize -v -1'
# screenでウィンドウサイズを変更しない
termcapinfo xterm 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l'
termcapinfo kterm-color 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l'
termcapinfo xterm-256color hs@:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l\E[?1;4;6l
# レイアウトモード
# 使い方:http://rcmdnk.github.io/blog/2014/02/18/computer-screen-3/
# ここのディレクトリが必要→https://github.com/rcmdnk/dotfiles/tree/master/.screen
# Layout command prefix C-q
bind ^q eval 'command -c layout' 'layout show'
bind -c layout ^q eval 'command -c layout' 'layout show'
# Make/Remove layout
bind -c layout c eval 'command -c layout' 'layout new' 'layout show'
bind -c layout x eval 'command -c layout' 'layout remove' 'layout show'
# Modify title/number
bind -c layout T colon 'layout title '
bind -c layout N colon 'layout number '
# Go to Next/Previous layout
bind -c layout n eval 'layout next' 'layout show'
bind -c layout p eval 'layout prev' 'layout show'
bind -c layout ^N eval 'command -c layout' 'layout next' 'layout show'
bind -c layout ^P eval 'command -c layout' 'layout prev' 'layout show'
# Choose n-th layout
bind -c layout 0 eval 'command -c layout' 'layout select 0' 'layout show'
bind -c layout 1 eval 'command -c layout' 'layout select 1' 'layout show'
bind -c layout 2 eval 'command -c layout' 'layout select 2' 'layout show'
bind -c layout 3 eval 'command -c layout' 'layout select 3' 'layout show'
bind -c layout 4 eval 'command -c layout' 'layout select 4' 'layout show'
bind -c layout 5 eval 'command -c layout' 'layout select 5' 'layout show'
bind -c layout 6 eval 'command -c layout' 'layout select 6' 'layout show'
bind -c layout 7 eval 'command -c layout' 'layout select 7' 'layout show'
bind -c layout 8 eval 'command -c layout' 'layout select 8' 'layout show'
bind -c layout 9 eval 'command -c layout' 'layout select 9' 'layout show'
# Initialize layouts
bind -c layout i eval 'screen' 'screen' 'screen' 'source $HOME/.screen/all.layout'
bind -c layout ^i eval 'screen' 'screen' 'screen' 'source $HOME/.screen/all.layout'
# Reset layouts
bind -c layout r eval 'source $HOME/.screen/all.layout'
bind -c layout ^r eval 'source $HOME/.screen/all.layout'
# Set layout
bind -c layout m eval 'command -c layoutm' 'echo "0 only, l 4-windows, 2 3-win_1-top, 3 3-win_1-top, 4 monitor (1 top, 3 small bottoms), 5 split, 6 vsplit, "'
bind -c layoutm 0 eval 'source $HOME/.screen/only.layout'
bind -c layoutm 1 eval 'source $HOME/.screen/4-windows.layout'
bind -c layoutm 2 eval 'source $HOME/.screen/3-win_1-top.layout'
bind -c layoutm 3 eval 'source $HOME/.screen/3-win_1-left.layout'
bind -c layoutm 4 eval 'source $HOME/.screen/monitor.layout'
bind -c layoutm 5 eval 'source $HOME/.screen/split.layout'
bind -c layoutm 6 eval 'source $HOME/.screen/vsplit.layout'
# Save layout
bind -c layout s eval 'command -c layouts' 'layout show'
bind -c layouts 0 eval 'layout save 0' 'echo "save to 0"'
bind -c layouts 1 eval 'layout save 1' 'echo "save to 1"'
bind -c layouts 2 eval 'layout save 2' 'echo "save to 2"'
bind -c layouts 3 eval 'layout save 3' 'echo "save to 3"'
bind -c layouts 4 eval 'layout save 4' 'echo "save to 4"'
bind -c layouts 5 eval 'layout save 5' 'echo "save to 5"'
bind -c layouts 6 eval 'layout save 6' 'echo "save to 6"'
bind -c layouts 7 eval 'layout save 7' 'echo "save to 7"'
bind -c layouts 8 eval 'layout save 8' 'echo "save to 8"'
bind -c layouts 9 eval 'layout save 9' 'echo "save to 9"'
.bash_logout
# dirsの内容を保存
function save_dirs() {
if [ $LOGOUT_TAB_FILER_SAVED = "TRUE" ] ; then
file_name="$HOME/.dirs"
if [ ! -f ${file_name} ]; then
echo ~ > ${file_name}
fi
file_size=`wc -c ${file_name} | awk '{print $1}'`
if [ ${file_size} -gt 4096 ] ; then
sed -i -e '$d' ${file_name}
elif [ ${file_size} -eq 0 ] ; then
echo ~ > ${file_name}
fi
sed -i -e "1i `dirs`" ${file_name}
fi
}
# これを実行すると前回のタブ状態を保存する
# 実行したくない場合はコメントアウトする
save_dirs