Working With Shell

Script Koder - Sep 12 - - Dev Community

What is a shell?

  • Linux shell is a program that allows text-based interaction between the user and the computer

Linux Commands

  • Linux command consists of program name followed by the option and arguments.
  • For ex.
cat -n myfile

# program name: cat
# option: -n
# argument: myfile
Enter fullscreen mode Exit fullscreen mode

Home directory

  • The home directory is a directory that stores user-specific data.
  • this data can be of any type
  • for ex. If there is a user named john, then john's data like images, documents, downloads, etc. data will reside in /home/john/ location

How to know the Home directory

echo $HOME
# This command will read from the environment variable $HOME

echo ~
# This command will expand the tilde symbol to the given user's home directory path
Enter fullscreen mode Exit fullscreen mode

Image description

Directory Management

How to create & remove directories

  • mkdir is a command which is used to create directory
  • rmdir is a command which is used to delete a empty directory
  • example
# to create a directory
mkdir test_directory

# to delete a directory
rmdir test_directory
Enter fullscreen mode Exit fullscreen mode

How to create a chain of directories

  • in order to perform this we use "-p" parameter with mkdir command
  • Example
# We want to create 4 directories each inside of it's parent directory
mkdir -p a/b/c/d
Enter fullscreen mode Exit fullscreen mode

How to navigate through directories

  • To switch directories we use cd command
# Changing to the parent directory
cd ../

# Changing to the home directory
cd /home/user1
Enter fullscreen mode Exit fullscreen mode

How to navigate folder directory in Linux

  • there are two types of path notation in Linux
  • 1. Relative Path
  • 2. Absolute Path

Relative path

  • relative path is defined with respect to the current directory
  • "." represents the current working folder
  • ".." represents the parent directory
  • For example
# to change to the parent directory for the current directory
cd ../

# to run a file/script present in the current directory
bash ./example.sh
Enter fullscreen mode Exit fullscreen mode

Absolute path

  • absolute paths are defined with respect to the root "/" location
  • for example
# navigating to the user1 document folder
cd /home/user1/documents
Enter fullscreen mode Exit fullscreen mode

PUSHD & POPD

  • pushd and popd commands are used to manage the directory stack.
  • Example
# we created 3 directories
mkdir -p a/b/c

# now we navigate to c directory using pushd command, by pushing it's path to directory stack
pushd a/b/c

# this command will take us to the given c directory and we can create a file here
echo "hello world" > test_file.txt

# to navigate to the previous path we can use popd command
# this will bring us to the parent directory
popd 
Enter fullscreen mode Exit fullscreen mode
. . . . . . .
Terabox Video Player