Bash commands: A beginners' guide, Part 1

Bash commands: A beginners' guide, Part 1

The shell is a program that takes commands from the keyboard and gives them to the operating system to perform. Bourne Again Shell(Bash) is a shell program written as an upgrade to the Bourne shell(sh) and can be easily defined as a command-line interpreter that typically runs in a text window where the user can interpret commands to carry out various actions. The combination of these commands as a series within a file is known as a Shell Script. You can run various commands on bash. These commands can be grouped into Navigation Commands, File Navigation Commands, Permission Commands, Filtering and Searching commands System and Process commands

These are commands that enable you to work with directories and links. Let us look at some of them:

  1. Pathnames A path is a unique location to a file or folder in a file system in an OS. Paths can be absolute or relative,An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from the start of the actual file system from / directory. A relative path is defined as the path related to the present working directly(pwd). It starts at your current directory and never starts with a / . Let us see these types of paths in action.

    Let us say we are located at project/code/ and we want to move to project/code/tests . Doing this with an absolute path

     $ pwd
     /project/code/
     $ cd /project/code/tests
     $ pwd
     /project/code/tests
    

    With a relative path it is as simple as;

     $ pwd
     /project/code/
     $ cd tests
     $ pwd
     /project/code/tests
    
  2. Check current directory In the absolute and relative path examples, we used the pwd command which stands for print working directory. This is used when you want to find out the current directory.

  3. Making a new directory To create a new directory make use of the mkdir command followed by the name you want to give to your directory.

    $ mkdir newHome
    
  4. Switch between directories Suppose we are in our home directory and we want to switch between directory1 and directory2;

    $ pwd
    /home/
    $ cd directory1/
    $ pwd
    /home/directory1/
    $ cd ~/directory2/
    $ pwd
    /home/directory2/
    
  5. Display and remove files To display all files in a directory we use the ls command. To do this just navigate to any directory and type in ls, a list of all files. This command has many options, type man ls to view them. To remove all files in a directory, the command rm -r directory-name is used. Let us see an example
     $ pwd
     /home/stuff
     $ ls
     somefile.txt otherfile.md oldstuff
    
    The stuff directory has two files and another directory inside it,the files are somefile.txt and otherfile.md and the directory is oldstuff To delete everything, we would just have to do:
     $ cd ..
     $ rm -r stuff/
    
    Always remember to navigate out of the directory you want to delete first, that's why the cd .. command is used.

File Navigation Commands

Basic file operations include; creating, removing, opening, moving and copying. Bash has commands to handle each of these. Let us handle each of these.

  1. Creating files
    $ touch newfile.txt
    
  2. Removing a file
    $ rm newfile.txt
    
  3. Opening a file Assume we have a file file.txt whose contents are This is a text file. To display the contents of this file in bash,
     $ cat file.txt
     This is a text file
    
  4. Moving files Moving files between directories is done using the mv command with the filename and directory to be moved into.
    $ pwd
    /home/
    $ mkdir code 
    $ ls
    helloworld.py code 
    $ mv helloworld.py code
    $ ls
    code
    $ ls code
    helloworld.py
    
  5. Copying files Copying files is a lot similar to moving files but instead of the mv command, cp is used. As an example instead of moving helloworld.py from the previous example, copy it instead to code folder.
    $ pwd
    /home/
    $ mkdir code 
    $ ls
    helloworld.py code 
    $ cp helloworld.py code
    $ ls
    helloworld.py code
    $ ls code
    helloworld.py
    
    That concludes the first of this two-part series. See you in the next one.