Eines Tages hatte ich diese seltsame Idee, als ich einen Artikel für Enable Sysadmin durchsah. Ich war neugierig, welche Befehle Linux-Systemadministratoren in ihren bashrc-Dateien verwenden. Die bashrc-Datei ist ein Ort, an dem Sie Ihre Linux-Umgebung anpassen und Aliase erstellen können, die Ihnen Zeit auf der Befehlszeile sparen können.
Ich beschloss, unsere Sudoer zu fragen, ob sie mitteilen würden, welche Aliase sie erstellt und die ganze Zeit verwendet haben. Obwohl ich von den großartigen Antworten nicht überrascht war, fand ich ein paar Dinge, die ich bei meinen Shortcuts beachten sollte.
Die Idee war, dass das Teilen dieser Informationen andere dazu inspirieren würde, ihre Bashrc-Kenntnisse zu verbessern. Sehen Sie sich an, was unsere Sudoers-Gruppe geteilt hat, und leihen Sie sich bitte alles aus, was Ihnen gefällt, um Ihr Leben als Systemadministrator einfacher zu machen.
[Das könnte Ihnen auch gefallen: Parsing Bash history in Linux ]
Jonathan Römer
# 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'
Valentin Bajrami
Hier sind ein paar Funktionen aus meinem ~/.bashrc
Datei:
# 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
Zum Herunterfahren nachts beende ich alle SSH-Sitzungen und dann alle VPN-Verbindungen:
#!/bin/bash
/usr/bin/killall ssh
/usr/bin/nmcli connection down "Raleigh (RDU2)"
/usr/bin/nmcli connection down "Phoenix (PHX2)"
Valentin Rothberg
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'
Steve Ovens
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
Jason Hibbets
Auch wenn meine Bashrc-Aliase nicht so ausgefeilt sind wie die früheren Technologen, können Sie wahrscheinlich sagen, dass ich Abkürzungen wirklich mag:
# 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 -'
Bonus:Organisieren von bashrc-Dateien und Bereinigen von Dateien
Wir wissen, dass viele Systemadministratoren gerne Skripte erstellen, um ihre Arbeit stärker zu automatisieren. Hier sind ein paar Tipps von unseren Sudoern, die Sie vielleicht nützlich finden.
Chris Collins
Ich weiß nicht, wem ich dafür danken muss, einer großartigen Frau auf Twitter, an deren Namen ich mich nicht mehr erinnere, aber es hat die Organisation meiner Bash-Aliase und -Befehle komplett verändert.
Ich habe Ansible Drop einzelne ~/.bashrc.d/
mit jedem Alias, Befehl oder Shortcut, den ich möchte, bezogen auf eine bestimmte Technologie oder Ansible-Rolle, und kann sie alle separat pro Host verwalten. Das war der beste Einzeltrick, den ich je für .bashrc-Dateien gelernt habe.
Git-Sachen bekommt einen ~/.bashrc.d/git.bashrc
, kommt Kubernetes in ~/.bashrc.d/kube.bashrc
.
if [ -d ${HOME}/.bashrc.d ]
then
for file in ~/.bashrc.d/*.bashrc
do
source "${file}"
done
fi
Peter Gervase
Dies sind keine Bashrc-Aliase, aber ich verwende sie ständig. Ich habe ein kleines Skript namens clean
geschrieben um überflüssige Zeilen in Dateien zu entfernen. Hier ist zum Beispiel nsswitch.conf
mit vielen Kommentaren und Leerzeilen:
[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]+"
[ Kostenloser Online-Kurs:Technischer Überblick zu Red Hat Enterprise Linux. ]
Abschluss
Das ist alles. Eine personalisierte und effiziente Umgebung macht Ihr Linux-Erlebnis noch besser. Die bashrc-Datei ist ein großartiger Ort, um diese Anpassungen zu implementieren. Ich hoffe, das Teilen dieser Tipps inspiriert Sie dazu, Ihre bashrc-Datei zu aktualisieren, und erspart Ihnen ein paar Tastenanschläge.