Bash and Terminal tutorial

Lesson 1: Going through directory tree and listing files and folders

First line that you will get in terminal is: alexander@pc:~$

This means that current user is “alexander” at machine named “pc”. ~ means that the current directory is home directory, which is /home/alexander. “$” at the end marks that this is the regular user, while root user will have “#” instead of “$” sign. To switch to root user use: sudo su root, to return from the root user just type exit, and to use root permissions only for the given row, type sudo and than command you with to execute as a root.

man – command that shows what other commands do and how to use them.
Example that shows how to use “ls” command: man ls

If you do not know which command to use for the task you need, you can use command “apropos”. Example that will find commands related to the keyword “zip”: apropos zip

ls – command that is the same as “dir” in Windows and DOS. It lists files and directories. Its most important options include the one that lists all the properties: ls –l, and the one that lists hidden files and directories as well: ls –a.

cd command is the same as in Windows. However, instead of backslashes “\”, Linux uses slashes “/”. To go to the home directory enter command: cd  /home/alexander/ or just cd ~.
To go to the root: cd /
Go one directory up: cd ..
Go two directories up: cd ../..

To make directory use command mkdir in the form: mkdir [name]

To delete empty directory use command: rmdir

If the directory is not empty, use this command, but with caution: rm –rf [path to directory or file]

Lesson 2: Copying, moving and renaming files and directories, and writing into text files

cp copies directories and files. Syntax: cp [path to what we want to copy] [path where we want it copied]. We can use wildcards if we want to copy lots of files: * or ?. To copy set of files or directories we can enter all those paths to the files we want to copy, and at the end path to directory: cp [file1] [file2] [file3] [path where we want it copied]

mv moves files or directories, also it is being used to rename them: mv [file or dir] [path where it will be moved]. In the path where we want it moved, we can enter some other name and have it moved to another place with another name. If the path is the same and the name is different, we will rename the file.

To write some text into a file thus replacing the whole text inside: echo [text we want to write] > [file]
If we want to append text we will use >>. Example: echo Marko Polo >> listofexplorers.txt

Lesson 3: Searching and displaying results

To see what is inside some file we can use cat command. Example: cat listofexplorers.txt

Searching text inside list of files can be done with grep. For example search for word netbook inside all the files in the current directory: grep netbook *
Option –i is used when we do not want search to be case sensitive:  grep –i netbook * will search for words “netbook” and “Netbook”, but also “NetBook”.
-r option means recursive for almost all commands. It searches within all subdirectories.
To search more than one word, we must enclose the search term: grep ‘netbook eee’ *
If we want to write our search result into some text file: grep netbook * > searchResult.txt

Searching in linux can be done with locate and find commands. Locate goes through index, so it is faster, but find searches for files with the given path, without using index. The same options –i and –r apply, in general it is best to use them both: -ir (r for locate does not make sense, only for find): locate –i netbook
Example: list all the files named netbook that contain the word eee: locate –i netbook | grep eee
What we did here is we used pipe “|” to pass the results to another command.
Example: find in home directory all the files that contain eee in their name and we want to search only for files, not directories (option –-type f): find ~ --name *eee* --type f

Lesson 4: Conditions and Variables

Variables begin with a dollar sign. To assign a value to a variable, $ sign is not being used, just when we want to recall it. Example: assign text “netbook” to variable “eee”: eee=netbook.
Example: write out variable in a sentence: echo I love my $eee eee. This will write out: “I love my netbook eee”.

Inside [] is test for if command. If the condition is true, it does the next command.

NumbersTextFiles
OptionMeaningOptionMeaningOptionMeaning
-eqequal=identical-ddirectory
-nenot equal!=not identical-ffile
-ltless thanvariabletext in variable is defined-rreadable file
-leless than or equal-n variabletext length in variable is greater than zero-sfile length is greeater than zero
-gtgreater than-z variableempty variable is defined-wwritable file
-gegreater than or equal-xexecutable file

if command we must end with fi.
Example:
if [42>1]; then echo super else 'the universe has collapsed!' fi

Example for for command what downloads all of the 50 jpg files that are from certain web address:
url='http://leeenux-linux.com/image'
for i in $(seq 1 50) do wget $url$(printf $i).jpg
done

Same example for command while:
i=1
url='http://leeenux-linux.com/image'
while [i -le 50] do wget $url$(printf $i).jpg
((i++))
done

Until command:
i=1
url='http://leeenux-linux.com/image'
until [$i -gt 50] do wget $url$(printf $i).jpg
((i++))
done

Lesson 5: Bash

In leafpad, gedit or other text editor we can create .sh file. Type in Terminal:
gedit script.sh

To make our script executable enter this command: chmod +x script.sh

To make it executable for all users: chmod u+x script.sh

To give read write execute permissions for all the users: chmod 777 script.sh

To change owner attributes we can use chown command

The first line of our script must be: #!/bin/bash, which calls out bash. There are others, like sh, zash etc, but the most widely used is bash.

You can enter the commands in the previous lessons. It executes the second row, than the third, than fourth etc, until the last line.

To execute a script type in terminal: ./script.sh

If the script is not executable, we must call sh: sh script.sh

Leave a Reply