Some miscellaneous BASH tricks. Useful tips for all Linux users.

This command shows how to display a listing of files with ls and show line numbers at the same time.

[email protected] ~/Desktop/Site
 % ls | nl
     1	back.jpg
     2	bejiitas_phpb1
     3	bejiitas_phpb1.sql
     4	cgi-bin
     5	Files.tar.lzma
     6	Map07Tufb.zip
     7	my2.tar.lzma
     8	program.c
     9	quake38.jpg
    10	sysinfo
    11	tekcrazy_2.zip
    12	wrap.rar
    

if you wish to not see any of your executed commands listed in the .bash_history file, then put this line in your .bashrc file. This will prevent the executed commands from being listed and this is a good security tip.

unset HISTFILE

These environment variables below are useful variables to put into your .bashrc file. These define the mcedit editor as the default text viewer and editor. Also the default pager and the dircolors value.

export EDITOR=mcedit
export DOOMWADDIR="/usr/local/games/doom" # For doom!
# These two examples from bash-doc.
export PAGER="/usr/bin/less"
export VIEWER="mcedit"
export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
shopt -s checkwinsize

[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"

This is how to set the default date output when listing files with ls.

export TIME_STYLE=full-iso

This is the date output when using the full-iso value in the TIME_STYLE variable.

bash 11:39:34 Wed Jul 10 [[email protected] $ ls
total 6.7M
drwxr-xr-x 4 homer homer 4.0K 2013-07-10 10:31:03.781905207 +1000 .
drwxr-xr-x 3 homer homer 4.0K 2013-07-09 12:43:10.993410527 +1000 ..
-rw-r--r-- 1 homer homer  15K 2013-06-09 18:54:11.015889660 +1000 back.jpg
-rw-r--r-- 1 homer homer   10 2013-07-10 10:31:04.441905228 +1000 bejiitas_phpb1
-rw-r--r-- 1 homer homer 1.8M 2013-07-10 10:31:04.453905229 +1000 bejiitas_phpb1.sql
drwxr-xr-x 3 homer homer 4.0K 2013-07-10 10:31:03.805905208 +1000 cgi-bin
-rw-r--r-- 1 homer homer 4.3M 1970-01-01 10:00:00.000000000 +1000 Files.tar.lzma
-rw-r--r-- 1 homer homer 300K 1970-01-01 10:00:00.000000000 +1000 Map07Tufb.zip
-rw-r--r-- 1 homer homer 4.3K 1970-01-01 10:00:00.000000000 +1000 my2.tar.lzma
-rw-r--r-- 1 homer homer 1.2K 2013-07-10 10:31:04.441905228 +1000 program.c
-rw-r--r-- 1 homer homer 188K 2013-06-11 22:57:01.759458966 +1000 quake38.jpg
drwxr-xr-x 2 homer homer 4.0K 2013-07-10 10:31:03.821905208 +1000 sysinfo
-rw-r--r-- 1 homer homer  13K 1970-01-01 10:00:00.000000000 +1000 tekcrazy_2.zip
-rw-r--r-- 1 homer homer 7.6K 2013-07-01 23:31:15.589473021 +1000 wrap.rar

This is the output you get when using the long-iso value.

bash 11:42:16 Wed Jul 10 [[email protected] $ ls
total 6.7M
drwxr-xr-x 4 homer homer 4.0K 2013-07-10 10:31 .
drwxr-xr-x 3 homer homer 4.0K 2013-07-09 12:43 ..
-rw-r--r-- 1 homer homer  15K 2013-06-09 18:54 back.jpg
-rw-r--r-- 1 homer homer   10 2013-07-10 10:31 bejiitas_phpb1
-rw-r--r-- 1 homer homer 1.8M 2013-07-10 10:31 bejiitas_phpb1.sql
drwxr-xr-x 3 homer homer 4.0K 2013-07-10 10:31 cgi-bin
-rw-r--r-- 1 homer homer 4.3M 1970-01-01 10:00 Files.tar.lzma
-rw-r--r-- 1 homer homer 300K 1970-01-01 10:00 Map07Tufb.zip
-rw-r--r-- 1 homer homer 4.3K 1970-01-01 10:00 my2.tar.lzma
-rw-r--r-- 1 homer homer 1.2K 2013-07-10 10:31 program.c
-rw-r--r-- 1 homer homer 188K 2013-06-11 22:57 quake38.jpg
drwxr-xr-x 2 homer homer 4.0K 2013-07-10 10:31 sysinfo
-rw-r--r-- 1 homer homer  13K 1970-01-01 10:00 tekcrazy_2.zip
-rw-r--r-- 1 homer homer 7.6K 2013-07-01 23:31 wrap.rar

This is a good way to format the output of ls to suit whatever you are using it for.

Another way is to use strftime formatting and show the date like this.

bash 11:48:32 Wed Jul 10 [[email protected] $ export TIME_STYLE="+%d-%m-%y %H:%M:%S %P"

This will give you this output.

bash 11:48:44 Wed Jul 10 [[email protected] $ ls
total 6.7M
drwxr-xr-x 4 homer homer 4.0K 10-07-13 10:31:03 am .
drwxr-xr-x 3 homer homer 4.0K 09-07-13 12:43:10 pm ..
-rw-r--r-- 1 homer homer  15K 09-06-13 18:54:11 pm back.jpg
-rw-r--r-- 1 homer homer   10 10-07-13 10:31:04 am bejiitas_phpb1
-rw-r--r-- 1 homer homer 1.8M 10-07-13 10:31:04 am bejiitas_phpb1.sql
drwxr-xr-x 3 homer homer 4.0K 10-07-13 10:31:03 am cgi-bin
-rw-r--r-- 1 homer homer 4.3M 01-01-70 10:00:00 am Files.tar.lzma
-rw-r--r-- 1 homer homer 300K 01-01-70 10:00:00 am Map07Tufb.zip
-rw-r--r-- 1 homer homer 4.3K 01-01-70 10:00:00 am my2.tar.lzma
-rw-r--r-- 1 homer homer 1.2K 10-07-13 10:31:04 am program.c
-rw-r--r-- 1 homer homer 188K 11-06-13 22:57:01 pm quake38.jpg
drwxr-xr-x 2 homer homer 4.0K 10-07-13 10:31:03 am sysinfo
-rw-r--r-- 1 homer homer  13K 01-01-70 10:00:00 am tekcrazy_2.zip
-rw-r--r-- 1 homer homer 7.6K 01-07-13 23:31:15 pm wrap.rar

This shows how flexible the output of the terminal is. This is a good way to change the output. The strftime formatting method shows how to set any date and time format you wish. Type man strftime to read about the available formatting options.

No comments have been made. Use this form to start the conversation :)

Leave a Reply