Basic Linux Commands

0 33 minutes read
| Published on: June 23, 2023 | Last updated on: July 23, 2023

Introduction

In the realm of operating systems, Linux holds a place of honor. It’s a symbol of versatility, power, and freedom in the world of software, with its open-source roots giving users a level of control and customization few other operating systems can match. At the heart of Linux’s prowess is its command-line interface, a powerful tool that provides unparalleled control over the system.

Whether you’re a developer, a system administrator, or a tech enthusiast, mastering Linux commands is not just a skill – it’s a rite of passage. These commands allow you to interact with your system, navigate through directories, manipulate files, monitor system resources, and even control other machines remotely. And that’s just scratching the surface. This blog aims to shed light on the essential Linux commands, their purpose, features, and how to use them effectively.

The command-line interface might seem daunting initially, especially if you’re used to graphical user interfaces. However, understanding the purpose of Linux commands (our first keyphrase) will demystify the Linux command line and reveal it as an indispensable tool, a true friend of every Linux user. You’ll discover that every command, from ‘ls’ to ‘sudo’, serves a specific purpose designed to make tasks easier, faster, and more efficient.

But understanding the purpose is just the first step. You’ll also explore the unique features of these commands, learning how options and arguments can change command behavior, and how commands can be combined for greater efficiency and automation. And, of course, we will guide you through the usage of these commands, with step-by-step examples and tips to help you navigate the Linux terminal with ease.

The Power of Linux Commands

In the dynamic realm of operating systems, Linux has carved a niche for itself as a paragon of flexibility, resilience, and power. A quintessential component of its power lies in the command-line interface, which offers a level of interaction that’s far beyond what any graphical user interface (GUI) can provide. It’s through this command-line interface that users can unlock the true potential of the Linux operating system.

Moving from the comfortable, graphical confines of operating systems like Windows or macOS to the text-based Linux command-line can initially seem challenging. However, real-world examples underscore the efficiency that comes with this shift. For instance, consider a simple task like finding all the PDF files in a directory and its subdirectories. A GUI would require you to manually click through each folder and subfolder. With a Linux command, such as find /path/to/directory -name "*.pdf", this task becomes a matter of seconds, regardless of how many nested folders you have.

Linux commands offer an unparalleled level of direct interaction with the system’s functions. A system administrator, for instance, can monitor real-time system processes using a command like top, which provides an ongoing look at processor activity in real time. A developer can streamline their workflow by using grep to search for a specific string in a codebase, or cron to schedule scripts to run at specific times. This direct control is a feature unique to command-line interfaces and is part of why learning Linux commands is indispensable for professionals in tech fields.

Think about the tech giants like Google, Amazon, and Facebook, who manage vast amounts of data across servers. These servers predominantly run on Linux, and understanding Linux commands is crucial for maintaining and troubleshooting these systems. For example, the rsync command is often used to synchronize files across multiple servers, while ssh allows administrators to remotely access and control these servers.

Thus, the command-line interface is not just another aspect of Linux; it’s a fundamental part of interacting with the system. Grasping the purpose and potential of Linux commands is not an option, but a necessity for those aiming to excel in Linux environments. So, let’s unravel the power and potential of these commands in the following sections.

Commonly Used Linux Commands and Their Purposes

Transitioning from our discussion on the overarching importance of Linux commands, we shift our focus to delve into specific, fundamental commands. Understanding these Linux commands and their usages paves the way towards effective navigation and interaction with the Linux environment. Let’s walk through 15 of the most useful beginner commands and their typical use-cases:

File and Directory Operations:

  1. ls: List directory contents
    • Example: ls (Lists all non-hidden files and directories in the current directory)
    • Example: ls -l (Lists files and directories in the current directory in long format, including additional information like permissions, owner, group, size, and modification time)
    • Example: ls -a (Lists all files and directories, including hidden ones)
  2. cd: Change directory
    • Example: cd /home/user/Documents (Changes the current directory to /home/user/Documents)
    • Example: cd .. (Navigates one directory up)
    • Example: cd (With no arguments, cd returns you to your home directory)
  3. pwd: Print working directory
    • Example: pwd (Displays the path of the current directory)
  4. touch: Create a new empty file
    • Example: touch myfile.txt (Creates a new empty file named myfile.txt in the current directory)
  5. cp: Copy files or directories
    • Example: cp source.txt destination.txt (Copies source.txt to destination.txt)
    • Example: cp -r sourcedir/ destinationdir/ (Copies a directory and all its contents to another directory)
  6. mv: Move or rename files or directories
    • Example: mv oldname.txt newname.txt (Renames a file from oldname.txt to newname.txt)
    • Example: mv myfile.txt /home/user/Documents/ (Moves myfile.txt to /home/user/Documents)
  7. rm: Remove files or directories
    • Example: rm myfile.txt (Deletes myfile.txt)
    • Example: rm -r mydir (Deletes a directory and its contents)
  8. find: Search for files or directories
    • Example: find /home/user/ -name '*.txt' (Searches the /home/user directory and all its subdirectories for files ending in .txt)
  9. mkdir: Create new directory
    • Example: mkdir newdir (Creates a new directory named newdir in the current directory)
  10. rmdir: Remove directory
    • Example: rmdir mydir (Deletes the directory named mydir. Note that this command only works on empty directories)

Folder/File Viewing and Editing:

  1. cat: Concatenate and display file content
    • Example: cat file.txt (Displays the content of file.txt)
  2. less: View file content page by page
    • Example: less file.txt (Opens file.txt for viewing in the terminal, one page at a time)
  3. more: Another tool to view file content page by page
    • Example: more file.txt (Opens file.txt for viewing in the terminal, one page at a time)
  4. head: View the start of a file
    • Example: head file.txt (Displays the first ten lines of file.txt)
    • Example: head -n 20 file.txt (Displays the first twenty lines of file.txt)
  5. tail: View the end of a file
    • Example: tail file.txt (Displays the last ten lines of file.txt)
    • Example: tail -n 20 file.txt (Displays the last twenty lines of file.txt)
  6. nano: Simple text editor
    • Example: nano file.txt (Opens file.txt in the Nano text editor for editing)
  7. vi: Advanced text editor
    • Example: vi file.txt (Opens file.txt in the Vi text editor for editing)

File Permissions and Ownership:

  1. chmod: Change file permissions
    • Example: chmod 755 script.sh (Sets the permissions of script.sh to 755, allowing the owner to read, write, and execute the file, and others to read and execute it)
    • Alternative: setfacl is an alternative tool that provides more fine-grained control over file permissions, allowing you to set permissions for specific users or groups.
    • Feature: chmod is a versatile command that enables you to modify file permissions to control read, write, and execute access for owners, groups, and others.
  2. chown: Change file ownership
    • Example: chown newuser file.txt (Changes the ownership of file.txt to newuser)
    • Example: chown newuser:newgroup file.txt (Changes both the ownership and group of file.txt)
    • Alternative: chgrp is a command used specifically to change group ownership of files and directories.
    • Feature: chown allows you to change the ownership of files or directories, providing control over access rights and privileges.
  3. chgrp: Change group ownership
    • Example: chgrp newgroup file.txt (Changes the group ownership of file.txt to newgroup)
    • Feature: chgrp is used to change the group ownership of files or directories, allowing you to manage file access permissions based on groups.

Process and Job Control:

  1. top: Display system summary and top processes
    • Example: top (Displays a dynamic, real-time view of the running system, including system summary information and a list of processes currently being managed by the kernel)
    • Alternative: htop provides an interactive and more user-friendly process viewer with features like sorting, filtering, and detailed system information.
    • Feature: top allows you to monitor system performance, view resource usage, and manage processes.
  2. ps: Report a snapshot of current processes
    • Example: ps (Displays a snapshot of the current processes)
    • Example: ps aux (Displays detailed information about all the current processes)
    • Alternative: pstree displays running processes in a tree-like structure, making it easier to visualize parent-child relationships.
    • Feature: ps provides information about active processes, including their IDs, resource usage, and relationships with other processes.
  3. kill: Terminate a process
    • Example: kill 1234 (Sends a signal to the process with PID 1234, usually resulting in that process terminating)
    • Example: killall process_name (Terminates all processes with the specified name)
    • Feature: kill allows you to send signals to processes, enabling you to terminate or control their behavior.
  4. bg: Send jobs to the background
    • Example: If you’re running a program like nano file.txt and you press Ctrl+Z, the program will suspend. Typing bg will make it continue running in the background.
    • Feature: bg allows you to move suspended or stopped jobs to the background, freeing up the terminal for other tasks.
  5. fg: Bring jobs to the foreground
    • Example: If you have a job running in the background (after using bg), you can type fg to bring it back to the foreground.
    • Feature: fg enables you to bring background jobs back to the foreground for interaction and control.

Package and System Management

  1. sudo: Execute a command as another user (usually the superuser)
    • Example: sudo apt-get update (Runs the apt-get update command with superuser permissions)
    • Feature: Allows regular users to run commands that require superuser permissions in a controlled manner.
    • Alternative: su (switch user), though it’s typically used less because it requires the root password and gives full root permissions, while sudo allows for more fine-grained control.
  2. apt-get: APT package handling utility (for Debian-based systems like Ubuntu)
    • Example: sudo apt-get install git (Installs the git package)
    • Example: sudo apt-get update (Updates the list of available packages and their versions, but it does not install or upgrade any packages)
    • Example: sudo apt-get upgrade (Upgrades all upgradable packages)
    • Feature: Easily manage packages on your system, allowing for simple installations, upgrades, and removals.
    • Alternative: apt is a newer tool that includes the most commonly used commands from apt-get and apt-cache with more user-friendly syntax.
  3. yum: Package manager (for Red Hat-based systems like CentOS)
    • Example: sudo yum install git (Installs the git package)
    • Example: sudo yum update (Updates all packages on the system)
    • Feature: Similar to apt-get, it allows easy management of packages but is used primarily on Red Hat-based systems.
    • Alternative: dnf is the next-generation version of yum used in newer Red Hat-based distributions like Fedora.
  4. shutdown: Shutdown or restart the system
    • Example: sudo shutdown -h now (Shuts down the system immediately)
    • Example: sudo shutdown -r 5 (Restarts the system after 5 minutes)
    • Feature: Provides a way to safely shut down or reboot your system.
    • Alternative: reboot for rebooting the system, poweroff to power it off.
  5. reboot: Reboot the system
    • Example: sudo reboot (Reboots the system immediately)
    • Feature: A quick way to restart your system.
    • Alternative: shutdown -r now

Networking

  1. ping: Send ICMP ECHO_REQUEST packets to network hosts
    • Example: ping google.com (Sends ICMP packets to google.com and reports the response time)
    • Feature: ping is commonly used to check network connectivity and measure the round-trip time (RTT) between the local machine and a remote host.
  2. ifconfig: Display or configure a network interface
    • Example: ifconfig (Displays information about all active network interfaces)
    • Example: ifconfig eth0 (Displays information about the network interface named eth0)
    • Alternative: ip command is a more modern and versatile alternative to ifconfig. It provides similar functionality with additional features and support for newer networking technologies.
    • Feature: ifconfig allows you to view and configure network interface settings, including IP addresses, netmasks, and other network parameters.
  3. netstat: Network statistics
    • Example: netstat (Shows network connections, routing tables, interface statistics, masquerade connections, and multicast memberships)
    • Alternative: ss command is a newer alternative to netstat that provides similar functionality but with better performance and more comprehensive output.
    • Feature: netstat provides information about network connections, network interfaces, routing tables, and various network statistics.
  4. ssh: Securely log into a remote machine
    • Example: ssh username@hostname (Logs into the machine hostname with the specified username)
    • Feature: ssh allows secure remote access to other machines over a network. It provides encrypted communication and secure authentication.
  5. scp: Securely copy files between hosts on a network
    • Example: scp sourcefile username@hostname:/path/to/destination (Copies sourcefile to the specified destination on the remote hostname)
    • Feature: scp enables secure file transfer between hosts on a network, utilizing SSH for encryption and authentication.
  6. wget: Retrieve files from the web
    • Example: wget http://website.com/file.txt (Downloads file.txt from the specified URL)
    • Feature: wget is a versatile command-line tool for downloading files from the web, supporting HTTP, HTTPS, and FTP protocols.

Disk Usage

  1. df: Report file system disk space usage
    • Example: df -h (Displays disk usage in human-readable format)
    • Feature: Provides information about the amount of disk space used and available on file systems.
    • Alternative: du command can be used to estimate file and directory space usage.
  2. du: Estimate file and directory space usage
    • Example: du -sh /path/to/directory (Displays the total size of the specified directory in human-readable format)
    • Feature: Allows you to determine the disk space occupied by files and directories.
    • Alternative: ncdu is a more interactive alternative that provides a detailed, navigable summary of disk usage.

Searching and Sorting

  1. grep: Print lines matching a pattern in a file
    • Example: grep "error" logfile.txt (Searches for the word “error” in logfile.txt and displays matching lines)
    • Alternative: ripgrep (rg) is an alternative tool to grep that offers faster searching with a similar syntax. It is optimized for speed and supports features like searching specific file types, ignoring files, and more.
    • Feature: grep is a versatile command-line tool that supports regular expressions and allows for complex pattern matching. It is widely used for searching and filtering text files.
  2. sort: Sort lines in text files
    • Example: sort names.txt (Sorts the lines in names.txt in ascending order)
    • Alternative: natsort is a natural sorting tool that sorts alphanumeric strings in a human-friendly way, producing more intuitive results compared to the default lexicographic sorting of sort.
    • Feature: sort provides extensive sorting capabilities, including numerical sorting, case-sensitive sorting, and the ability to sort based on specific keys or fields.
  3. uniq: Report or omit repeated lines
    • Example: uniq data.txt (Displays unique lines from data.txt, omitting duplicates)
    • Alternative: awk '!seen[$0]++' data.txt is an alternative way to achieve similar functionality using the Awk command. It eliminates duplicate lines and retains the original order.
    • Feature: uniq is a handy tool for identifying and eliminating duplicate lines from sorted or unsorted files. It also offers options to count the occurrences of each line and display only duplicates or unique lines.

Compression and Archives

  1. gzip: Compress or expand files
    • Example: gzip file.txt (Compresses file.txt and creates file.txt.gz)
    • Example: gzip -d file.txt.gz (Decompresses file.txt.gz and creates file.txt)
    • Alternative: pigz is a parallel implementation of gzip that utilizes multiple processor cores for faster compression and decompression.
    • Feature: gzip provides fast and efficient compression and decompression of files, commonly used for creating compressed archives or reducing file sizes.
  2. gunzip: Decompress files compressed by gzip
    • Example: gunzip file.txt.gz (Decompresses file.txt.gz and creates file.txt)
    • Feature: gunzip is a simple tool specifically designed to decompress files compressed using gzip.
  3. tar: Create, add to, or extract from an archive file
    • Example: tar -cvf archive.tar file1 file2 (Creates a tar archive named archive.tar containing file1 and file2)
    • Example: tar -xvf archive.tar (Extracts the contents of archive.tar)
    • Alternative: pax is a versatile archiving tool that supports multiple formats and provides more advanced features like extended file attributes and access control lists.
    • Feature: tar is commonly used for creating and manipulating archive files. It can compress files using other tools like gzip or bzip2 by combining commands, such as tar -cvzf to create a compressed tar archive.
  4. zip: Create, modify, or extract ZIP archives
    • Example: zip archive.zip file1 file2 (Creates a ZIP archive named archive.zip containing file1 and file2)
    • Example: unzip archive.zip (Extracts the contents of archive.zip)
    • Feature: zip is a widely used compression tool that supports various compression levels, password protection, and metadata preservation. It is compatible across different operating systems.

Help and Documentation

  1. man: Display the online manual of a command
    • Example: man grep (Displays the manual page for the grep command)
    • Feature: man provides detailed documentation, explanations, and usage examples for various commands and system functions.
  2. info: Display command’s info entry
    • Example: info tar (Displays the info entry for the tar command)
    • Feature: info is an alternative documentation system that provides more extensive and hypertextual documentation for commands and topics.
  3. whatis: Display one-line manual page descriptions
    • Example: whatis ls (Displays a brief description of the ls command)
    • Feature: whatis provides concise descriptions of commands, making it useful for quickly getting an overview of a command’s purpose.

Features of Linux Commands

  1. Piping (|): Piping is a powerful feature that allows the output of one command to be used as the input of another. This feature significantly enhances the interactivity of different commands. For instance, ls -l | grep ".txt" lists detailed information about only the .txt files in a directory, demonstrating a combination of ls and grep commands using a pipe.
  2. Redirection (>, >>, <): Redirection controls where the output of a command is sent. It can be directed to files, devices, or even to other commands. For example, ls -l > filelist.txt saves the long list of directory contents to the file filelist.txt. The >> appends the output to an existing file, while < takes input from a file.
  3. Command Chaining (;, &&, ||): Command chaining allows you to execute multiple commands consecutively. The ; operator executes commands in sequence, regardless of whether the previous command succeeded. On the other hand, the && operator only executes the second command if the first command succeeds, while || executes the second command only if the first fails. For instance, cd /var/log && ls changes to the /var/log directory and, if successful, lists its contents.
  4. Job Control (bg, fg, jobs, ctrl+z, &): Linux allows for great control over running processes. You can pause processes, resume them, run them in the background, or bring them back to the foreground. For example, a large file copy operation can be started with cp largefile newlocation & to run in the background, freeing up the terminal for other tasks.

These advanced features enhance the efficiency and flexibility of Linux commands, enabling users to perform complex operations, automate tasks, and customize their command-line experience. By mastering these features, you can unlock the full potential of the Linux command line.

How to Use Linux Commands

Having immersed ourselves in the world of Linux command features, it’s now time to transition into the practical aspect of our journey: using these commands effectively. In this section, we’ll explore a step-by-step guide to using basic Linux commands in the terminal, and share valuable tips for enhancing your command line efficiency.

  1. Open the Terminal: The first step to using Linux commands is to open the terminal. You can do this by searching for ‘terminal’ in your system’s application launcher or by using the keyboard shortcut Ctrl+Alt+T.
  2. Start with Basic Commands: Begin with simple commands like ls to list directory contents, cd to navigate between directories, and pwd to print your current working directory. Try these commands:
    • ls
    • cd /var
    • pwd
  3. Create and Remove Directories: Practice creating and removing directories using mkdir and rmdir respectively. For instance:
    • mkdir testdir
    • cd testdir
    • cd ..
    • rmdir testdir
  4. Create and View Files: Use the touch command to create a file and cat to view its contents. Try this:
    • touch testfile
    • cat testfile
  5. Delete Files: Practice caution while using the rm command to delete files as it does not move files to the trash, but deletes them permanently:
    • rm testfile
  6. Learn to Use Command Options: Most commands have options that modify their behavior. For instance, ls -l provides a detailed list of files, including permissions, number of links, owner, group, size, and time of last modification.
  7. Try Advanced Features: Once you are comfortable with the basics, explore advanced features like piping, redirection, and command chaining. Here’s an example of using piping and redirection together:
    • ls -l | grep ".txt" > txtfiles

Tips and Tricks for Efficient Command Usage

  • Use Tab Completion: When entering commands or file names, use the Tab key to auto-complete. This not only saves time but also reduces errors from typos.
  • Scroll Through Command History: Use the Up and Down Arrow keys to scroll through previously executed commands. This can be a great time-saver when you need to repeat commands.
  • Learn to Use man: The man command followed by any Linux command will bring up a helpful manual. Use this when you’re unsure about a command or want to learn more about its options: man ls.
  • Use clear or Ctrl+L to Clear the Terminal: Over time, the terminal can get cluttered with output from previous commands. Use clear or Ctrl+L to clear the terminal and start fresh.

Conclusion

In the world of operating systems, Linux commands stand out as a powerful tool that unlocks the true potential of the Linux operating system. The command-line interface offers unparalleled flexibility, resilience, and power, allowing users to interact with the system at a deeper level than graphical user interfaces can provide.

By understanding and utilizing Linux commands, you gain the ability to perform tasks efficiently and effectively. From simple operations like navigating directories and creating files to advanced features like piping, redirection, and command chaining, Linux commands offer a wide range of capabilities.

IF YOU ARE HAVING PROBLEMS WITH THIS GUIDE, EMAIL US AT:

Vivaldi Se6iujxznk

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

Copyright @2022-2024 All Right Reserved – PCPlanet

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. You understand and give your consent that your IP address and browser information might be processed by the security plugins installed on this site. By clicking “Accept”, you consent to the use of ALL the cookies.
.
Accept Read More

Privacy & Cookies Policy