Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Working With Shell
Linux_guy
Linux_guy

Posted on • Edited on

     

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 modeExit 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 $HOMEecho ~# This command will expand the tilde symbol to the given user's home directory path
Enter fullscreen modeExit 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 modeExit 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 directorymkdirtest_directory# to create multiple directoriesmkdirtest1 test2 test3# to delete a directoryrmdirtest_directory# to remove multiple directoriesrmdirtest1 test2 test3
Enter fullscreen modeExit 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 directoryls
Enter fullscreen modeExit 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 directorymkdir-p a/b/c/d
Enter fullscreen modeExit fullscreen mode

How to navigate through directories

  • To switch directories we use cd command
# Changing to the parent directorycd ../# Changing to the home directorycd /home/user1
Enter fullscreen modeExit 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 directorycd ../# to run a file/script present in the current directorybash ./example.sh
Enter fullscreen modeExit fullscreen mode

Absolute path

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

PUSHD & POPD

  • pushd and popd commands are used to manage the directory stack.
  • Example
# we created 3 directoriesmkdir-p a/b/c# now we navigate to c directory using pushd command, by pushing it's path to directory stackpushda/b/c# this command will take us to the given c directory and we can create a file hereecho"hello world"> test_file.txt# to navigate to the previous path we can use popd command# this will bring us to the parent directorypopd
Enter fullscreen modeExit fullscreen mode

Top comments(13)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jimmymcbride profile image
Jimmy McBride
Full Stack Web 🕸️ Android Dev 🤖 Linux Wizard 🧙‍♂️ Crafting cutting-edge apps 🚀 Kotlin & Jetpack enthusiast ✨ Forever learning & evolving 🌱
  • Email
  • Location
    Kyle, Texas
  • Education
    Lambda School
  • Work
    Senior Android Developer
  • Joined

Would love to see something about pushd and popd in addition to cd. They can be very handy tools when you changing directories a lot and need to make a quick escape to where you've previously been! :)

Can't wait to see more!

CollapseExpand
 
nerdflash28 profile image
Linux_guy
@ HPC Cloud Engineer
  • Location
    New Delhi, India
  • Pronouns
    linux/coder
  • Work
    IT Professional
  • Joined

Thanks for your response, i'll keep updating this page.

CollapseExpand
 
kubernetic profile image
BC
Linux, Bash, and getting my feet wet in Kubernetes. Hoping to work on GoLang very soon!
  • Joined

I think an important concept is also where a user's shell is defined.

A user's shell is typically setup when the user is added to the system. Often /bin/bash is set as a default, but macOS now sets /bin/zsh as the default shell for users.

A user could find their default shell by viewing the/etc/passwd file. For example,

root:x:0:0:root:/root:/bin/bashgames:x:12:100:games:/usr/games:/sbin/nologin
Enter fullscreen modeExit fullscreen mode

In the above example root's default shell is set to the Bash shell. While the games user actually has been set to a nologin shell, preventing anyone from logging into the user. This has certain uses for non-interactive user sessions. We have other shell options that we can use as well. There are lots of varieties and it depends on your OS, or what the administrator has set at account creation.

There are different commands to modify a user's shell. But one way you can do this is with the usermod command:

usermod -s /bin/bash USER

If you want to find a list of available shells on your system, try this command:
cat /etc/shells

CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

There's no such thing as a Linux shell. There are only various shells, e.g., bash, tcsh, zsh, etc. You can use exactly the same shells on FreeBSD, macOS, and others as you can on Linux.

CollapseExpand
 
nerdflash28 profile image
Linux_guy
@ HPC Cloud Engineer
  • Location
    New Delhi, India
  • Pronouns
    linux/coder
  • Work
    IT Professional
  • Joined
• Edited on• Edited

@pauljlucas Yeah, you are correct mate, thanks for the help, i've updated the title accordingly.

CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

You might want to reconsider the banner image and#linux tag as well.

CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

You still mention "Linux shell....". You ought to replace that with "A shell...."

 
nerdflash28 profile image
Linux_guy
@ HPC Cloud Engineer
  • Location
    New Delhi, India
  • Pronouns
    linux/coder
  • Work
    IT Professional
  • Joined

sure, i'm working on it

CollapseExpand
 
manchicken profile image
Mike Stemle
I have been a software professional since I was in high school in 1998. I'm enthusiastic about open source, and I really enjoy working in unusual software systems or within strange constraints.
  • Location
    Yardley, PA
  • Education
    N/A
  • Pronouns
    he/him
  • Work
    Principal Architect at Arc XP
  • Joined

I’m glad to see folks still talking about the terminal. That said, none of this is bash-specific. These will all work in a variety of shells, and I hope folks find whatever shell they prefer and have a great time in the shell.

CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

You mention commands, but never once mention thePATH environment variable or explain that commands likemkdir "live" in certain directories and the way commands work is that the shell searchesPATH for a matching command.

CollapseExpand
 
kmsaifullah profile image
Khaled Md Saifullah
MERN Stack DeveloperMongoDB | Express.js | React.js | Node.js

In my opinion "how bash works" is also an important topic for a beginners, Don't you think so?

CollapseExpand
 
nerdflash28 profile image
Linux_guy
@ HPC Cloud Engineer
  • Location
    New Delhi, India
  • Pronouns
    linux/coder
  • Work
    IT Professional
  • Joined

Beginners with no experience with bash, see the black screen and don't know what to write on it.

From my Teaching experience, they are more interested in, How to use it.
not "How it works".

I hope you understood my point, sir.

Thanks for your Feedback though

Some comments may only be visible to logged-in visitors.Sign in to view all comments.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

@ HPC Cloud Engineer
  • Location
    New Delhi, India
  • Pronouns
    linux/coder
  • Work
    IT Professional
  • Joined

More fromLinux_guy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp