Linux - Command Line

Linux - Command Line

The Linux command line is a powerful tool for interacting with the operating system, automating tasks, and performing a wide range of system operations. Mastering it can make you a more efficient and capable Linux user or system administrator. In this blog, we’ll dive into the essential commands and concepts to help you master the Linux command line.

1. Basic Navigation Commands

Let’s start by learning how to navigate the Linux filesystem.

pwd – Print Working Directory

Displays the current directory you're in.

pwd

ls – List Files

Lists the contents of a directory.

ls      # List files in the current directory
ls -l   # Detailed list (including permissions and file sizes)
ls -a   # List hidden files

cd – Change Directory

Moves you to another directory.

cd /path/to/directory  # Change to a specific directory
cd ~                   # Change to home directory
cd ..                  # Move up one directory

2. File Management

Managing files and directories is fundamental to working in the Linux command line.

touch – Create an Empty File

Creates a new, empty file.

touch filename.txt

cp – Copy Files and Directories

Copies files or directories from one location to another.

cp file1.txt file2.txt           # Copy a file
cp -r /source/directory /target  # Copy a directory recursively

mv – Move or Rename Files

Moves or renames files and directories.

mv file.txt /new/path/           # Move file to another directory
mv oldname.txt newname.txt       # Rename a file

rm – Remove Files and Directories

Deletes files or directories.

rm file.txt             # Delete a file
rm -r /path/to/dir      # Delete a directory and its contents recursively

mkdir – Create Directory

Creates a new directory.

mkdir new_directory             # Create new directory
mkdir -p parent_dir/child_dir   # Create parent and child directories at once

3. File Permissions & Ownership

Understanding and managing file permissions and ownership is key to maintaining security and proper access control on your Linux system.

Viewing Permissions

  • Using ls -l:
ls -l filename

This command displays file permissions, ownership, and other details. Permissions are shown as a string like -rwxr-xr--, where:

  • r stands for read

  • w stands for write

  • x stands for execute

Changing Permissions

  • Using chmod Numeric Mode :

  • 7 (rwx): Read, write, and execute

  • 5 (r-x): Read and execute

  • 0 (---): No permissions

chmod 755 file

Using chmodSymbolic Mode:

  • Use the following notations with the chmod command:

  • u: User (the file owner)

  • g: Group (the group that owns the file)

  • o: Others (everyone else)

chmod u+x file  # Add execute permission for the other user

Changing Ownership

  • Using chown:
chown user:group file

The chown command changes the owner and group of a file or directory. For example, chown ubuntu:dev file changes the owner to ubuntu and the group to dev.

  • Changing Group Ownership:
chgrp group file

  • The chgrp command changes the group ownership of a file or directory.

4. User and Group Management

Linux allows the creation of multiple users, each with specific roles and permissions.

  • adduser: Create a new user.
sudo adduser username

  • passwd: Set or change a user’s password.

  • su: Switch user.

Managing Groups:

  • groupadd: Creates a new group (e.g., groupadd groupname).

  • usermod: Modifies user properties, including group membership (e.g., usermod -aG groupname username).

Viewing Users and Groups:

  • cat /etc/passwd: Lists all users.

  • cat/etc/group: Lists all groups.

5. Process Management

Managing processes is essential for system stability and performance. Some useful process management commands are:

  • ps: (process status) command displays information about active processes

  • top: Provides a dynamic, real-time view of system processes.

  • htop: An improved version of top with a better interface.

top and htop: Both tools display real-time CPU usage.

mpstat: Provides detailed CPU usage statistics.

  • kill: Terminates a process by its ID (PID).
kill -9 PID

df: Shows disk space usage for mounted file systems in a human-readable format.

df -h

6. Network Management

Linux provides powerful tools to interact with networks, making it a go-to platform for system administration.

ifconfig – Network Interface Configuration

Displays the network interface configuration (useful for checking IP addresses).

ifconfig

ping – Test Network Connectivity

Sends packets to a network address to test connectivity.

ping google.com

netstat – Network Statistics

Shows network connections, routing tables, and interface statistics.

netstat -tuln   # Show listening ports

7. Package Management

Package management systems like apt (Debian/Ubuntu) or yum (RHEL/CentOS) allow for easy installation, updating, and removal of software packages.

apt-get – Manage Packages in Ubuntu/Debian

Installs, updates, or removes software packages.

sudo apt-get update      # Update package list
sudo apt-get upgrade     # Upgrade all installed packages
sudo apt-get install package_name   # Install a package
sudo apt-get remove package_name    # Remove a package

yum – Manage Packages in CentOS/RHEL

Similar to apt-get but used in Red Hat-based distributions.

sudo yum install package_name

8. Text Processing

Linux offers several tools for processing text files, making it easy to manipulate, search, and transform data.

cat – Display File Contents

Displays the content of a file.

cat file.txt

grep – Search Text

Searches for patterns in a file or output.

grep 'pattern' file.txt    # Search for a pattern in a file
grep -r 'pattern' /dir     # Search recursively in a directory

9. Text Editors

Text editors allow you to view or edit files from the terminal. Popular choices include:

Nano: Simple and user-friendly.

  • Opening a File: To open or create a file with nano, use the following command:
nano filename

  • Editing Text: Simply type to insert text. Use arrow keys to navigate the file.

  • Saving Changes: Press Ctrl + O (write out), then press Enter to save the file.

  • Exiting nano: Press Ctrl + X. If you have unsaved changes, you will be prompted to save them.

  • Cutting and Pasting Text:

    • Cut: Press Ctrl + K to cut a line.

    • Paste: Press Ctrl + U to paste the cut text.

Vi: More advanced, with powerful features for text manipulation.

  • Opening a File: To open a file with vi, use:
vi filename

  • Modes in vi:

    • Normal Mode: Default mode for navigation and commands. Press Esc to enter normal mode.

    • Insert Mode: Used for editing text. Enter by pressing i.

    • Command Mode: Used for executing commands. Enter by pressing :.

  • Editing Text:

    • Enter Insert Mode: Press i (insert before the cursor) or a (append after the cursor).

    • Exit Insert Mode: Press Esc.

  • Saving Changes:

    • Save and Exit: Type :wq and press Enter.

    • Save Without Exiting: Type :w and press Enter.

    • Exit Without Saving: Type :q! and press Enter.

  • Cutting and Pasting Text:

    • Cut Line: In normal mode, press dd.

    • Paste Line: Press p after cutting.

10. Automation with Shell Scripting

Shell scripting allows you to automate repetitive tasks by writing a series of commands in a script file.

Basic Shell Script Example:

Create a script:

nano script.sh

Write your script:

#!/bin/bash
echo "Hello, Linux World!"

Make it executable:

chmod +x script.sh

Run the script:

./script.sh

Conclusion

Mastering the Linux command line is a crucial skill for system administrators, developers, and DevOps professionals. With the commands covered here, you’re well on your way to becoming proficient with the Linux terminal. Once you get comfortable, you can start automating tasks, processing large amounts of data, and managing systems with ease.