Skip to main content

Get Started with bash

bash

Adding permissions

Make a shell script executable by the user/owner

chmod u+x deploy.sh # You can then execute it like this: ./deploy.sh
chmod 444 file # - Allow read permission to owner and group and world
chmod 777 file # - Allow everyone to read, write, and execute file

The command used to change permissions is chmod
```bash
chmod g+w hello.txt

Here, I am adding (+) the permission to write (w) for the group (g) users.

Similarly, if I want to remove the read permission for anyone outside the group, then:

chmod o-r hello.txt

Here, I am removing (-) the permission to read (r) for the others (o)

Create a file

touch hello.txt

Read and stream a file tail command is similar to cat in a way that it lets you read a file.

But unlike cat you can define how you want to view it.

For e.g. if you want to see only the last 100 lines of a log file (log files contain thousands of lines of text) you can use the following command

tail -n100 log_filename

An added advantage of tail command is that you can use it to stream a file as well

tail -f filename

This command will give you a streaming output that keeps updating as the log file is updated.

Filtering the output of a command The above commands to read through a file can produce hundreds of lines of output. But that will make it difficult for you to go through them.

You can filter out the output so any line that contains a substring you want to see will be shown in the output.

Let me introduce you to pipe in unix. A pipe is used to send the output of a certain command to a different command. A pipe is represented by | .

grep is the command you use to filter out an output. If you pass the output of tail to the grep command via a pipe, that should do the trick of filtering lines.

Let’s say you only want to view only the lines in a file that contain the word gunicorn in it then this is how you do it

tail -n100 log_filename | grep 'gunicorn'

A much more powerful tool for filtering is awk

awk follows the pattern

awk options 'selection _criteria {action }' input-file

An example of awk command is like this

awk '/gunicorn/ { print $1 }' log_filename

Here, the command will print out the first word (indicated by $1) of lines that contain the word “gunicorn” More info awk-command