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
Navigation Commands
These are commands that enable you to work with directories and links. Let us look at some of them:
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 toproject/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
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.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
Switch between directories Suppose we are in our home directory and we want to switch between
directory1
anddirectory2
;$ pwd /home/ $ cd directory1/ $ pwd /home/directory1/ $ cd ~/directory2/ $ pwd /home/directory2/
- 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 commandrm -r directory-name
is used. Let us see an example
The stuff directory has two files and another directory inside it,the files are$ pwd /home/stuff $ ls somefile.txt otherfile.md oldstuff
somefile.txt
andotherfile.md
and the directory isoldstuff
To delete everything, we would just have to do:
Always remember to navigate out of the directory you want to delete first, that's why the$ cd .. $ rm -r stuff/
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.
- Creating files
$ touch newfile.txt
- Removing a file
$ rm newfile.txt
- Opening a file
Assume we have a file
file.txt
whose contents areThis is a text file
. To display the contents of this file in bash,$ cat file.txt This is a text file
- 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
- 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 movinghelloworld.py
from the previous example, copy it instead tocode
folder.
That concludes the first of this two-part series. See you in the next one.$ pwd /home/ $ mkdir code $ ls helloworld.py code $ cp helloworld.py code $ ls helloworld.py code $ ls code helloworld.py