DEV Community

Cover image for Unlocking Linux: A Beginner’s Guide to Essential Commands and Shell Scripting
RB Punna
RB Punna

Posted on

Unlocking Linux: A Beginner’s Guide to Essential Commands and Shell Scripting

Why Linux? Understanding the Power Behind the OS

Linux has steadily gained popularity over the years, especially in server environments, software development, and IT operations. What makes it so special? Here’s why Linux stands out:

1. Free
Linux is completely free! You don’t need to purchase expensive licenses or subscriptions to use it. It’s accessible to everyone.

2. Open Source
Linux’s open-source nature allows users to inspect, modify, and distribute the code. This fosters a global community of developers working to improve the system.

3. Secure
With frequent updates and its permission-based architecture, Linux is one of the most secure operating systems available. It’s far less susceptible to viruses and malware than its competitors.

4. Versatile Flavors
Linux comes in many distributions (distros) such as Ubuntu, CentOS, Debian, and Fedora, catering to different needs, from enterprise solutions to personal computing.

5. Fast Performance
Linux’s lightweight structure makes it fast and resource-efficient. It’s especially suited for older hardware or systems requiring high performance, like servers.

Linux Shell: Where the Magic Happens

The shell is the interface between the user and the Linux operating system. It’s where you enter commands and where most of your interaction with the system happens. Let’s dive into the essential Linux commands that every user should know.

Basic Navigation and Directory Management

Linux treats everything as a file, even directories (folders), making navigation a core skill to master.

1. Understanding Your Current Location:
pwd – Print Working Directory. Displays the path of your current directory.

pwd
Enter fullscreen mode Exit fullscreen mode

2. Listing Files and Directories:
ls – List the contents of a directory.

ls
Enter fullscreen mode Exit fullscreen mode

ls -ltr – List files and directories in long format, showing file properties and sorted by time.

ls -ltr
Enter fullscreen mode Exit fullscreen mode

3. Changing Directories:
cd – Change directory. You can move between directories using this command.

cd /path/to/directory     # Move to specific directory
cd ..                     # Move to parent directory
cd ../..                  # Move two directories up
Enter fullscreen mode Exit fullscreen mode

cd directory1/directory2 – Navigate through multiple directories in one go.

4. Creating Directories:
mkdir– This command is used to create a new directory.

mkdir my_new_directory
Enter fullscreen mode Exit fullscreen mode

You can also create multiple directories at once:

mkdir dir1 dir2 dir3
Enter fullscreen mode Exit fullscreen mode

mkdir -p – This option creates parent directories if they don’t already exist. For example, if you want to create a directory inside another directory that doesn't exist yet, use -p:

mkdir -p parent_directory/child_directory
Enter fullscreen mode Exit fullscreen mode

5. Removing Directories:
rmdir – Removes an empty directory.

rmdir empty_directory
Enter fullscreen mode Exit fullscreen mode

rm -r – Removes a directory and its contents recursively.

rm -r directory_to_remove
Enter fullscreen mode Exit fullscreen mode

File Management
Working with files is a daily task for every Linux user, and there are several commands that make this easy.

1. Creating and Editing Files:
touch– Create a new, empty file.

touch newfile.txt
Enter fullscreen mode Exit fullscreen mode

vi – Open or create a file and start editing with the vi text editor.

vi test.txt
Enter fullscreen mode Exit fullscreen mode
  • Press i to switch to insert mode and start typing.
  • Press Esc to exit insert mode.
  • Type :wq! to save and exit.

2. Viewing File Contents:
cat – Display the contents of a file.

cat test.txt
Enter fullscreen mode Exit fullscreen mode

System Monitoring
Monitoring system resources and performance is crucial, especially in server environments or during heavy computation tasks.

1. Checking Memory Usage:
free -g – Display memory usage in gigabytes.

free -g
Enter fullscreen mode Exit fullscreen mode

2. Checking CPU Information:
nproc – Display the number of CPU cores available.

nproc
Enter fullscreen mode Exit fullscreen mode

3. Checking Disk Space:
df -h – Display available disk space in a human-readable format.

df -h
Enter fullscreen mode Exit fullscreen mode

4. Real-Time System Monitoring:
top – Display real-time system information, including CPU and memory usage and running processes.

top
Enter fullscreen mode Exit fullscreen mode

Shell Scripting: Automating Tasks in Linux

Linux is not just about running commands manually. Shell scripting allows you to automate repetitive tasks, making your life easier, especially in DevOps environments.

1. Creating a Simple Shell Script:
Step 1: Create the script
Use the touchcommand to create a script file:

touch myscript.sh
Enter fullscreen mode Exit fullscreen mode

Step 2: Edit the script
Open the script using vior vimand add your commands:

vi myscript.sh
Enter fullscreen mode Exit fullscreen mode

Step 3: Add a Shebang
The shebang (#!/bin/bash) at the top tells the system that this is a bash script:

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Commands
Write your commands under the shebang, like printing a message:

echo "Hello, Linux!"
Enter fullscreen mode Exit fullscreen mode

Step 5: Save and exit
Press Esc and type :wq! to save and exit.

2. Running the Shell Script:
Before running the script, give it execute permission:

chmod +x myscript.sh
Enter fullscreen mode Exit fullscreen mode

Now run the script:

./myscript.sh
Enter fullscreen mode Exit fullscreen mode

3. Managing File Permissions with chmod
In Linux, every file has three types of permissions: read, write, and execute. Using the chmod command, you can modify these permissions for the owner, group, and others. Let’s look at how you can manage file permissions using chmod with some practical examples:

Each group has three types of access:

Read (4) (r)
Write (2) (w)
Execute (1) (x)

4. Granting Read Permission:
To allow a user to read a file, you can use the +r option:

chmod +r myscript.sh
Enter fullscreen mode Exit fullscreen mode

This command gives read permission to the file myscript.sh.

5. Granting Write Permission:
To give write permission (allowing modification of the file), use the +w option:

chmod +w myscript.sh
Enter fullscreen mode Exit fullscreen mode

This allows you or other users to edit the file myscript.sh.

6. Granting Execute Permission:
To make a script or program executable, use the +x option:

chmod +x myscript.sh
Enter fullscreen mode Exit fullscreen mode

This command allows the file myscript.sh to be run as an executable program.

Combining Permissions:
You can also combine multiple permissions in a single command. For example, to give read, write, and execute permissions to a file, use

chmod +rwx myscript.sh
Enter fullscreen mode Exit fullscreen mode

Or, using numeric notation:

chmod 777 myscript.sh
Enter fullscreen mode Exit fullscreen mode

This would grant read, write, and execute permissions to everyone.

DevOps and Shell Scripting: Why It’s Essential

Shell scripting is the backbone of many DevOps operations. Here’s why:

Automating Repetitive Tasks: From deploying applications to managing configurations, shell scripts reduce manual effort.

Efficiency: By scripting routine tasks, you can improve the overall efficiency of the system.

Monitoring and Maintenance: Automate tasks like system monitoring, log rotation, backups, and more using shell scripts.

Bonus: Additional Useful Linux Commands

1. Getting Command Options:
man – The manual for any command. For example, man ls will show all the options for the ls command.

man ls
Enter fullscreen mode Exit fullscreen mode

2. History of Commands:
history – Display the list of commands you've previously entered.

history
Enter fullscreen mode Exit fullscreen mode

3. Canceling a Running Command:
Ctrl+C – Stop a running program in the terminal.

Conclusion

Linux is a powerful and flexible operating system that rewards users who take the time to master its command-line interface. By learning and practicing the commands outlined in this post, you’ll unlock a world of potential — whether you’re navigating the file system, managing resources, or automating tasks through shell scripting.

The key to becoming proficient in Linux is practice. So fire up your terminal, run these commands, and start exploring the endless possibilities that Linux offers!

Thank you for reading..!! If you like the post, Clap and comment..!!

If you feel any other important Linux command to be added, please mention in comment, I will try to add.

I will also post another article on Shell Scripting soon here.

Top comments (1)

Collapse
 
josephj11 profile image
Joe • Edited

Nice. Hadn't seen nproc before.

In general, I'd recommend chmod 755 somefile. It's a bit safer by default.

As part of bare bones basic, I'd also include a pager. Most people use less. I prefer most, but it isn't installed by default.

Although all hardcore users use vi or vim, it has a bit of a steep learning curve. nano is probably easier for beginners. I live in kate, but choice of editors is very personal.

The only other command I'd add (without getting into the GNU tools) is apropos because it helps you find everything else.