Mein macOS .bash_profile
Seit einiger Zeit beschäftige ich mich relativ viel mit meinem Terminal. Dabei hat sich einige Mac-Bash-Aliase und Shortcuts angesammelt, die mir das Leben erleichtert hat. Meine aktuelles .bash_profile findest Du weiter unter. Übernimm was Dir nützlich erscheint.
Eine schnelle Einführung was .bash_profile ist
Im Benutzerverzeichnis eines Macs befindet sich eine versteckte Datei mit dem Namen .bas_profile. Diese Datei wird jedes Mal geladen, bevor der Terminal seine Shell-Umgebung lädt, und enthält sämtliche Startkonfigurationen und Einstellungen für die Befehlszeilenschnittstelle. In dieser Datei können Terminal-Eingabeaufforderungen geändert werden wie beispielsweise Textfarben oder aber Aliase und Funktionen hinzugefügt werden, was viel Zeit einsparen lässt.
Diese Datei wird häufig als ‘dot file’ bezeichnet, da der "." am Anfang des Names sie im Mac Finder unsichtbar macht. Durch die Eingabe von ls -al
in einem beliebigen Verzeichnis werden alle unsichtbaren Dateien im Terminal angezeigt.
So bearbeitet man die .bash_profile
- Terminal starten
- Gib
nano .bash_profile
ein - Dieser Befehl öffnet oder erstellt, falls nicht vorhanden, das Dokument .bash_profile im dem am einfachsten zu verwendenden Texteditor im Terminal - Nano. - Jetzt kannst Du Dein .bash_profile bearbeiten und z.B. einen Alias erstellen.
- Änderungen werden mit
ctrl+o
&Enter
gespeichert. Ctrl+x
beendet Nano.- Um die neuen Einstellungen zu aktivieren nur noch
source .bash_profile
tippen und fertig.
Meine .bash_profile
Ich habe mir, wie schon erwähnt eigene Aliase und Konfigurationen zusammengestellt, welche ich für sinnvoll gehalten habe. Online gibt es aber gewiss noch viele weitere. Ich verweise hier auch auf Nathaniel Landau für mehr
Also here you go:
# --------------------------------------------------------------------------- # # Description: This file holds all my BASH configurations and aliases # # Sections: # 1. Enviroment Configuration & Quick Go 2 # 2. Make Terminal Better (remapping defaults and adding functionality) # 3. File and Folder Management # 4. Searching # 5. Networking # 6. System Operations & Information # # --------------------------------------------------------------------------- # ------------------------------- # 1. ENVIRONMENT CONFIGURATION & QUICK GO 2 # ------------------------------- # Set Default Editor # ------------------------------------------------------------ export EDITOR=/usr/bin/atom alias b='atom ~/.bash_profile' # ----------------------------- # 2. MAKE TERMINAL BETTER # ----------------------------- alias ~='cd ~' # ~: Go Home alias cd..='cd ../' # Go back 1 directory level alias ..='cd ../' # Go back 1 directory level alias ...='cd ../../' # Go back 2 directory levels alias .3='cd ../../../' # Go back 3 directory levels alias .4='cd ../../../../' # Go back 4 directory levels alias o='open -a Finder ./' # o: Opens current directory in MacOS Finder alias d='cd ~/desktop' # d: Go 2 Desktop alias n='c; exec sudo --login --user $USER;' # n: Clear & Reload terminal display = new Window alias cls='c; exec bash --login;' # cls: Clear & Refresh terminal display alias which='type -all' # which: Find executables alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths alias showOptions='shopt' # ShowOptions: display bash options settings alias fixStty='stty sane' # fixStty: Restore terminal settings when screwed up alias cic='set completion-ignore-case On' # cic: Make tab-completion case-insensitive mcd () { mkdir -p "$1" && cd "$1"; } # mcd: Makes new Dir and jumps inside trash () { command mv "$@" ~/.Trash ; } # trash: Moves a file to the MacOS trash ql () { qlmanage -p "$*" >& /dev/null; } # ql: Opens any file in MacOS Quicklook Preview alias DT='tee ~/Desktop/terminalOut.txt' # DT: Pipe content to file on MacOS Desktop code () { atom $1; } #code: Open file in Coding Enviroment Atom url () { open http://$1; } #url: Open Url in Chrome # c: same as hitting cmd + k Clear terminal display # ------------------------------------------ function c { osascript -e 'tell application "System Events" to keystroke "k" using command down' } # fastest way to reboot MAC # ------------------------------------------ alias restart='sudo launchctl reboot userspace' # ------------------------------- # 3. FILE AND FOLDER MANAGEMENT # ------------------------------- # convert: convert into another format Example: convert file.avi mp4 # ---------------------------------------- convert(){ command ffmpeg -i "$1" "${1%%.*}.$2" } zipf () { zip -r "$1".zip "$1" ; } # zipf: To create a ZIP archive of a folder alias numFiles='echo $(ls -1 | wc -l)' # numFiles: Count of non-hidden files in current dir # extract: Extract most know archives with one command # --------------------------------------------------------- extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xjf $1 ;; *.tar.gz) tar xzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar e $1 ;; *.gz) gunzip $1 ;; *.tar) tar xf $1 ;; *.tbz2) tar xjf $1 ;; *.tgz) tar xzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "'$1' cannot be extracted via extract()" ;; esac else echo "'$1' is not a valid file" fi } # --------------------------- # 4. SEARCHING # --------------------------- alias qfind="find . -name " # qfind: Quickly search for file ff () { /usr/bin/find . -name "$@" ; } # ff: Find file under the current directory ffs () { /usr/bin/find . -name "$@"'*' ; } # ffs: Find file whose name starts with a given string ffe () { /usr/bin/find . -name '*'"$@" ; } # ffe: Find file whose name ends with a given string # spotlight: Search for a file using MacOS Spotlight's metadata # ----------------------------------------------------------- spotlight () { mdfind "kMDItemDisplayName == '$@'wc"; } # --------------------------- # 5. NETWORKING # --------------------------- alias myip='curl ip.appspot.com' # myip: Public facing IP Address alias netCons='lsof -i' # netCons: Show all open TCP/IP sockets alias flushDNS='dscacheutil -flushcache' # flushDNS: Flush out the DNS Cache alias lsock='sudo /usr/sbin/lsof -i -P' # lsock: Display open sockets alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets alias ipInfo0='ipconfig getpacket en0' # ipInfo0: Get info on connections for en0 alias ipInfo1='ipconfig getpacket en1' # ipInfo1: Get info on connections for en1 alias openPorts='sudo lsof -i | grep LISTEN' # openPorts: All listening connections alias showBlocked='sudo ipfw list' # showBlocked: All ipfw rules inc/ blocked IPs # killPort: kills used port Example: killPort 8080 # --------------------------- killPort(){ pid=$(lsof -ti tcp:$1) if [[ $pid ]]; then kill -9 $pid fi }