Working With Shell

nerdflash28

Script Koder

Posted on September 12, 2024

Working With Shell

What is a shell?

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

Types of shell in Linux

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 know your current path

  • To know the current path, we use the command pwd, which stands for Present working directory
pwd 
Enter fullscreen mode Exit fullscreen mode

How to create & remove directories

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

# to create multiple directories
mkdir test1 test2 test3

# to delete a directory
rmdir test_directory

# to remove multiple directories
rmdir test1 test2 test3
Enter fullscreen mode Exit fullscreen mode

How to list the content of a folder

  • to list the content of a directory we use the ls command
# to list the content of a directory
ls 
Enter fullscreen mode Exit fullscreen mode

How to create a chain of directories

  • in order to perform this we use the "-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
💖 💪 🙅 🚩
nerdflash28
Script Koder

Posted on September 12, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Working With Shell
bash Working With Shell

September 12, 2024