There are many very useful shell tricks when using the bash shell on Linux or UNIX.
Print the last used command-line arguments with a keyboard shortcut.
ESC-. This will print the last used command-line arguments to the prompt. Very useful if it is a very long one-liner.
Print a listing of your most-used commands and print a number telling you how many times they were used.
1 2 3 4 | bash-3.2$ history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head 5 history 1 ps 1 ls |
Add this to the ~/.bashrc file to enable Bash completion.
if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi |
This will enable per-program tab-completion (e.g. attempting tab-completion when the command line starts with evince will only show files that evince can open, and it will also tab-complete command-line options).
If you forget to use sudo when running a command, use the following to fix this.
$ sudo !! |
This re-runs your last command and uses sudo, this way you do not need to type it all again.
Setting the following makes bash erase duplicate commands in your history.
bash-3.2$ export HISTCONTROL="erasedups:ignoreboth" |
This function will scan a folder and find any symbolic links that do not point anywhere. Very useful indeed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | function badlink() # From Atomic magazine #43 August 2004. http://www.atomicmpc.com.au { DEFAULT=$(tput sgr0); FILELIST=.badlink.list [ -e $FILELIST ] && $( rm -fr $FILELIST ) function checklink() { for badlink in $1/*; do [ -h "$badlink" -a ! -e "$badlink" ] && echo \ \"$badlink\" >> $FILELIST [ -d "$badlink" ] && checklink $badlink done } for directory in `pwd`; do if [ -d $directory ] ; then checklink $directory; fi done if [ -e $FILELIST ] ; then for line in $(cat $FILELIST); do echo $line | xargs -r rm | echo -e "$line \ -removed" echo done rm -fr $FILELIST else printf "Bad symlinks not found.\n\n" fi } # End Atomic function. |
Print information about your user when a terminal is opened. This is very useful information.
1 2 3 4 5 6 7 | if [ -x /usr/bin/finger ] ; then INFO=$(finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since" ) else INFO=$(uname -msov) fi echo -ne "${INFO}\n" |
How to set a blinking block cursor in Linux with Bash.
# Setting a blinking block cursor for the console. echo -e '\033[?6c' |
This is what it should look like.
unknown ~ $ █ |