Bash Basics

Bash Basics

Basic shell commands

·

3 min read

The BASH shell is one of the best shells available in the successors of UNIX such as GNU/Linux. Knowing common commands on how to manipulate it is a handy skill that is crucial in navigating the system. Bash works best using the command line. For a bigger explanation of the shell and the terminal. See %https://codetree3.hashnode.dev/simple-bash-basics for an explanation of how to run commands on the bash shell.

Current working directory

The path of the current working directory is printed by the command pwd. This command will give the current path, from the root to the working directory the user is currently on. A simple script to achieve this would be:

#!/bin/bash
pwd

listing directory contents

The ls command is used to list the contents of a directory. Of course, it takes different options but the default command lists the contents of the current directory (the one given by pwd ).

options

To list the contents in a long format, use the -l option i.e. ls -l . Now most (if not all) commands on the GNU/Linux system take options. The ls command takes several options as well (man ls to learn more). These options can be combined to give a desired output. For example, the option to list hidden files ( they begin with . ) is -a. However, it could be combined with the -l option to list both in long format and show hidden files as in ls -al.

Other directories

To list the contents of a different directory give the ls command the path to that file. For example to list the contents of the etc directory: ls /etc. Of course, you can use the options together with the file name ls -alh /etc. Now when giving the path, it is important that the path given, whether relative or absolute end with a directory.

Changing Directories

The cd command usually changes the directory to the directory at the end of the path given to it. To change to the etc directory cd /etc. Now the path given could be relative or absolute. To move to the parent directory cd .. since .. gives the path of the parent directory, (one level from the one you are at). Now the cd command possesses some handy shortcuts so that you can move to the home directory without specifying the full path by using cd without any arguments. It goes to the previous one with cd -.

Make Directories

To create a directory, the mkdir command along with the name of the directory. To create multiple directories, separate the directory names with a space. To create nested directories i.e. a dorectory within another use the -p option with mkdir. An example would be mkdir -p home/sittingroom/chair. This command would create the home directory and within it sittingroom and within it, chair. For more manipulations while making directories man mkdir.

Copy, move, Delete and rename

To copy a file, the cp command is used. When it is a directory do it recursively, use the -r option. i.e. cp -r dir.

To move files from one directory to another use the mv command. It is done by giving the path of the source file first and then the destination directory path second. This command is used to rename. In this case, the path is not changed but the second argument is simply the new name.

To delete, the rm command is used. This command works with files and takes options similar to the ls command. To delete directories, delete recursively using the -r option.

Command-basics

There are many commands in Linux on bash that come as needed. With practice, these commands and their options would be at the fingertips. However, these are the most common commands used on the terminal. With Bash, these commands take options to aid in performing basic tasks and do them well and easily.