Useful BASH shell tips for Ubuntu and Debian alike.


The BASH shell has many useful features that allow you to perform your tasks very easily. If you are running a program in a terminal and you wish to run another command whilst the first command is running, then press CTRL-Z to move the running process to the background. Then once you have finished the other task you were performing, type fg to bring the task back to the foreground. The jobs command will print a list of the running processes in the background. In the example below I have started top and put it in the background, then started htop and backgrounded that as well.

neo@deusexmachina:~/Documents$ jobs
[1]-  Stopped                 top
[2]+  Stopped                 htop

if you type fg 2 the second process will be brought back to the foreground.

neo@deusexmachina:~/Documents$ jobs
[1]-  Stopped                 top
[2]+  Stopped                 htop
neo@deusexmachina:~/Documents$ fg 2
htop

To find the process id of a bunch of running processes, for example you need to kill a process that has multiple instances and they need to be killed all at once, then use the pidof command to find the process ids of all of the instances.

neo@deusexmachina:~/Documents$ pidof chromium-browser
8647 8645 8643

Then you would use the kill command to kill all of the processes at once.

kill `/bin/pidof chromium-browser`

If the pidof command is not in the same path on your particular system, use the which pidof command to find it.

The cut command may be used to generate a list of users with accounts on your Linux system.

neo@deusexmachina:~/Documents$ cut -d : -f 1 /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
libuuid
Debian-exim
messagebus
usbmux
saned
neo
gordon
gdm
colord
pulse
rtkit
lightdm
timidity
avahi
zeroinst
ntp

And this command will count how many users there are in this list.

neo@deusexmachina:~/Documents$ cut -d : -f 1 /etc/passwd | wc -l

To convert a MSDOS/Windows formatted text-file to UNIX line ending format the tr command will manage this perfectly well.

cat mywinfile.txt | tr -d '\015' > unixformat.txt