Posted: . At: 11:15 PM. This was 7 years ago. Post ID: 4760
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.

Some interesting and useful Linux commands and BASH tricks.

A useful awk implementation to count the number of entries in the /etc/passwd file.

john@deusexmachina:~$ sudo awk -F: '{ print $1 }' /etc/passwd | wc -l
34

And the quintessential “Hello World” in Awk.

john@deusexmachina:~$ awk 'BEGIN { printf "%s, %s\n", "Hello", "World!" }'
Hello, World!

This command will change your login shell if you wish to use an alternative.

john@deusexmachina:~$ chsh -s /bin/tcsh
Password:

Shell quoting is a powerful feature of the BASH shell. This example shows how we are using a command within a command to generate output. Inception in BASH!

john@deusexmachina:~$ echo "You have `ls | wc -l` files in `pwd`"
You have 17 files in /home/john

This example shows you may use the output of the ls command to provide a list of files to search for that contain a certain word or phrase.

john@deusexmachina:~/Desktop$ egrep '(Example)' `ls`
examples.desktop:Name=Examples
examples.desktop:Name[en_AU]=Examples
examples.desktop:Name[en_CA]=Examples
examples.desktop:Name[en_GB]=Examples
examples.desktop:Name[sco]=Examples
examples.desktop:Comment=Example content for Ubuntu
examples.desktop:Comment[en_AU]=Example content for Ubuntu
examples.desktop:Comment[en_CA]=Example content for Ubuntu
examples.desktop:Comment[en_GB]=Example content for Ubuntu
examples.desktop:Comment[sco]=Example content fur Ubuntu

Another way to print the current directory that you are in at the terminal or virtual console.

john@deusexmachina:~$ echo ${PWD#*/}
home/john

And finally; this command will print out the last ten commands executed on your terminal.

john@deusexmachina:~$ fc -l -10
140	 ls -hula | 's/Example/Content/'
141	 tail=${PWD##*/} 
142	 ls -hula | tail=${PWD##*/} 
143	 ls -hula | tail=${PWD#*/} 
144	 ls -hula | tail=${PWD##*/} 
145	 cd
146	 ls -hula | tail=${PWD##*/} 
147	 fc -l -10
148	 echo ${PWD##*/}
149	 echo ${PWD#*/}

if you want to edit the last command entered at the BASH shell; use this command: fc -e vi. This is good if it is a very long and complex command and you want an easier way to edit it.

2 thoughts on “Some interesting and useful Linux commands and BASH tricks.”

Leave a Comment

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