Are you a beginner programmer stepping into the captivating world of Linux? Brace yourself, for we’re about to unlock a treasure trove of knowledge that will propel you towards mastery of the Linux shell. In this post, we present you with 25 best linux shell commands that will unleash your potential and make you a force to be reckoned with in the Linux realm.
From navigating the file system with finesse to manipulating files and directories like a pro, these commands are the building blocks of your Linux journey. Whether you’re a curious tinkerer or an aspiring developer, understanding these essential tools will revolutionize the way you interact with your Linux system.
So, gear up, fellow adventurer, as we embark on a quest to uncover the secrets of the Linux shell. Prepare to embrace the power and flexibility of commands that will elevate your proficiency, empowering you to accomplish tasks with lightning speed and precision. Get ready to dive into the world of Linux wizardry with these 25 must-know shell commands!
The ‘Command-Line Interface (CLI)’ or ‘Terminal’ or ‘Shell’
The Command-Line Interface (CLI) or Terminal is a powerful tool that every Linux beginner should familiarize themselves with. At its core, the CLI is a text-based interface that allows users to interact with their operating system through commands. It may seem daunting at first, but mastering a few essential shell commands can greatly enhance your Linux programming experience.
So, what exactly is a shell? In Linux, the shell acts as the interface between the user and the operating system. It interprets the commands you enter and executes them, allowing you to perform various tasks efficiently. Think of it as your personal assistant, taking your instructions and swiftly carrying them out.
The shell’s importance cannot be overstated when it comes to Linux programming. It provides direct access to the system’s resources, enabling you to perform tasks that would otherwise require multiple steps through a graphical user interface. By using the command-line interface, you gain unmatched efficiency and versatility in managing files, executing programs, configuring system settings, and more.
Without further ado, let’s get to the definitive list of the commands.
List of 25 best Linux shell commands with example
1. Listing Files and Directories: `ls`
The `ls` shell command is an essential tool for Linux beginners, allowing you to view the files and directories in a specified location. Its simplicity and versatility make it a go-to command for exploring your system.
To get started, simply open your terminal and type `ls` followed by the desired location. For example, `ls /home/user/documents` will display the files and directories within the “documents” folder.
To enhance your listing experience, consider using useful options like `-l` for detailed information such as permissions, ownership, and file sizes. Additionally, the `-a` option reveals hidden files that start with a dot (.), providing a complete overview of your directory contents.
2. Changing Directories: `cd`
The `cd` linux shell command allows you to navigate through the file system and change directories effortlessly. One cannot use the shell and move around in the filesystem without this command.
To change to an absolute path, type `cd` followed by the full directory path. For example, `cd /home/user/documents` will take you directly to the “documents” folder.
Alternatively, you can use relative paths by specifying the destination relative to your current location. For instance, `cd ../pictures` will move you up one directory level and then into the “pictures” directory.
3. Present Working Directory: ’pwd’
The ’pwd’ linux shell command allows you to determine your current working directory within the file system. Understanding where you are in the file hierarchy is essential for effective navigation and file management.
To find out your present working directory, simply enter `pwd` in the terminal, and it will display the full path of your current location. This knowledge empowers you to understand the structure of the file system and make informed decisions about your actions.
With the `pwd` command, you’ll always know where you stand in the Linux file system.
4. Creating Directories: `mkdir`
The `mkdir` command is a vital tool for Linux beginners, enabling you to effortlessly create new directories within the file system. This is essential for organizing your files and projects effectively.
To create a directory, simply type `mkdir` followed by the desired directory name. For instance, `mkdir documents` will create a directory named “documents” in your current location.
You can also create multiple directories at once by specifying their names separated by spaces: `mkdir folder1 folder2 folder3`.
To create nested directory structures, use the `-p` option. For example, `mkdir -p parent/child/grandchild` will create a nested structure with three directories.
With the `mkdir` command, you’ll have the power to build a well-structured file system, ensuring efficient file organization and project management.
5. Removing Files and Directories: `rm`
The `rm` command is a very critical tool for Linux beginners, using which you can delete files and directories from your system. Understanding how to remove unwanted items is essential for maintaining a clean and organized file structure.
To delete a file, simply type `rm` followed by the file name: `rm myFile.txt`. The file will be permanently removed, so exercise caution.
For directories, use the `-r` option for recursive deletion: `rm -r myDirectory`. This option ensures that all files and subdirectories within the specified directory are removed as well.
Be careful when using the `rm` command, as deleted files cannot be easily recovered. Double-check your command before executing it to avoid accidental data loss.
6. Copying Files and Directories: `cp`
The `cp` command allows you to make copies of files and directories effortlessly. This command is crucial for duplicating and preserving important data.
To copy a file, use the `cp` command followed by the source file name and the destination directory: `cp myFile.txt /path/to/destination`. This creates an identical copy of the file in the specified location.
For directories, use the `-r` option for recursive copying: `cp -r myDirectory /path/to/destination`. This option ensures that all files and subdirectories within the specified directory are copied as well.
You can also rename the copied file or directory by specifying a new name in the destination: `cp myFile.txt /path/to/newFile.txt`.
7. Moving and Renaming Files: `mv`
The `mv` command allows you to move and rename files effortlessly. Understanding how to manipulate file locations and names is essential for effective file organization.
To move a file, use the `mv` command followed by the source file name and the destination directory: `mv myFile.txt /path/to/destination`. This relocates the file to the specified location.
For renaming a file, use the `mv` command and provide the current file name followed by the desired new name: `mv oldName.txt newName.txt`. This changes the file’s name without changing its location.
Remember, the `mv` command can be used for both moving and renaming files, offering flexibility and control over your file management tasks.
8. Creating Files: `touch`
The `touch` command allows you to create new files with ease.
To create a new file, simply type `touch` followed by the desired file name: `touch myFile.txt`. This will create an empty file with the specified name in your current directory.
Additionally, the `touch` command can be used to update the timestamp of an existing file. This can be handy for marking when a file was last accessed or modified without changing its content: `touch myFile.txt`.
9.Displaying File Contents: `cat`
The `cat` linux shell command is an essential tool for Linux beginners, allowing you to view the contents of a file directly in the terminal. Instead of using a text-editor, this is a quick way to view the contents. Also, this is a very useful command when you are not working with any Graphical User-Interface (GUI).
To display the contents of a file, simply type `cat` followed by the file name: `cat myFile.txt`. The file’s content will be printed in the terminal window.
To enhance readability, you can use the `-n` option to display line numbers alongside the content: `cat -n myFile.txt`. This is particularly useful for large files or when referencing specific lines.
10.Viewing File Headers and Footers: `head` and `tail`
The `head` and `tail` commands allow you to view the beginning and end of a file respectively. These are generally used to quickly inspect the contents of the file, for example logs.
To display the first few lines of a file, use the `head` command followed by the file name: `head myFile.txt`. By default, it shows the first 10 lines. You can specify a different number of lines using the `-n` option: `head -n 5 myFile.txt` displays the first 5 lines.
Similarly, to view the last few lines of a file, use the `tail` command: `tail myFile.txt`. The default is the last 10 lines, but you can adjust it with the `-n` option: `tail -n 3 myFile.txt` shows the last 3 lines.
11.Searching Text: `grep`
The `grep` command is an invaluable tool for Linux beginners, allowing you to search for specific text within files or any data given in the input stream like data passed via pipeline. This command is very critical for quickly locating information and extracting relevant data. Generally this is used to filter out the result of another command.
To search for a specific text pattern, use the `grep` command followed by the search term and the file name: `grep “pattern” myFile.txt`. This will display all lines containing the specified pattern.
For advanced searches, you can utilize regular expressions to define complex patterns: `grep -E “pattern” myFile.txt`.
To perform a case-insensitive search, use the `-i` option: `grep -i “pattern” myFile.txt`.
Also, to flip the results, i.e., to exclude all the lines with matching text, then you can use the `-V` option: `grep -V “pattern” myFile.txt`.
12. Changing File Permissions: `chmod`
The `chmod` command allows you to modify file permissions. Understanding how to manage permissions is essential for controlling access to your files and ensuring security.
To change file permissions, use the `chmod` command followed by the permission settings and the file name: `chmod permissions myFile.txt`. Permissions can be represented in symbolic or numeric form.
In symbolic mode, you can use symbols like `u` (user), `g` (group), `o` (others), `+` (add), – (remove), and = (set). For example, `chmod u+r myFile.txt` adds read permission for the user.
In numeric mode, permissions are represented by a three-digit number. Each digit corresponds to the user, group, and others, respectively. For instance, `chmod 644 myFile.txt` grants read and write permission to the user, and read-only permission to the group and others.
13. Terminating Processes: `kill`
The `kill` command is an essential tool for Linux beginners, allowing you to terminate running processes. Understanding how to manage processes is crucial for maintaining system stability and resolving issues.
To terminate a process, use the `kill` command followed by the process ID (PID) of the target process: `kill PID`. This sends a termination signal to the process, requesting it to exit gracefully.
Different termination signals can be used, such as SIGTERM (kill) for a graceful termination or SIGKILL (kill -9) for a forceful termination. For example, `kill -9 PID` will forcefully terminate the process.
To identify the PID of a process, use the `ps` command (covered earlier).
14. Viewing Command History: `history`
The `history` command is a valuable tool for Linux beginners, allowing you to view your command history. Mastering this command is essential for recalling and reusing previous commands, saving you time and effort.
To view your command history, simply type `history` in the terminal. It displays a numbered list of your recent commands, along with their execution order.
To rerun a specific command from your history, use `!` followed by the command number. For example, `!42` will rerun the command with number 42.
15. Accessing Manual Pages: `man`
The `man` command is an indispensable tool for Linux beginners, providing access to comprehensive manual pages. Mastering this command is essential for learning about commands, their options, and how to use them effectively.
To access the manual pages, simply type `man` followed by the command you want to learn more about: `man ls`. This opens the manual page for the specified command.
Within the manual pages, you can navigate using the arrow keys or press the spacebar to scroll down. To search for a specific term, press / followed by the search term.
To exit the manual pages, press `q` to return to the terminal prompt.
16. Downloading Files: `wget`
The `wget` command is a valuable tool for Linux beginners, allowing you to effortlessly download files from the internet. Mastering this command is essential for accessing resources and files for your projects.
To download a file, use the `wget` command followed by the URL of the file: `wget https://example.com/file.zip`. This retrieves the file and saves it in your current directory.
You can also specify the destination directory using the `-P` option: `wget -P /path/to/destination https://example.com/file.zip`.
For more advanced usage, `wget` offers additional options such as resuming downloads (-c), limiting download speed (–limit-rate), and downloading recursively (-r) for entire directories.
17. Archiving and Compressing Files: `tar`
The `tar` command is an essential tool for Linux beginners, allowing you to create and extract archives of files and directories. Mastering this command is essential for organizing and compressing data efficiently.
To create a tar archive, use the `tar`command followed by the `-cf` options and the desired archive name: `tar -cf archive.tar files…`. This creates a tar archive containing the specified files.
To extract files from a tar archive, use the `tar` command with the `-xf` options: `tar -xf archive.tar`. This extracts the contents of the tar archive to the current directory.
For compression, you can combine `tar` with compression tools like gzip (tar -czf) or bzip2 (tar -cjf) to create compressed tar archives.
18. Searching for Files and Directories: `find`
The `find` command is a powerful tool for Linux beginners, enabling you to search for files and directories based on various criteria. Mastering this command is essential for efficient file exploration and organization.
To search for files and directories, use the `find` command followed by the starting directory and the search criteria: `find /path/to/start -name “*.txt” -size +1M -mtime -7`. This example searches for text files larger than 1MB modified within the last 7 days.
You can specify search criteria based on name, size, modification time, permissions, and more. This flexibility allows you to tailor your search and locate specific files or directories.
19. Monitoring System Resources: `top`
The `top` command is a valuable tool for Linux beginners, allowing you to monitor system resources in real-time. Mastering this command is essential for understanding the performance and utilization of your system.
When you execute the `top` command, it displays a dynamic table with information about CPU usage, memory consumption, running processes, and more. The table refreshes periodically to provide up-to-date information.
To interpret the information, focus on key indicators such as CPU usage (%CPU), memory usage (%MEM), and load averages. Use the function keys (e.g., F9 to sort by a specific column) to customize the view.
To exit the `top` command, press `q`.
20. Securely Accessing Remote Systems: `ssh`
The `ssh` command is an essential tool for Linux beginners, allowing you to securely connect to remote systems. Mastering this command is essential for remote administration and accessing resources on remote machines.
To connect to a remote system, use the `ssh` command followed by the username and the IP address or domain name of the remote system: `ssh username@remote_ip`.
To specify a custom port for the connection, use the `-p` option: `ssh -p port_number username@remote_ip`.
For enhanced security, you can use a private key with the `-i` option: `ssh -i private_key_file username@remote_ip`.
21. Securely Copying Files between Systems: `scp`
The `scp` command is a valuable tool for Linux beginners, allowing you to securely copy files between local and remote systems. Mastering this command is essential for efficient file transfer and collaboration.
To copy a file from the local system to a remote system, use the `scp` command followed by the source file and the destination in the format `scp source_file username@remote_ip:destination_path`. For example, `scp myFile.txt username@remote_ip:/path/to/destination`.
To copy a file from a remote system to the local system, use the same command structure but reverse the source and destination. For example, `scp username@remote_ip:/path/to/source_file.txt`.
22. Calculating Disk Usage: `du`
The `du` command is an essential tool for Linux beginners, allowing you to calculate disk usage for files and directories. Mastering this command is crucial for managing storage space and identifying large files.
To determine disk usage, use the `du` command followed by the file or directory name: `du myFile.txt` or `du myDirectory`.
By default, `du` provides the disk usage in kilobytes. For a more human-readable output, use the `-h` option: `du -h myFile.txt`.
To see the total disk usage of a directory and its subdirectories, add the `-s` option: `du -sh myDirectory`.
23. Checking Disk Space Usage: `df`
The `df` command is a vital tool for Linux beginners, allowing you to check disk space usage on your system. Mastering this command is essential for monitoring available storage and managing disk space effectively.
To check disk space usage, simply type `df` in the terminal. It displays a table with information about filesystems, including the total, used, and available space.
The “total” space represents the overall capacity of the filesystem, “used” indicates the amount of space already occupied, and “available” shows the remaining space that can be utilized.
24. Shutting Down or Rebooting the System: `shutdown`
The `shutdown` command is a crucial tool for Linux beginners, allowing you to gracefully shut down or reboot the system. Mastering this command is essential for proper system management and maintenance.
To shut down the system, use the `shutdown` command followed by the `-h` option: `shutdown -h now`. This initiates an immediate system halt.
To reboot the system, use the `-r` option: `shutdown -r now`. This triggers an immediate system reboot.
For scheduled shutdown or reboot, you can specify a future time using the `+` option: `shutdown -h +10 “System will halt in 10 minutes”`.
In conclusion, mastering the Linux shell commands presented in this blog will unlock a world of possibilities and transform you into a Linux pro. By familiarizing yourself with these indispensable tools, you will gain the power to navigate the file system, manipulate files and directories, and accomplish tasks with speed and precision.
The Command-Line Interface (CLI) or Terminal acts as your gateway to the Linux operating system, providing direct access to its resources. By harnessing the efficiency and versatility of shell commands, you can manage files, execute programs, configure system settings, and more, all with unparalleled control.
So gear up and embrace the power and flexibility of these shell commands. Let them unleash your potential and elevate your proficiency, allowing you to accomplish tasks with lightning speed and precision. Embark on a quest to uncover the secrets of the Linux shell and dive into the world of Linux wizardry with these must-know commands. Your Linux journey starts here!