Linux For Beginners Part 3 - Basic BASH

In the previous articles, we have learned a lot about the Linux, what the terminal is, the Linux file systems and what each file directory in the file system does. In the final part of this series, we are going to be diving deep into the bash terminal and play around some Linux commands. Here we go.

Prerequisite

You need to have Linux installed on your machine. If you don’t kindly visit the part 1 of this series.

Getting Started

Earlier, we established that Linux file system comprises of a tree of directories, where each directory is either a parent directory or is a sub-directory(that is it is contained within a single directory). Navigating backwards from one sub-directory in the tree will always get you to the parent directory in that tree. When working within a file system, the user is always working within some directory, which we call the working directory. Let us understand this:

Open your BASH terminal, you will be greeted will the same information you saw in the part 2 of this series. To check the current directory the terminal is, there is a Linux command known as pwd. pwd stands for ‘print working directory’ , it prints the current user’s directory to the terminal.

if you type pwd on your terminal, you will get a result like:

/home/jackhoudini

This means you are in the directory associated with your username which has the home directory as the parent. Recall the home directory is what contains every created users file and data. Let us see other commands:

To move around the file system, you must know what files are there before moving and there is a command known as ls. The ls commands list the content of the current working directory.

    jackhoudini@houdini:~$ ls
100-Days-of-Go                             home.go
2020-08-08-191259_1280x776_scrot.png       intermediate-python
2020-08-08-191447_1280x800_scrot.png       js-boilerplate
2020-08-08-191449_1280x800_scrot.png       mac-fonts.zip
2020-08-08-191453_1280x776_scrot.png       Music
2020-08-25-173822_1280x800_scrot.png       nickel
2020-08-25-173823_1280x800_scrot.png       nickel2
2020-08-25-175714_1280x800_scrot.png       nickelsf
abc.sh                                     node_modules
backup                                     nonnytoast
Beginning-Deno                             package.json

As you can see, it lists the entire file that belongs to the current working directory which is /home/jackhoudini. If you need to find hidden files(dotfiles), simply pair the command with -a like this:

    ls -a 

If you need to see the user permissions on each directory or file, simply pair it with ls -l. You can also combine both commands like ls -al for more details.

cd - The next command stands for change directory. It allows a user to move back and forth between a parent and sub-directory. To move into a sub-directory, we use:

  cd <sub-directory>

To move from a sub-directory to the parent, we simply do:

  cd ..

To move back and forth, after accessing a particular directory, a little shorthand will be to do :

  cd -

We can also jump multiple directory levels

  cd ../..

Getting Help

If you need help on what commands to use, the bash terminal has an in-built manual that contains a list of what a particular command does and the options you can chain each command with. To access this commands simply use the man command on any command you want to use. For example, man ls will generate this :

     LS(1)                      User Commands                  

    NAME
     ls - list directory contents

    SYNOPSIS
      ls [OPTION]... [FILE]...

   DESCRIPTION

Creating and Deleting Files and Directories

Here is a list of helpful commands to create files and directories

touch- This command will create a file on your Linux system, all you need to do is specify the file extension like this :

   touch index.js

You can also create multiple files at the same time in a working directory with this command:

   touch <file1> <file2> <file3>

mkdir - This command is will create directories in a particular working directory. You simply do:

      mkdir <dirname>

Creating multiple files is the same with the touch command:

      mkdir <dirname1> <dirname2>

You can also create a directory that will contain multiple sub-directories. Look at this:

        mkdir -p /parent-dir/sub-dir/sub-dir of sub-dir 

This command will generate a folder structure like:

        parent-dir
           |-sub-dir
                |-sub-dir of sub-dir

It creates sub-directories in the specified parent directories.

To delete a file or directory we simply run:

          rm <dirname> or  rm  <filename>

Alternatively We can run:

          rmdir <dirname>

This will delete only directories and not file The above commands will only delete directories and files that are empty and not directories that contain files in it. To achieve that Linux offers two options: to remove recursively or to remove forcefully.

To remove recursively we simply do:

            rm -r <dirname> or rm -r <filename>

            rm -f <dirname> or rm -r <filename>

Alternatively, we can chain both commands:

            rm -rf <dirname> or rm -rf <filename> 

Viewing and Editing Contents of a File

The bash terminal provides commands like head, less, more, cat, nano and tail to accomplish this.

nano - The nano commands is a command-line text editor. it is used to edit files on Linux and it offers an easy and minimalistic approach of doing this.

To see this, create a file using touch and open it using nano like:

     nano <filename>

Write anything you want. To save simply press [ ctrl + o ] or [ command + o ] if you are on a mac and exit the text editor with [ ctr + X] + Enter .

head - head outputs the first few lines of a file. When paired with the -n flag, it specifies how many lines it should read. The default is the first 10 lines of a file.

             head   -n   3   hello.txt 

tail - head outputs the last few lines of a file. It can be paired with the -n flag like the head command above

less - less opens up a file in a read-only mode. It shows you the contents of a file in a vim-like editor.

more - more is the opposite of less and it is highly favoured over less. it is a filter for paging through text one screenful at a time.

cat - This commands serves two purposes. It reads a file and concatenates the contents of two files together and prints the output.

             cat a b 

Moving, Copying Files, and Viewing Command History

mv – The mv command is used to move a file or directory and to also rename a file or directory. It could be somewhat misleading because the commands for both operations remain the same but with a little twist.

To rename a file, simply do:

              mv filename new filename

The above command will change the filename to the new name you specified while keeping the file in the directory. However, to move a file from one directory to another, you need to know the path to the current directory and the path to the directory you are moving to. Observe:

         mv ~/desktop/testfile  ~/documents/

In the above command, I am moving the testfile in the desktop directory belonging to my folder on the Linux file system, to the documents directory. Notice there is a trailing forward slash, this indicates that the file should end up in that directory and still maintain the filename.

However, If I had added a filename other than the one it possesses, the file will be moved and renamed. You can also move multiple files at once, the Linux filesystem offers a wildcard attribute you can chain to any command. Notice this:

        mv / home/downloads/*.js  /home /jsfiles/ 

In the above command, I am moving every file with the js file extension in the downloads directory (belonging to the home parent directory) to the jsfiles extension of that same directory.

cp – the cp command makes copies of files and directories. Let's say you have a file named picture.jpg in your working directory, and you want to make a copy of it called picture-02.jpg. You would run the command:

       cp picture.jpg picture-02.jpg

Here, picture.jpg is the source of the copy operation, and picture-02.jpg is the destination. Both files now exist in your working directory. You can also copy multiple files from one directory to another directory. Take a look:

        cp ~/pictures/*.jpg ~/picture-backup

Notice, we are using the wildcard(which is indicated by the asterisk, ‘*’) to copy every file in the pictures directory that have the .jpg extension.

You can use cp to copy entire directory structures from one place to another using the -R option to perform a recursive copy. Let's say you are the user John and you have a directory, /home/john/files, which contains many files and subdirectories. You want to copy all those files, and all the subdirectories (and the files and subdirectories they contain), to a new location, /home/john/files-backup. You can copy all of them using the command:

        cp -R  ~/files ~/files-backup

The entire directory structure will be copied to the directory /home/john/files-backup. When performing a recursive copy: • If the directory files-backup already exists, the directory files will be placed inside. • If files-backup does not already exist, it will be created and the contents of the files directory will be placed inside it.

history - Linux shell saves a history of the commands you run, and you can search it to repeat commands you’ve run in the past. Once you understand the Linux history command and how to use it, it can significantly boost your productivity. In its simplest form, you can use the history command by just typing its name: history The commands are numbered, with the most recently used (those with the highest numbers) at the end of the list.

Miscellaneous

There are some noteworthy commands that I wanted to cover but because I intend to keep this article simple, I am going to brush through them.

su - su switches you to the root user account and requires the root account’s password. It is used this way:

     su 

You will be prompted to supply the password belonging to the root user to perform root privileges.

sudo – Sudo runs a single command with root privileges – it doesn’t switch to the root user or require a separate root user password. It is used this way:

 sudo <whatever command that requires the superuser to perform>

apt install / yum install – apt and yum are package managers for Debian flavours of Linux and RedHat flavour of Linux respectively. Apt means aptitude and yum stands for yellow-dog updater modified. The ‘install’ command is used to install programs and software on your Linux system based on the package manager you are using. Most times you will require root privileges to install packages on your system, a notable way to do this is:

  sudo apt install  <program-name> or

  sudo yum install <program-name>

Conclusions

We have come to the end of this series. There are over 150 BASH commands on Linux and many other commands on other shell types. Learning to live in the Linux terminal is a skill one masters based on continuous repetition, if you want to learn more about the shell, I recommend this book Unix and Linux System Administration Handbook. . If you have any questions or require any assistance as regards this article or anything Linux, Feel free to leave them in the comment section or shoot me a DM on twitter @jackhoudini__, I'll be more than happy to help.

Thank you for reading!