Bash basics: learn the Linux terminal — Linux beginners guide, part 5 [Guide]

This article is part of an ongoing series that hopes to bring a working knowledge of Linux to users of Windows. In the first article, we introduced Linux on a basic level. In the second article, we answered some commonly asked questions about Linux. In the third article we discussed Linux distributions and continued to answer questions. In the fourth article we looked at file managers, desktops, and installing peripherals.

One of the most powerful features of the GNU/Linux system is the command line environment. If you are or were a Windows user, you may have used the command line a few times and perhaps powershell. The Linux system provides users a much more comprehensive and powerful experience, In this article I hope to cover some of the basics of the Linux command line, so need not fear it any more.

Many people use the command line on daily basis without even knowing it. If you have every installed software using yum install or zypper in or even apt-get install, you have used a command. So let’s learn a few important commands today!

the-best-terminal-emulator-for-mac-os-x_1

History of BASH

Bash is a free and open source replacement for the Bourne shell on Unix systems. It provides users a text based interface into the system. Bash is a command processor, all that means is you use it to access another program. A great example of this is Google, you use Google to find something else. Windows power users many use the RUN prompt to quickly access WINWORD.EXE (Microsoft Word). Anyone who does this will tell you that it is faster to type a small command then click 30 times.

Command Logic

Commands are broken down into two essential components. You have the program you are invoking followed by it’s arguments or in plain English the thing you want the program to do.

touch file1 file2

As you can see above, touch is the program and file1/file2 are the arguments which are all separated by a space. In this case the touch command creates files in the current directory. In some cases like the apt-get or yum commands you can add a modifier like -y which will accept all prompts. For example sudo yum install gimp -y will install GIMP without prompting you to accept the file-system changes.

One thing that many people have a hard time with is case sensitivity. All commands and file names used in the shell are case sensitive, unlike Windows you must make sure you enter the correct case.

Important Commands

cd command

Change directory is one of the most important commands you will ever use. When you open a terminal window the default location is your Home directory and so any commands you run, will effect your user folder. For example running touch file1 will create a file called file1 in your home directory. Lets change this to your downloads folder in an example;

1. cd Downloads

2. touch file1

See if it worked, if not try using the full path for your downloads folder ‘/home/username/Downloads’

tar command

In GNU/Linux systems have a file compression system, which is similar to zip. We call it tar and you can create, extract and view the contents of your tar file directly from the command line.

Create a new tar archive.

$ tar cvf archive_name.tar dirname/

Extract from an existing tar archive.

$ tar xvf archive_name.tar

View an existing tar archive.

$ tar tvf archive_name.tar

 

grep command

grep is very useful command for search a file for a specific string. It is comparable to CTRL+F in a web browser. A GNU/Linux administrator will use grep to find certain system information or values of system files, without opening the file directly.  grep is case-sensitive and you will need to remember that.

$ grep -i "the" demo_file

Print the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text

Search for a given string in all files recursively

$ grep -r "ramesh" *

find command

The find command allows you to find files based on their name. It is similar to a windows tool called Search Everything. Find is very useful if need to find a file in a sea if badly filed files or what seems to be a yodabyte of information. Like grep find is case sensitive and you will need to make sure you remember that, otherwise you won’t find anything you want.

Find files using file-name ( case in-sensitve find)

# find -iname "MyCProgram.c"

Execute commands on files found by the find command

$ find -iname "MyCProgram.c" -exec md5sum {} \;

Find all empty files in home directory

# find ~ -empty

unzip command

If you have just made the switch from Windows and you are bound and determined to try the command line, you probability have a bunch of zips. If you want to extract or view these files in the command line, unzip is your best friend.

To extract a *.zip compressed file:

$ unzip test.zip

View the contents of *.zip file (Without unzipping it):

$ unzip -l jasper.zip
Archive:  jasper.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
    40995  11-30-98 23:50   META-INF/MANIFEST.MF
    32169  08-25-98 21:07   classes_
    15964  08-25-98 21:07   classes_names
    10542  08-25-98 21:07   classes_ncomp

cp command

The cp (copy) command is used from moving files around the system. In some cases you may need to extract a program to your /Opt directory and the best way to do that is move your files with administrative rights. In Windows you have access to the main directories by default. This is very insecure and in GNU/Linux you must be a sudoer or root user. using sudo will make this command work as the root user (administrative user)

Copy file1 to file2 preserving the mode, ownership and timestamp.

$ cp -p file1 file2

Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.

$ cp -i file1 file2

mv command

One thing you will notice in your coming years of Linux learning, is that the command line has no rename command. In order to rename a file you will need to use the mv (move) command. Move works very much like cp (copy). You will notice that I have added the -i modifier to the command, this will make mv prompt you before you make the move or rename. -i is very important because you wouldn’t want to rename something critical by mistake.

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.

$ mv -i file1 file2

Note: mv -f is just the opposite, which will overwrite file2 without prompting.

mv -v will print what is happening during file rename

$ mv -v file1 file2

chmod command

In Windows you would have at some point needed to change the security on a file. You might have needed to share it or prevent other users from accessing it. In GNU/Linux we use a command called chmod.

chmod command is used to change the permissions for a file or directory.

Give full access to user and group (i.e read, write and execute ) on a specific file.

$ chmod ug+rwx file.txt

Revoke all access for the group (i.e read, write and execute ) on a specific file.

$ chmod g-rwx file.txt

Apply the file permissions recursively to all the files in the sub-directories.

$ chmod -R ug+rwx file.txt

Make a shell file executable.

$ sudo chmod u+x program.sh or program.run

mkdir command

mkdir is a command that we use to create a new folder. In the graphical side of GNU/Linux or Windows, all you need to do is right-click and select “New Folder”. mkdir is easier to use if your in the command line side of the system.

Following example creates a directory called temp under your home directory.

$ mkdir ~/temp

Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.

$ mkdir -p dir1/dir2/dir3/dir4/

 ifconfig command

In the Windows command prompt, you can view all your network information using the command ipconfig. In GNU/Linux we use the command ifconfig, it provides the same information as ipconfig. ifconfig offers you the power to enable or disable a network connection without a graphical tool.

View all the interfaces along with status.

$ ifconfig -a

Start or stop a specific interface using up and down command as shown below.

$ ifconfig eth0 up

$ ifconfig eth0 down

whatis command

Whatis command displays a single line description about a command. So if any point you need to find out what a command does simply use whatis.

$ whatis ls
ls		(1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface

Going total terminal(inator)

If you want to save battery life, access things quickly or just seem like a cool hacking wizard to your friends you can do that. GNU/Linux allows you to have multiple sessions on the same computer. I have a graphical desktop running, but most of my time is spent on the text based user interface (tui).

To launch or switch a session all you need to do is use a keyboard combo. CRTL+ALT+F1/2/3/4/5/6/7/8/9 and when you want to switch back to your graphical interface simply cycle through the CTRL+ALT+F1….9 keys.

In the next article

In the next article. we are going to begin wrapping up the series with securing your computer and using free technologies to do so. Thanks for reading.

Related Posts