GNU/Linux >> Linux Esercitazione >  >> Linux

Quali comandi mancano nel tuo file bashrc?

Un giorno ho avuto questa strana idea durante la revisione di un articolo per Enable Sysadmin. Ero curioso di sapere quali comandi usassero gli amministratori di sistema Linux nei loro file bashrc. Il file bashrc è un luogo in cui personalizzare il tuo ambiente Linux e creare alias che possono farti risparmiare tempo sulla riga di comando.

Ho deciso di chiedere ai nostri sudoer se volessero condividere quali alias creavano e usavano continuamente. Anche se non sono rimasto sorpreso dalle ottime risposte, ho trovato alcune cose da considerare per le mie scorciatoie.

L'idea era che condividere questo avrebbe ispirato gli altri a migliorare la loro abilità bashrc. Dai un'occhiata a ciò che il nostro gruppo Sudoers ha condiviso e, per favore, prendi in prestito tutto ciò che ti piace per semplificare la vita del tuo amministratore di sistema.

[ Potrebbe piacerti anche: Analisi della cronologia di Bash in Linux ]

Jonathan Roemer

# 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

Ecco alcune funzioni del mio ~/.bashrc file:

# 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"; }'
}

Pietro Gervase

Per lo spegnimento notturno, uccido tutte le sessioni SSH e quindi uccido tutte le connessioni VPN:

#!/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 Forni

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

Anche se i miei alias bashrc non sono così sofisticati come i tecnologi precedenti, puoi probabilmente dire che mi piacciono molto le scorciatoie:

# 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:organizzazione dei file bashrc e pulizia dei file

Sappiamo che molti amministratori di sistema amano creare script per rendere il loro lavoro più automatizzato. Ecco alcuni suggerimenti dei nostri sudoer che potresti trovare utili.

Chris Collins

Non so chi devo ringraziare per questo, una donna fantastica su Twitter di cui non ricordo più il nome, ma ha cambiato completamente l'organizzazione dei miei comandi e alias bash.

Ho Ansible drop individuale .bashrc file in ~/.bashrc.d/ con qualsiasi alias o comando o scorciatoia che desidero, relativi a qualsiasi tecnologia particolare o ruolo Ansible, e posso gestirli tutti separatamente per host. È stato il miglior trucco che abbia mai imparato per i file .bashrc.

Git stuff ottiene un ~/.bashrc.d/git.bashrc , Kubernetes va in ~/.bashrc.d/kube.bashrc .

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

Pietro Gervase

Questi non sono alias bashrc, ma li uso sempre. Ho scritto un piccolo script chiamato clean per eliminare le righe in eccesso nei file. Ad esempio, ecco nsswitch.conf con molti commenti e righe vuote:

[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]+"

[ Corso online gratuito:panoramica tecnica di Red Hat Enterprise Linux. ] 

Concludi

È tutto. Avere un ambiente personalizzato ed efficiente rende la tua esperienza Linux ancora migliore. Il file bashrc è un ottimo posto per implementare tali personalizzazioni. Spero che condividere questi suggerimenti ti ispiri ad aggiornare il tuo file bashrc e ti risparmi alcune sequenze di tasti.


Linux
  1. Che cosa significa "rc" in .bashrc?

  2. A cosa servono gli inode?

  3. Cosa sono i file sparsi in Linux

  4. Qual è lo scopo del file .bashrc in Linux

  5. A cosa servono i file .la di libtool?

Avvia Flatpaks dal tuo terminale Linux

Cos'è ZFS? Perché le persone ne vanno pazze?

Bashhub:accedi alla cronologia del tuo terminale da qualsiasi luogo

Cosa sono gli inode in Linux?

Che cos'è il file .bashrc in Linux?

Qual è il tuo file PHP.ini predefinito?