GNU/Linux >> Linux の 問題 >  >> Linux

bashrcファイルに欠落しているコマンドは何ですか?

ある日、Enable Sysadminの記事をレビューしているときに、この奇妙なアイデアを思いつきました。 Linuxのシステム管理者がbashrcファイルで使用しているコマンドに興味がありました。 bashrcファイルは、Linux環境をカスタマイズし、コマンドラインで時間を節約できるエイリアスを作成するための場所です。

私はSudoersに、彼らが作成して常に使用しているエイリアスを共有するかどうかを尋ねることにしました。素晴らしい反応には驚かされませんでしたが、ショートカットについて考慮すべきことがいくつか見つかりました。

これを共有することで、他の人がbashrcの知識を向上させることができるという考えでした。 Sudoersグループが共有した内容を確認し、システム管理者の生活を楽にするために好きなものを借りてください。

[次のこともお勧めします:LinuxでのBash履歴の解析]

ジョナサンレーマー

# Require confirmation before overwriting target files. This setting keeps me from deleting things I didn't expect to, etc
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

# Add color, formatting, etc to ls without re-typing a bunch of options every time
alias ll='ls -alhF'
alias ls="ls --color"
# So I don't need to remember the options to tar every time
alias untar='tar xzvf'
alias tarup='tar czvf'

# Changing the default editor, I'm sure a bunch of people have this so they don't get dropped into vi instead of vim, etc. A lot of distributions have system default overrides for these, but I don't like relying on that being around
alias vim='nvim'
alias vi='nvim'

バレンティンバジュラミ

これが私の~/.bashrcのいくつかの関数です ファイル:

# Easy copy the content of a file without using cat / selecting it etc. It requires xclip to be installed
# Example:  _cp /etc/dnsmasq.conf
_cp()
{
  local file="$1"
  local st=1
  if [[ -f $file ]]; then
    cat "$file" | xclip -selection clipboard
    st=$?
  else
    printf '%s\n' "Make sure you are copying the content of a file" >&2
  fi
  return $st    
}

# This is the function to paste the content. The content is now in your buffer.
# Example: _paste   

_paste()
{
  xclip -selection cliboard -o
}

# Generate a random password without installing any external tooling
genpw()
{
  alphanum=( {a..z} {A..Z} {0..9} ); for((i=0;i<=${#alphanum[@]};i++)); do printf '%s' "${alphanum[@]:$((RANDOM%255)):1}"; done; echo
}
# See what command you are using the most (this parses the history command)
cm() {
  history | awk ' { a[$4]++ } END { for ( i in a ) print a[i], i | "sort -rn | head -n10"}' | awk '$1 > max{ max=$1} { bar=""; i=s=10*$1/max;while(i-->0)bar=bar"#"; printf "%25s %15d %s %s", $2, $1,bar, "\n"; }'
}

Peter Gervase

夜間にシャットダウンするために、すべてのSSHセッションを強制終了してから、VPN接続をすべて強制終了します:

#!/bin/bash
/usr/bin/killall ssh
/usr/bin/nmcli connection down "Raleigh (RDU2)"
/usr/bin/nmcli connection down "Phoenix (PHX2)"

バレンティンロスバーグ

alias vim='nvim'
alias l='ls -CF --color=always''
alias cd='cd -P' # follow symlinks
alias gits='git status'
alias gitu='git remote update'
alias gitum='git reset --hard upstream/master'

スティーブオーブン

alias nano='nano -wET 4'
alias ls='ls --color=auto'
PS1="\[\e[01;32m\]\u@\h \[\e[01;34m\]\w  \[\e[01;34m\]$\[\e[00m\] "
export EDITOR=nano
export AURDEST=/var/cache/pacman/pkg
PATH=$PATH:/home/stratus/.gem/ruby/2.7.0/bin
alias mp3youtube='youtube-dl -x --audio-format mp3'
alias grep='grep --color'
alias best-youtube='youtube-dl -r 1M --yes-playlist -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]''
alias mv='mv -vv'
shopt -s histappend
HISTCONTROL=ignoreboth

ジェイソンヒベッツ

私のbashrcエイリアスは以前の技術者ほど洗練されていませんが、ショートカットが本当に好きだと言えるでしょう:

# User specific aliases and functions

alias q='exit'
alias h='cd ~/'
alias c='clear'
alias m='man'
alias lsa='ls -al'
alias s='sudo su -'

ボーナス:bashrcファイルの整理とファイルのクリーンアップ

多くのシステム管理者は、作業をより自動化するためにスクリプトを作成することを好みます。役立つと思われるSudoersからのヒントをいくつか紹介します。

クリスコリンズ

誰に感謝する必要があるのか​​わかりません。Twitterで名前を覚えていない素晴らしい女性ですが、bashのエイリアスとコマンドの構成が完全に変わりました。

Ansibleドロップの個人.bashrcがあります ファイルを~/.bashrc.d/に 特定のテクノロジーまたはAnsibleの役割に関連する、必要なエイリアス、コマンド、またはショートカットを使用して、ホストごとに個別に管理できます。これは、.bashrcファイルについて私が学んだ中で最高の単一のトリックです。

Gitのものは~/.bashrc.d/git.bashrcを取得します 、Kubernetesは~/.bashrc.d/kube.bashrcに入ります 。

if [ -d ${HOME}/.bashrc.d ]
then
  for file in ~/.bashrc.d/*.bashrc
  do
    source "${file}"
  done
fi

Peter Gervase

これらはbashrcエイリアスではありませんが、私はいつもそれらを使用しています。 cleanという名前の小さなスクリプトを作成しました ファイルの余分な行を取り除くため。たとえば、ここにnsswitch.confがあります コメントと空白行がたくさんあります:

[pgervase@pgervase etc]$ head authselect/nsswitch.conf
# Generated by authselect on Sun Dec  6 22:12:26 2020
# Do not modify this file manually.

# If you want to make changes to nsswitch.conf please modify
# /etc/authselect/user-nsswitch.conf and run 'authselect apply-changes'.
#
# Note that your changes may not be applied as they may be
# overwritten by selected profile. Maps set in the authselect
# profile always take precedence and overwrites the same maps
# set in the user file. Only maps that are not set by the profile

[pgervase@pgervase etc]$ wc -l authselect/nsswitch.conf
80 authselect/nsswitch.conf

[pgervase@pgervase etc]$ clean authselect/nsswitch.conf
passwd:     sss files systemd
group:      sss files systemd
netgroup:   sss files
automount:  sss files
services:   sss files
shadow:     files sss
hosts:      files dns myhostname
bootparams: files
ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
publickey:  files
aliases:    files

[pgervase@pgervase etc]$ cat `which clean`
#! /bin/bash
#
/bin/cat $1 | /bin/sed 's/^[ \t]*//' | /bin/grep -v -e "^#" -e "^;" -e "^[[:space:]]*$" -e "^[ \t]+"

[無料のオンラインコース:Red HatEnterpriseLinuxの技術概要。 ]

まとめ

それで全部です。パーソナライズされた効率的な環境を持つことで、Linuxエクスペリエンスがさらに向上します。 bashrcファイルは、これらのカスタマイズを実装するのに最適な場所です。これらのヒントを共有することで、bashrcファイルを更新し、いくつかのキーストロークを節約できることを願っています。


Linux
  1. 10Linuxでのファイル検索を改善するためのコマンドの検索[基本]

  2. .bashrcの「rc」は何の略ですか?

  3. Linux のスパース ファイルとは

  1. メモリ マップド ページと匿名ページとは何ですか?

  2. シェルコマンドは何語で書かれていますか?

  3. bashrcエイリアスでコマンドをエスケープするには?

  1. Zshにはどのような機能があり、Bashにはない、またはその逆ですか?

  2. iノードは何に適していますか?

  3. あなたのネームサーバーは何ですか