Basics of Linux Commands

Basics of Linux Commands

Introduction

Linux, an open-source operating system widely used in servers and desktops, provides users with powerful command-line tools for managing files and services. This blog summarizes key Linux commands that beginners must know to navigate the terminal, manage files, and execute essential system administration tasks. Understanding these commands not only enhances productivity but also deepens the user’s comprehension of the Linux environment.


Navigating and Managing Files

Command Basics

  • ls – List files and directories.

  • ls -al – List files with detailed information, including hidden files.

  • ls -lt – List files, sorted by time modification.

  • cd dir – Change to the directory dir.

  • cd – Change to the home directory.

  • pwd – Show the current working directory.

Directory Management

  • mkdir dir – Create a directory dir.

  • rmdir dir – Remove an empty directory dir.

Viewing Content

  • cat file – Display the entire content of a file.

  • more file – View file content one screen at a time.

  • less file – Similar to more, but with additional navigation options.

  • head file – Display the first 10 lines of a file.

  • tail file – Display the last 10 lines of a file.

  • tail -f file – View new content added to a file in real time.

Creating and Editing

  • touch file – Create or update a file.

  • cat >file – Create a file and enter content interactively.

  • nano file or vim file – Open the file in a text editor for editing.

Searching and File Manipulation

Searching Content

  • grep pattern file – Search for pattern in a file.

  • grep -r pattern dir – Search recursively for pattern in a directory.

  • command | grep pattern – Search for pattern in the output of a command.

  • locate file – Find all instances of a file.

  • find . -name filename – Search for files and directories named filename in the current directory.

File Management Commands

  • cp file1 file2 – Copy file1 to file2.

  • cp -r dir1 dir2 – Copy directory dir1 to dir2.

  • mv file1 file2 – Rename or move file1 to file2.

  • rm file – Delete a file.

  • rm -r dir – Delete a directory and its contents.

  • rm -f file – Force-delete a file.

  • rm -rf dir – Force-delete a directory and its contents.

  • ln -s file link – Create a symbolic link named link to file.

Using Wildcards

  • * – Matches any number of characters (e.g., .txt for all .txt files).

  • ? – Matches a single character.

Compression and Decompression

File Compression

  • tar cf file.tar file – Create a tar archive named file.tar containing file.

  • tar czf file.tar.gz files – Create a tar archive with Gzip compression.

  • tar cjf file.tar.bz2 files – Create a tar archive with Bzip2 compression.

  • gzip file – Compress a file, renaming it to file.gz.

Decompressing Files

  • tar xf file.tar – Extract files from an .tar archive.

  • tar xzf file.tar.gz – Extract an .tar.gz archive.

  • tar xjf file.tar.bz2 – Extract an .tar.bz2 archive.

  • gzip -d file.gz – Decompress a Gzip file.

Service Management and System Administration

Installing Services

  • apt-get install package or yum install package – Install a package (depending on the Linux distribution).

Service Control

  • systemctl start service – Start a service.

  • systemctl stop service – Stop a service.

  • systemctl status service – Check the status of a service.

  • systemctl restart service – Restart a service.

Monitoring System Resources

  • top – Display running processes and resource usage.

  • htop – Interactive version of top.

  • df – Show disk space usage.

  • du – Show directory space usage.

  • free – Display memory and swap usage.

Networking and Permissions

IP Configuration

  • ifconfig – Display network configuration.

  • ping host – Ping a host to check network connectivity.

  • wget file – Download a file from the internet.

  • wget -c file – Resume a stopped download.

File Permissions

  • chmod octal file – Change file permissions using octal notation.

    • 4 – Read (r)

    • 2 – Write (w)

    • 1 – Execute (x)

  • chown user file – Change the owner of a file.

  • chown user:group file – Change the owner and group of a file.

Process Management

Viewing Processes

  • ps – Display active processes.

  • top – View running processes with real-time updates.

Managing Processes

  • kill pid – Kill a process by its process ID (pid).

  • killall proc – Kill all processes with the name proc.

  • pkill pattern – Kill processes matching a pattern.

Job Control

  • bg – Resume a stopped job in the background.

  • fg – Bring a background job to the foreground.

  • fg n – Bring job n to the foreground.

System Information

Checking System Info

  • date – Show the current date and time.

  • cal – Show this month’s calendar.

  • uname -a – Display kernel information.

  • cat /proc/cpuinfo – Display CPU information.

  • cat /proc/meminfo – Show memory details.

  • df – Show disk space usage.

  • du – Display directory space usage.

  • man command – Open the manual page for a command.

Real-World Applications

User Management

  • adduser username – Add a new user.

  • deluser username – Delete an existing user.

CSV File Manipulation

  • cut – Extract specific fields from a file.

  • awk – Process and analyze text, especially useful for CSV files.

Summary of Common Options

OptionDescription
-nameSearch by file name (case-sensitive).
-inameSearch by file name (case-insensitive).
-type dSearch for directories.
-type fSearch for files.
-sizeSearch by file size.
-mtimeSearch by modification time.
-execExecute a command on the matching files.
-permSearch by file permissions.

Conclusion

The Linux command line is a powerful tool that, when mastered, can significantly enhance a user’s ability to manage files, services, and system resources. By familiarizing themselves with essential commands, beginners can efficiently navigate the Linux environment and perform system administration tasks with confidence. Understanding the importance of file permissions, service management, and network configurations is crucial for anyone seeking to work effectively in a Linux-based system. This blog equips readers with foundational knowledge that paves the way for further exploration of Linux capabilities.


Alright, folks, that’s a wrap on the most basic and commonly used Linux commands! These essentials are your stepping stones to mastering Linux for DevOps. I get it—reading commands in theory might feel a bit dry, like munching on plain crackers.

But here’s the exciting part: in the next blog, we’re taking these commands off the page and into the terminal! We’ll roll up our sleeves, get hands-on, and see these commands in action. So, get ready for some real Linux magic. Until then, keep exploring and stay curious—because the best is yet to come!

Until next time, keep coding, automating, and advancing in DevOps! 😁

Peace out ✌️