People Innovation Excellence

A Beginners Guide to BASH (Part 3)

4. Multiple Comments

i. Command coordination
We can use “;” to execute commands sequentially
Cd DirName ; ls : will cd to DirName then execute ls command
We can also use logical operators && and ||
Cd x && ls
Assuming that directory x doesn’t exist, the command above will not run the ls command, the && will only run the next command if the previous one succeeded.
Ls && cd x
The command above will list all files in the current directory, and then lists an error message.
Cd x || ls
The command above will print an error message then lists all files in the current directory.
Ls || cd x
But the in the example above, the command will list all files in the current directory, and doesn’t print the error message, this is because || will not run the next command if the previous one succeeded.

ii. Pipe and Tee
To count words / characters / lines we use the command wc with its options –c to count characters, -w to count words and –l to count lines. To count the number of files in a directory we can do the following
Ls > temp : redirects output of ls to file temp
Wc –l < temp : use temp as an input for the command wc –l
Rm –f temp : here the rm (remove) with the flag –f (without prompt) will delete the file temp.

The method above could work, but problems may still occur, such as:
– What if there is a folder named temp in the directory?
– What if 2 users run commands that use temp in the same folder at the same
– The program may count “temp”, making the output 1 number more
To solve the problems the method above has, we use pipe “|”. By using it we can directly “feed” the next command the output of the first command
Ls | wc –l
The solution above shows the function of counting files in the directory without using a temp file.

iii. Xargs and “
Sometimes using pipe just won’t do. Assume we have 3 files in a directory, namely a.txt, b.txt, and c.txt, and we wanted to count the number of lines in each file whose names ends with .txt.

5. Wildcard Pattern
Notice the use of * in the previous example (wc –l ` ls *.txt `), it is a standard wildcard. Wildcard patterns consists of the following :

(1) ? represents any 1 character
(2) * represents any number of characters (including 0)
(3) [] specifies a range or option
a. M[a,e,i,o,u]* will match anything that begins with Ma Me Mi Mo or Mu
b. M[a-z]* will match any words starting with M followed by any lower case alphabet
(4) \ is an escape character notice how we use it in an early example (CD File\name\ with\ space) to escape the space character.
(5) {} specifies an or statement, {*.txt,*.doc,*.png} will match any words ending with .txt .doc or .png
(6) [!] is the opposite of [], if we do M[!E]* , will match to any word starting with M and followed by any characters except E , i.e. the word Meta, Mom, Male will be accepted but not Meta and so on.

Note: as a computer science student I do use the terminal regularly but not on anexpert level, the methods mentioned above may not be the best and may contain some mistakes, but are the ones I use regularly and do their jobs well. I apologize if there exist errors in any form in the article above, and thanks for your time.


Author: Michael Gunawan (1901500111)
Supervisor: Dewi Suryani, S.Kom., M.Eng. (D5878)

Published at : Updated
Written By
Dewi Suryani, S.Kom., M.Eng.
Subject Content Coordinator Database Systems | School of Computer Science

Periksa Browser Anda

Check Your Browser

Situs ini tidak lagi mendukung penggunaan browser dengan teknologi tertinggal.

Apabila Anda melihat pesan ini, berarti Anda masih menggunakan browser Internet Explorer seri 8 / 7 / 6 / ...

Sebagai informasi, browser yang anda gunakan ini tidaklah aman dan tidak dapat menampilkan teknologi CSS terakhir yang dapat membuat sebuah situs tampil lebih baik. Bahkan Microsoft sebagai pembuatnya, telah merekomendasikan agar menggunakan browser yang lebih modern.

Untuk tampilan yang lebih baik, gunakan salah satu browser berikut. Download dan Install, seluruhnya gratis untuk digunakan.

We're Moving Forward.

This Site Is No Longer Supporting Out-of Date Browser.

If you are viewing this message, it means that you are currently using Internet Explorer 8 / 7 / 6 / below to access this site. FYI, it is unsafe and unable to render the latest CSS improvements. Even Microsoft, its creator, wants you to install more modern browser.

Best viewed with one of these browser instead. It is totally free.

  1. Google Chrome
  2. Mozilla Firefox
  3. Opera
  4. Internet Explorer 9