Posted: . At: 7:55 PM. This was 4 years ago. Post ID: 14447
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Very useful bash command line tips and information.


If you know you typed a command or password wrong, you can use ctrl + u to delete the whole line or ctrl + w to delete just a word. This is a very useful tip for editing the text you typed on the command line. Instead of using backspace.

Set a very nice colourful bash shell prompt.

~/.bashrc
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
35
36
37
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac
 
# uncomment for a coloured prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
 
if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi
 
if [ "$color_prompt" = yes ]; then
    PS1="\[\033[0;31m\]\342\224\214\342\224\200\$([[ \$? != 0 ]] && echo \"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]root\[\033[01;33m\]@\[\033[01;96m\]\h'; else echo '\[\033[0;39m\]\u\[\033[01;33m\]@\[\033[01;96m\]\h'; fi)\[\033[0;31m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;31m\]]\n\[\033[0;31m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]\[\e[01;33m\]\\$\[\e[0m\]"
else
    PS1='┌──[\u@\h]─[\w]\n└──╼ \$ '
fi
 
unset color_prompt force_color_prompt
 
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\033[0;31m\]\342\224\214\342\224\200\$([[ \$? != 0 ]] && echo \"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]root\[\033[01;33m\]@\[\033[01;96m\]\h'; else echo '\[\033[0;39m\]\u\[\033[01;33m\]@\[\033[01;96m\]\h'; fi)\[\033[0;31m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;31m\]]\n\[\033[0;31m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]\[\e[01;33m\]\\$\[\e[0m\]"
    ;;
*)
    ;;
esac

Clear the console screen after logging out to increase privacy.

~/.bash_logout
1
2
3
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

Press Alt-T after typing at the shell prompt to swap the last two words.

Type vim and then press Alt-. repeatedly to cycle through the last command-line arguments used on the command line, e.g vim .bashrc. That is the Alt key and the . key.

Press Control-A to quickly jump back to the start of the command line you have typed if you made a typo at the beginning.

Press Control-U to wipe the command line back to the beginning so you can very quickly type another command instead of using backspace. This also copies the command to the clipboard.

Then you may use Control-Y to paste the previously wiped command back to the terminal prompt. This may require Emacs mode to operate in bash.

Use this to set Emacs mode.

$ set -o emacs

Press Control-W to delete the command-line word by word. This is another quick way to edit a command you mistyped by mistake.

This strange example will list the contents of the directories under your current directory, but not recursively.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ ls -hula */
Documents/:
total 12M
drwxr-xr-x  5 john john 4.0K Jun 29 08:45  .
drwxr-xr-x 21 john john 4.0K Jun 29 08:45  ..
drwxr-xr-x  2 john john 4.0K Jun 16 05:12  4chan
-rwxr-xr-x  1 john john  17K May 26 07:41  a.out
-rwxr-xr-x  1 john john  16K May 24 09:12  dir
-rw-r--r--  1 john john  506 Jun  1 10:19  dir.c
-rw-r--r--  1 john john  535 May 24 09:12  dir.c~
-rw-r--r--  1 john john 3.9M Jun 21 08:54 'Hope is the good thing(The Shawshank Redemption 1994). [JAmspaaSNzE].f140.m4a'
-rw-r--r--  1 john john 2.0M Jun 21 08:54 'Hope is the good thing(The Shawshank Redemption 1994). [JAmspaaSNzE].f160.mp4'
-rw-r--r--  1 john john 5.9M Nov  8  2018 'Hope is the good thing(The Shawshank Redemption 1994). [JAmspaaSNzE].mp4'
drwxr-xr-x  3 john john 4.0K Jun 15 13:23  malware
-rw-r--r--  1 john john  971 May 24 09:12  monsters.c
-rwxr-xr-x  1 john john  181 May 24 09:12  my.sh
-rw-r--r--  1 john john   27 May 24 09:12  my.txt
-rw-r--r--  1 john john  733 May 24 09:12  net.py
-rw-------  1 john john    8 May 24 09:12  nohup.out
-rwxr-xr-x  1 john john  17K May 24 09:12  obf
-rw-r--r--  1 john john  858 May 24 09:12  obf.c
-rwxr-xr-x  1 john john  21K May 30 09:56  photos
-rw-r--r--  1 john john 3.3K Jun  1 10:19  photos.c
-rw-r--r--  1 john john  15K May 30 09:24  photos.c~
-rwxr-xr-x  1 john john  17K May 26 07:44  pi
-rw-r--r--  1 john john  531 Jun  1 10:19  pi.c
-rw-r--r--  1 john john  532 May 26 07:40  pi.c~
-rw-r--r--  1 john john  330 May 24 09:12  polling.c
-rwxr-xr-x  1 john john  114 May 24 09:12  search.sh
-rw-r--r--  1 john john  18K May 24 09:12  server-20220106-07:29:11.tar.gz
-rwxr-xr-x  1 john john  16K May 25 10:20  simple
-rw-r--r--  1 john john  169 Jun  1 10:19  simple.c
-rw-r--r--  1 john john    0 May 25 08:15  simple.c~
-rw-r--r--  1 john john  261 May 24 09:12  test.sh
drwxrwxrwx  2 john john  20K Jun 16 05:12  Wallpaper
 
Downloads/:
total 56K
drwxr-xr-x  3 john john 4.0K Jun 29 08:45 .
drwxr-xr-x 21 john john 4.0K Jun 29 08:45 ..
-rw-------  1 john john  35K Feb  2 09:33 GPL.txt
-rwxr-xr-x  1 john john 1018 Feb  2 09:33 install.sh
drwxr-xr-x  7 john john 4.0K Jun 16 05:12 Persepolis
-rwxr-xr-x  1 john john  582 Feb  2 09:33 readme.txt
 
website/:
total 220K
drwxr-xr-x  2 john john 4.0K Jun 29 08:45 .
drwxr-xr-x 21 john john 4.0K Jun 29 08:45 ..
-rw-r--r--  1 john john 5.4K Jun 14 11:59 example_config.xml
-rw-r--r--  1 john john  996 Jun 14 11:59 example_urllist.txt
-rw-r--r--  1 john john 110K Jun 14 12:11 me.tar
-rw-r--r--  1 john john 3.2K Jun 14 11:59 README
-rwxr--r--  1 john john  82K Jun 14 11:59 sitemap_gen.pl

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.