Aliasing in Linux simplifies most tasks by providing simple short-cuts to commands and paths. Lets take a look at how aliasing works and how aliasing can be done.
Step-1:
Check available aliases in system. below command should provide list of existing aliases.
or the below alias here will show disk-usage under a given directory
Step-1:
Check available aliases in system. below command should provide list of existing aliases.
No Aliases defined
bash-$
bash-$ alias
bash-$
bash-$
List of Aliases
bash-$
bash-$ alias
alias ls='ls --color=auto'
alias rm='rm -i'
alias temp='cd /tmp'
bash-$
Step-2:
Defining alias is all about simplifying tasks ( well not necessarily, aliases can be defined for fun as well ). Let us consider a user named break-it is working under a directory name, using files frequently under directories manuals, sources and run as below
/home/break-it/Documents/space/work-space/
/home/break-it/Documents/space/work-space/references/samples/manuals
/home/break-it/Documents/space/work-space/sources/programming-sources
/home/break-it/Documents/space/work-space/binaries/work-binary/run
Navigation is probably done with cd either with a relative path of absolute path, typing the entire path every time there is a change in directory is unavoidable. aliases will resolve this pain with ease. adding below lines to .bashrc with a unique name to each individual command will create aliases for each of the path.
alias ws='cd /home/break-it/Documents/space/work-space/'reload bashrc using below command
alias manuals='cd /home/break-it/Documents/space/work-space/references/samples/manuals'
alias srcs='cd /home/break-it/Documents/space/work-space/sources/programming-sources'
alias run='cd /home/break-it/Documents/space/work-space/binaries/work-binary/run'
bash-$ source ~/.bashrc
bash-$
bash-$
bash-$ alias
alias manuals='cd /home/break-it/Documents/space/work-space/references/samples/manuals'
alias run='cd /home/break-it/Documents/space/work-space/binaries/work-binary/run'
alias srcs='cd /home/break-it/Documents/space/work-space/sources/programming-sources'
alias ws='cd /home/break-it/Documents/space/work-space/'
bash-$
as you can see aliases are now created for navigating to reach directory simply type aliases on terminal followed enter to navigate to specified directory.
bash-$
bash-$ manuals
bash-$ pwd
/home/break-it/Documents/space/work-space/references/samples/manuals
bash-$
bash-$ run
bash-$ pwd
/home/break-it/Documents/space/work-space/binaries/work-binary/run
bash-$
bash-$ srcs
bash-$ pwd
/home/break-it/Documents/space/work-space/sources/programming-sources
bash-$
Step-3:
Here is a list of sample aliases which are quite useful. Below alias will help in searching for a given file
alias mfind='find ./ -name '
Example for searching for a file that end with config.
bash-$ mfind *config
./usr/lib/klibc/bin/ipconfig
./usr/lib/X11/rstart/config
./usr/lib/python2.7/dist-packages/numpy/core/lib/npy-pkg-config
./usr/lib/x86_64-linux-gnu/ImageMagick-6.7.7/config
./usr/lib/x86_64-linux-gnu/pkgconfig
./usr/lib/compizconfig
./usr/lib/smlnj/lib/pathconfig
alias disk_usage='sudo du -skh *'Example output as follows when issues under /var directory
bash-$ cd /var/
bash-$ disk_usage
5.6M backups
774M cache
28K crash
26G lib
4.0K local
0 lock
8.8M log
4.0K mail
4.0K metrics
4.0K opt
0 run
60K spool
4.0K tmp
bash-$
No comments:
Post a Comment