Page Content

Tutorials

How To Use Shell Command In Linux & Basic Shell Commands

How to use shell command in linux

You can use text instead of mouse clicks to operate your computer by using the shell, sometimes referred to as the command line or terminal. Developers and system administrators use this potent ability to automate processes and effectively manage systems.

How To Use Shell Command In Linux
How To Use Shell Command In Linux

Opening the Terminal

The Terminal is the application (the window), while the Shell is the program inside that window that processes your commands.

  • Windows: Use PowerShell or Command Prompt (search in the Start menu). For a Linux-like experience, many developers use WSL (Windows Subsystem for Linux).
  • macOS: Press Cmd + Space and type “Terminal.”
  • Linux: Press Ctrl + Alt + T.

Also Read About Explain Different Types Of Linux Shells In Operating System

Understanding the Shell Prompt

When you open the terminal, you’ll see a line of text ending in a symbol like $ or %. This is the prompt. It tells you the shell is ready for input.

A typical prompt looks like this: username@hostname:~$

  • username: Who you are currently logged in as.
  • hostname: The name of the computer.
  • ~ (Tilde): Represents your Home Directory (your personal folder).
  • $: Indicates you are a standard user. A # usually means you have “root” (administrative) privileges.

Command Syntax Structure

It follows a specific “grammar” to ensure the computer understands exactly what to do.

  • Command: The action (the verb). Example: ls (list).
  • Options (Flags): Modify how the command works. They usually start with a dash. Example: -l (long format).
  • Arguments: The target of the command (the noun). Example: Documents (the folder name).

Running Basic Commands

Here are the “Big Four” commands you’ll use every day:

CommandPurposeExample
pwdPrint Working Directory (Where am I?)pwd
lsList files and folders in the current spot.ls -a (shows hidden files)
cdChange Directory (Move to a folder).cd Desktop
clearCleans the screen of old text.clear

Also Read About Role Of Shell In Linux And Kernel vs Shell vs Terminal

Built-in vs External Commands

Not all commands are created equal. The shell distinguishes between those that are “part of the shell” and those that are separate “mini-apps” on your hard drive.

Built-in Commands

These are part of the shell program itself (like bash or zsh). Because they are “built-in,” they execute almost instantly.

  • Examples: cd, alias, exit, pwd.

External Commands

These are independent programs located in folders like /bin or /usr/bin. When you run them, the shell has to go find the file and start it up.

  • Examples: ls, mkdir, git, python.

You can find out which one a command is by typing type followed by the command name (e.g., type cd or type ls).

Basic Shell Commands

Your interaction with a computer changes when you become efficient with the shell; you go from being a passive user to an accurate operator. A GUI may be more comfortable, but the command line provides deep system access, speed, and automation.

File and Directory Management

Navigating the file system and organizing data are the most frequent tasks in the shell.

  • mkdir [name]: Creates a new directory (folder).
  • touch [file]: Creates a new, empty file.
  • cp [source] [destination]: Copies a file or folder. Use cp -r to copy a folder and everything inside it.
  • mv [source] [destination]: Moves or renames a file or folder.
  • rm [file]: Deletes a file. Warning: Unlike the Recycle Bin, shell deletion is usually permanent. Use rm -r for directories.

Viewing File Contents

You don’t need to open a heavy text editor just to read a file. The shell provides several lightweight ways to peek inside.

  • cat [file]: Prints the entire content of a file to your terminal. Best for short files.
  • less [file]: Opens a file in a scrollable interface. Press q to exit.
  • head -n [number] [file]: Shows the first few lines of a file (default is 10).
  • tail -n [number] [file]: Shows the last few lines. This is excellent for checking the latest entries in log files.

Also Read About What Is Linux Kernel? Why It Is Important And Its Components

Understanding File Permissions

In Linux and macOS, every file has “ownership” and “permissions” that determine who can read, write, or execute it. When you run ls -l, you see a string like -rwxr-xr--.

  • r (Read): Permission to view the file.
  • w (Write): Permission to edit or delete the file.
  • x (Execute): Permission to run the file as a program.
  • chmod: Used to change these permissions. For example, chmod +x script.sh makes a script executable.

Searching and Filtering

The shell excels at finding a “needle in a haystack” using search and filter commands.

  • grep "[pattern]" [file]: Searches for a specific string of text within a file.
  • find [path] -name "[filename]": Searches for files and directories based on their names.
  • | (The Pipe): This isn’t a command, but a tool to connect them. It takes the output of one command and sends it to the next.
    • Example: ls | grep "test" lists all files but only shows those with “test” in the name.

System Information Commands

Need to know how much RAM you have left or what’s slowing down your computer? These commands provide a “dashboard” view of your hardware.

CommandFunction
df -hShows available disk space in human-readable format (GB/MB).
free -mDisplays used and available memory (RAM).
topShows a real-time list of running processes and CPU usage.
uname -aProvides detailed information about the system kernel and OS.
whoamiConfirms which user account is currently active.

Next Steps

To get comfortable, try a small “mission”: Create a folder named Practice, create a text file inside it using touch, write a sentence into it using a text editor (like nano), and then use grep to find a word in that file.

Also Read About What Is A Linux Shell? And Different Types Of Shell In Linux

Hemavathi
Hemavathihttps://govindhtech.com/
Myself Hemavathi graduated in 2018, working as Content writer at Govindtech Solutions. Passionate at Tech News & latest technologies. Desire to improve skills in Tech writing.
Index