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 put that into the background as well.

ubuntu ~ $ jobs
[1]-  Stopped                 top
[2]+  Stopped                 htop

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

ubuntu ~ $ jobs
[1]-  Stopped                 top
[2]+  Stopped                 htop
ubuntu ~ $ 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.

ubuntu ~ $ pidof top
9077

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

ubuntu ~ $ kill `/bin/pidof top`

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.

ubuntu ~ $ 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
syslog
messagebus
landscape
sshd
pollinate
ubuntu
arma3
mysql
colord
jason
vaas
snort
xyz

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

ubuntu ~ $ cut -d : -f 1 /etc/passwd | wc -l
32

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

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

Leave a Reply