Both sides previous revisionPrevious revisionNext revision | Previous revision |
bash:aliases [2020/05/05 18:32] – peter | bash:aliases [2021/01/09 13:36] (current) – peter |
---|
Aliases are very convenient to use for frequently used commands. | Aliases are very convenient to use for frequently used commands. |
| |
For example, here the alias **c** is created against **clear** command. | ---- |
| |
| ===== Example ===== |
| |
| An alias **c** is created against **clear** command. |
<code bash> | <code bash> |
alias c='clear' | alias c='clear'` |
</code> | </code> |
| |
Instead of typing **clear** to clear the screen only the letter **c** is needed (and the [ENTER] key). | Instead of needing to type the word **clear** to clear the screen, the letter **c** can now be used instead (with the [ENTER] key). |
| |
<code bash> | <code bash> |
| |
[[BASH:Aliases:My Aliases|My Aliases]] | [[BASH:Aliases:My Aliases|My Aliases]] |
| |
| |
---- | ---- |
| |
Create the file /etc/profile.d/bash_aliases.sh which will contain all the aliases. | [[BASH:Aliases:Delete an alias|Delete an alias]] |
| |
<file bash /etc/profile.d/bash_aliases.sh> | [[BASH:Aliases:Disable an alias temporarily|Disable an alias temporarily]] |
# Instruct bash to expand the arguments to aliases. | |
shopt -s expand_aliases | |
| |
#------------------------------------------------------------- | [[BASH:Aliases:Examples|Examples]] |
# Protection for these commands; which will prompt before overwriting. | |
#------------------------------------------------------------- | |
# Do not delete / and prompt before deleting. | |
alias rm='rm -i --preserve-root' | |
| |
alias cp='cp -i' | [[BASH:Aliases:Local vs Global Aliases|Local vs Global Aliases]] |
alias mv='mv -i' | |
alias ln='ln -i' | |
| |
# Preventing changing perms on / | [[BASH:Aliases:OS specific aliases|OS specific aliases]] |
alias chown='chown --preserve-root' | |
alias chmod='chmod --preserve-root' | |
alias chgrp='chgrp --preserve-root' | |
| |
alias rmdir='rm -Rf $1' | [[BASH:Aliases:Privileged access specific aliases|Privileged access specific aliases]] |
| |
#------------------------------------------------------------- | |
# cd | |
#------------------------------------------------------------- | |
# To pick up when user forgets the space. | |
alias cd..='cd ..' | |
| |
# Uses cd() function = pushd,· | |
# b = go backwards (popd), f = go forwards (kind of like "unpopd"). | |
alias d='dirs' | |
alias b='pushd +1' # go backwards (popd). | |
alias f='pushd -0' # go forwards (kind of like "unpopd"). | |
| |
| |
#------------------------------------------------------------- | ---- |
# Disk | |
#------------------------------------------------------------- | |
alias du='du -kh' # Human-readable format. | |
alias dutotal='du -ch 2> /dev/null |tail -1' # Displays only the total. | |
alias dutop10='du -hsx * | sort -rh | head -10' # What is using the most space. | |
| |
alias df='\df -kTh;\df -Thl --total | grep total' # Adds total to the df command. | |
| |
# Shows the individual partition usages without the temporary memory values. | |
alias dfx='\df -kThl --exclude-type=tmpfs --exclude-type=devtmpfs' | |
| |
#------------------------------------------------------------- | |
# Directories. | |
#------------------------------------------------------------- | |
alias mkdir='mkdir -p' # Make parent directories if needed. | |
alias back='cd $OLDPWD' # Return to the last directory you were in. Same as 'cd -'. | |
| |
#------------------------------------------------------------- | |
# Downloading. | |
#------------------------------------------------------------- | |
alias wget='wget -c' # -c flag to continue download in case of problems.· | |
alias wgetpage='wget --html-extension --recursive --convert-links --page-requisites --no-parent $1' | |
| |
#------------------------------------------------------------- | |
# Files. | |
#------------------------------------------------------------- | |
alias fcount='ls -l | wc -l' # File count. | |
alias fcounta='ls -la | wc -l' # File count (incl hidden files). | |
alias fcountr='find . -type f | wc -l' # File count recursive. | |
alias lastmodified='find -type f -print0 | xargs -r0 stat -c %y\ %n | sort' | |
alias nocomment='grep -Ev "^(#|$)"' # Show text file without comment (#) lines; or empty lines. | |
| |
alias bigfiles='find . -type f 2>/dev/null | xargs du -a 2>/dev/null | awk "{ if ( \$1 > 5000) print \$0 }" | sort -hr' | |
alias verybigfiles='find . -type f 2>/dev/null | xargs du -a 2>/dev/null | awk "{ if ( \$1 > 500000) print \$0 }" | sort -hr' | |
| |
alias brokenlinks='\find . -xtype l -printf "%p -> %l\n"' # Find broken symlinks. | |
| |
# count files by type - http://www.shell-fu.org/lister.php?id=173 | |
alias ftype="find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr" | |
| |
# Show diffs between files | |
alias fdiff='grep -F -x -v -f $1 $2' | |
| |
#------------------------------------------------------------- | |
# File Permissions. | |
#------------------------------------------------------------- | |
alias fperm="find . -type f -exec chmod 0644 '{}' \;" | |
alias dperm="find . -type d -exec chmod 0755 '{}' \;" | |
| |
#------------------------------------------------------------- | |
# Greps. | |
#------------------------------------------------------------- | |
alias grep='grep --color=auto' #· | |
alias egrep='egrep --color=auto' # | |
alias fgrep='fgrep --color=auto' # | |
alias grepall='find . -name "*" | xargs grep --color=auto ' | |
| |
#------------------------------------------------------------- | |
# Logs. | |
#------------------------------------------------------------- | |
# alias syslog='sudo tail -10f /var/log/syslog' # Follow syslog. | |
# alias messages='sudo tail -10f /var/log/messages' # Follow messages. | |
| |
# Last 10 items within every log file. | |
alias logs='tail -f /var/log/{dmesg,messages,*{,/*}{log,err}}' | |
| |
#------------------------------------------------------------- | |
# ls. (Assumes you use a recent GNU ls). | |
#------------------------------------------------------------- | |
alias ls='ls -h --color=auto' # Add colors and human-readable sizes by default on 'ls'. | |
alias lsa='ls -A' # Show hidden files. | |
alias lsd='ls -d */ >/dev/null 2>/dev/null; [ $? -eq 0 ] && ls -d */ || : ;' # List only directories, if any exist. | |
alias lsdf='ls -v --group-directories-first' # Directories first, with alphanumeric sorting. | |
alias lsdt='ls -h --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F' | |
alias lse='ls -AF | grep \*' # Show executables only. | |
alias lsh='ls -Am' # List files horizontally, comma-separated. | |
alias lsm='ls -Al |more' # Pipe through 'more'. | |
alias lsne='ls -AF | grep -v \*' # Show non executables only. | |
alias lsnx='ls -AF | egrep -v "\.|/"' # Show only file without an extension. | |
alias lsr='ls -lR' # List files recursively. | |
alias lssh='ls -Ax' # Sort by name horizontally. | |
alias lsss='ls -Sr' # Sort by size, biggest last. | |
alias lsssh='ls -shAxSr' # Sort by size, biggest last, horizontally. | |
alias lsst='ls -Atr' # Sort by date, most recent last | |
alias lssta='ls -Atur' # Sort by/show access time,most recent last. | |
alias lsstc='ls -Atcr' # Sort by/show change time,most recent last. | |
alias lssx='ls -AXB' # Sort by extension. | |
alias lsx='ls -AF | egrep "\.|/"' # Show only file with an extension. | |
| |
alias ll='ls -lF' # Long listing, classify. | |
alias lla='ls -lAF' # All files, long listing. | |
alias lld='ls -lFA | grep :*/' # List only directories, if any exist. | |
alias lldf='ls -lv --group-directories-first' # Directories first, with alphanumeric sorting. | |
alias lldt='ls -lh --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F' | |
alias lle='ls -lFA | grep \*' # Show executables only. | |
alias llm='ll |more' # Pipe through 'more' | |
alias llne='ls -lFA | grep -v \*' # Show non executables only. | |
alias llnx='ls -lFA | egrep -v "\.|/"' # Show only file without an extension. | |
alias llr='ll -lAR' # Recursive ls. | |
alias llss='ls -lSr' # Sort by size, biggest last. | |
alias llst='ls -lAtrh' # Sort by date, most recent last, and human readable | |
alias llsta='ls -ltur' # Sort by/show access time,most recent last. | |
alias llstc='ls -ltcr' # Sort by/show change time,most recent last. | |
alias llsx='ls -lXB' # Sort by extension. | |
alias llx='ls -lFA | egrep "\.|/"' # Show only file with an extension. | |
| |
alias tree='find . | sed "s/[^/]*\//| /g;s/| *\([^| ]\)/+--- \1/"' # Simple tree of current dir. | |
| |
#------------------------------------------------------------- | |
# Memory. | |
#------------------------------------------------------------- | |
alias mem='echo -e Memory usage: $(free -h | grep "+" | cut -c26-30)/ $(free -h | grep "Mem:" | cut -c15-18)' | |
| |
#------------------------------------------------------------- | |
# Misc. | |
#------------------------------------------------------------- | |
alias mount='mount | column -t' # Make mount command output pretty and human readable format. | |
alias pause='read -n 1 -p "Press any key to continue…"' | |
alias restart='killall -SIGUSR1' | |
alias utf='iconv -f ISO-8859-1 -t UTF-8' | |
alias which='type -a' # Includes aliases. | |
| |
#------------------------------------------------------------- | |
# Networking. | |
#------------------------------------------------------------- | |
alias connections='sudo /usr/bin/lsof -i -P' | |
alias openports='netstat -lntp 2>/dev/null | grep -v " - *$"' # Shows open ports. | |
alias macs='ifconfig | grep HWaddr' # Shows MAC addresses for all nics. | |
alias nics='ifconfig em1 | egrep "[0-9]{1,3}(\.[0-9]{1,3}){3}"' # Shows all nics. | |
alias ping='ping -c 5' # Stop after sending count ECHO_REQUEST packets. | |
alias fastping='sudo ping -c 5 -s.2' # Do not wait interval 1 second, go fast. | |
| |
# Network diagnostic. | |
# Usage: mtr google.com | |
alias trace='mtr --report-wide --curses $1' | |
| |
#------------------------------------------------------------- | |
# Path. | |
#------------------------------------------------------------- | |
# Pretty-print of some PATH variables: | |
alias path='echo -e ${PATH//:/\\n}' | |
alias libpath='echo -e ${LD_LIBRARY_PATH//:/\\n}' | |
| |
#------------------------------------------------------------- | |
# Processes. | |
#------------------------------------------------------------- | |
alias psc='ps h -eo pcpu,pmem,pid,comm,user,cputime | sort -nr | head' | |
alias psm='ps h -eo pmem,pcpu,pid,comm,user,cputime | sort -nr | head' | |
alias pstree='ps -e -o pid,args --forest' | |
alias psfull='ps -auxwww' # ps with wide output so you can see full commands. | |
| |
# usage: psg peter | |
# usage: psg ssh | |
alias psg='ps -Helf | grep -v $$ | grep -i -e WCHAN -e ' # ps grep. | |
| |
# Show only my procs. | |
alias myps='ps -fHu $USER' # if not $USER, try $LOGIN | |
| |
#------------------------------------------------------------- | |
# Programming – C/C++/Java/PHP. | |
#------------------------------------------------------------- | |
# Count lines of php code under the current directory. | |
alias countphp='find . -name "*.php" -print0 | xargs -0 wc' | |
| |
# Count lines of java code under the current directory. | |
alias countjava='find . -name "*.java" -print0 | xargs -0 wc' | |
| |
# Count lines of C or C++ or Obj-C code under the current directory. | |
alias countc='find . \( -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.m" \) -print0 | xargs -0 wc' | |
| |
# Count lines of C or C++ or Obj-C or Java or PHP code under the current directory. | |
alias countcode='find . \( -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.m" -or -name "*.java" -or -name "*.php" \) -print0 | xargs -0 wc' | |
#------------------------------------------------------------- | |
# Spelling typos. Highly personal and keyboard-dependent. | |
#------------------------------------------------------------- | |
alias xs='cd' | |
alias vf='cd' | |
alias moer='more' | |
alias moew='more' | |
alias kk='ll' | |
| |
#------------------------------------------------------------- | |
# Time. | |
#------------------------------------------------------------- | |
alias now='date +"%T"' | |
alias nowtime=now | |
alias nowdate='date +"%Y-%m-%d"' | |
| |
#------------------------------------------------------------- | |
# Web. | |
#------------------------------------------------------------- | |
# Pass it via sudo so whoever is admin can reload it without calling you. | |
alias nginxreload='sudo /usr/sbin/nginx -s reload' | |
alias nginxtest='sudo /usr/sbin/nginx -t' | |
</file> | |
| |
Set the file owner and permissions: | |
| |
<code bash> | |
chown root:root /etc/profile.d/bash_aliases.sh | |
chmod 644 /etc/profile.d/bash_aliases.sh | |
</code> | |
| |