Unlocking Linux: Master the Power of Terminal Commands

What is Linux?

Linux is a free, open-source operating system that manages your computer’s hardware and software. It’s commonly used for servers, development, and personal computers due to its flexibility and security.


Why Learn Linux Terminal Commands?

The terminal is like a magic window where you can tell your computer what to do by typing commands. It helps you open files, run programs, and fix problems quickly.


Terminal Emulator

A terminal emulator is a program that lets us use the terminal on a graphical desktop environment, allowing us to run commands and interact with the computer using text.

Here are some examples of terminal emulators:

  • Command Prompt (cmd): The default terminal on Windows for running basic commands.

  • Git Bash: A terminal emulator that brings Linux-like commands to Windows, often used by developers.

  • WSL (Windows Subsystem for Linux): Allows users to run a Linux terminal directly on Windows, accessing a full Linux environment.


Introduction to Shells

A shell is a command-line interpreter that acts as an interface between users and the operating system. Common Linux shells include:

  • Bash (Bourne Again Shell): The most widely used shell, providing robust scripting capabilities and compatibility.

  • Zsh (Z Shell): An extended version of Bash, with advanced features like improved autocomplete and customization.

You can identify your current shell using:

echo $SHELL
  • echo: Prints the value of the specified variable.

  • $SHELL: Represents the current shell's path.


Basic Commands

1. Listing Files and Directories

ls
  • ls: Lists files and directories in the current directory.
ls -l
  • -l: Displays detailed information such as permissions, owner, group, size, and modification time.
ls -a
  • -a: Includes hidden files and directories (those starting with a .).

2. Creating Directories

mkdir directory_name
  • mkdir: Stands for "Make Directory" and creates a new directory with the specified name.

3. Changing Directories

cd directory_name
  • cd: Stands for "Change Directory" and moves to the specified directory.
cd ..
  • ..: Refers to the parent directory, moving up one level.
cd ~
  • ~: Represents the home directory of the current user.

File Management

1. Creating and Viewing Files

touch filename.txt
  • touch: Creates a new empty file or updates the timestamp of an existing file.
cat filename.txt
  • cat: Displays the contents of a file.
nano filename.txt
  • nano: Opens a simple text editor called Nano for editing the file.
vim filename.txt
  • vim: Opens the Vim editor, a more advanced terminal-based text editor.
echo "Hello, World!" > filename.txt
  • echo: Prints text to the terminal.

  • >: Redirects output to a file, overwriting its contents.

2. Copying Files

cp source.txt destination.txt
  • cp: Copies files from one location to another.
cp -r folder1 folder2
  • -r: Stands for "Recursive," copying directories and their contents.

3. Moving and Renaming Files

mv old_name.txt new_name.txt
  • mv: Moves or renames files and directories.
mv filename.txt directory_name/
  • Moves a file into a specified directory.

4. Deleting Files and Directories

rm filename.txt
  • rm: Removes files.
rm -r directory_name
  • -r: Recursively deletes directories and their contents.

File Permissions

Linux file permissions determine the actions users can perform on files or directories. Permissions are represented as Read (r), Write (w), and Execute (x). Each file or directory has three permission groups:

  • User (u): The owner of the file.

  • Group (g): Users within a specific group.

  • Others (o): All other users.

1. Viewing Permissions

ls -l filename.txt
  • ls -l: Displays file permissions in the format -rw-r--r--, where:

    • -: Indicates a regular file.

    • rwx: Represents permissions for the owner.

    • r--: Represents permissions for the group.

    • r--: Represents permissions for others.

2. Changing File Permissions

chmod 777 filename.txt
  • chmod: Changes file permissions.

  • 777: Grants read, write, and execute permissions to all users.

Understanding Numeric Permissions:

  • Read (r) = 4

  • Write (w) = 2

  • Execute (x) = 1

The sum of these values represents the permissions:

  • 7 = Read + Write + Execute

  • 5 = Read + Execute

  • 0 = No Permission

chmod u=rwx,g=rx,o=r filename.txt
  • u=rwx: Grants read, write, and execute permissions to the user.

  • g=rx: Grants read and execute permissions to the group.

  • o=r: Grants read-only permissions to others.

3. Changing Ownership

chown user:group filename.txt
  • chown: Changes the ownership of a file.

  • user: New owner of the file.

  • group: New group associated with the file.


Environment Variables

Environment variables are dynamic values used by the shell to influence its behavior. You can view environment variables using:

echo $PATH
  • $PATH: Displays a list of directories where executable files are located.

File Search using find and grep

1. Using find Command

The find command searches for files and directories based on name, type, size, or other criteria.

find /path/to/search -name "filename.txt"

Searches for a file named filename.txt in the specified path.

find . -type d

Finds all directories in the current directory.

2. Using grep Command

The grep command searches for patterns within files using text matching.

grep "keyword" filename.txt

Searches for the word "keyword" in the file filename.txt.

grep -r "error" /var/log/

Recursively searches for the word "error" in all files within /var/log/.

ps aux | grep nginx

Finds all running processes related to nginx.

Options:

  • -i: Perform a case-insensitive search.

  • -v: Invert match (show lines that don't match).

  • -n: Display line numbers with matching results.


Conclusion

Learning Linux terminal commands can greatly enhance your productivity and system control. Practice these commands to become proficient in navigating, managing files, monitoring resources, and troubleshooting. Happy learning!


Additional Resources to Practice Linux Commands

1
Subscribe to my newsletter

Read articles from Sourav Kumar Panda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sourav Kumar Panda
Sourav Kumar Panda