Learning basic Linux commands is crucial for beginners. These commands provide foundational skills for effective system navigation, file management, and overall interaction with the Linux operating system. They help in efficiently moving around the system, managing files, and working with tasks. Check these from below and bookmark this tutorial to refer to whenever you need it.
Basic Linux Commands for Beginners
Firstly, let’s explore the system navigation commands like PWD, CD, LS, RM, etc.
1. Essential Navigation
Go through each of the commands and try to run it in your terminal.
1.1) pwd
Print Working Directory: This command displays the absolute path of your current location.
$ pwd
/home/user
Scenario 1: You’re working on a complex project with nested directories and symbolic links. You accidentally navigate through a symlink and pwd
shows a different path than expected. How do you identify the actual location considering both the real and linked directories?
Solution: Combine pwd
with realpath
to display the canonical path, resolving any symlinks: realpath
Explanation: This reveals the true location on the filesystem, regardless of symlinks, helping you understand the file structure accurately.
Scenario 2: You’re collaborating remotely with a teammate who provides instructions based on their directory structure. However, your directory layout differs. How can you translate their commands to work in your environment?
Solution: Use relative paths in their instructions and adjust them based on your directory structure. For example, if they say cd documents/project
, you might need cd my_documents/work
1.2) cd
Change Directory: Move between directories with this command. Use cd ~
to go to your home directory and cd ..
to move up one level.
$ cd Desktop
$ cd ..
Scenario 1: You’re managing a web server with multiple user accounts. One user reports an issue accessing a specific directory but cd
works fine for you. How can you diagnose the problem?
Solution: Check file permissions with ls -l
in the problematic directory. Look for incorrect ownership or group permissions that might restrict access for the affected user.
Explanation: Identifying permission issues helps you adjust settings accordingly to grant proper access.
Scenario 2: You’re automating tasks with a script that involves navigating to specific directories. How can you handle situations where directory names might change in the future?
Solution: Use relative paths instead of absolute paths whenever possible. Relative paths reference the current location, making your script more adaptable to directory structure changes.
1.3) ls
List Directory Contents: View the files and directories within your current directory. Use ls -l
for detailed information like permissions and sizes.
$ ls
$ ls -l
1.4) mkdir
Create Directory: Make a new directory with a chosen name.
$ mkdir documents
1.5) rm
Remove Files: Delete files cautiously. Be aware that rm
is permanent! Use rm -rf
with caution as it bypasses the trash bin.
$ rm file.txt
2. File and System Management
Now, let’s learn some important Linux commands that beginners can use for managing files and the system. These basics will make your tasks easier.
2.1) cat
View File Contents: Display the contents of a text file on the screen.
$ cat sample.txt
Scenario 1: You suspect a configuration file is corrupted on a production server, but directly editing it might cause downtime. How can you safely inspect the file content for errors without modifying it?
Solution: Use cat
with the -n
option to display line numbers along with the content. This allows you to pinpoint potential issues without accidentally changing anything.
Explanation: Line numbers guide you to specific sections of the file, making troubleshooting more efficient and preventing accidental modifications.
Scenario 2: You’re working with large log files that might overwhelm your terminal window. How can you effectively extract specific information without displaying the entire content?
Solution: Combine cat
with grep
to filter the output based on keywords or patterns. For example, cat access.log | grep "error"
shows only lines containing the word “error.”
Explanation: Filtering with grep
saves time and helps you focus on relevant information within large files.
2.2) cp
Copy Files: Create a duplicate of a file, specifying the source and destination.
$ cp image.jpg pictures/
Scenario 1: You’re copying a large file across a network, but the connection is unstable. How can you ensure the transfer completes even if it’s interrupted?
Solution: Use rsync
with the -P
option for partial transfer resumption: rsync -P large_file.zip server:/backup/
Explanation: rsync
resumes transfers from where they left off, making it ideal for unreliable connections or large files.
Scenario 2: You need to create multiple copies of a file with different names for testing purposes. How can you quickly generate these copies without repetitive typing?
Solution: Use cp
with brace expansion: cp template.txt {test1,test2,test3}.txt
Explanation: Brace expansion creates multiple files with specified names, saving time and effort.
2.3) mv
Move or Rename Files: Change a file’s name or location within the same filesystem.
$ mv document.doc report.docx
Scenario 1: You’re reorganizing files, but accidentally move a file to the wrong directory. How can you undo the move without resorting to backup restoration?
Solution: Use mv -i
for interactive mode, asking for confirmation before overwriting: mv -i file.txt correct_directory/
Explanation: Interactive mode prevents accidental overwrites, ensuring you move files to the intended destinations.
Scenario 2: You need to move a large directory containing thousands of files, but mv
seems to take a long time. How can you optimize the process?
Solution: Use rsync
with the -a
option for efficient transfer and preservation of permissions: rsync -a source_directory/ target_directory/
Explanation: rsync
can often handle large directories more efficiently than mv
, especially for network transfers.
2.4) man
Manual Pages: Access comprehensive help for any command by typing man <command name>
.
$ man ls
Scenario 1: You’re searching for information about a command, but man
displays a lengthy manual page with overwhelming details. How can you quickly find the specific section you need?
Solution: Use man -k
to search for keywords within manual pages: man -k "file permissions"
Explanation: Keyword search narrows down results, helping you locate relevant information faster.
Scenario 2: You’re working offline and need to access manual pages without internet connectivity. How can you have offline access to documentation?
Solution: Download manual pages using man -w
: `
3. User and Permissions
Beginners must learn about users and permissions commands in Linux to secure access control. This knowledge will help you know the threats and manage them.
3.1) whoami
Show the Currently Logged-in User: Identify your username on the system.
$ whoami
Scenario 1: You’re troubleshooting an application that requires specific user permissions to run. How can you verify if you’re logged in with the correct user account without remembering the username?
Solution: Combine whoami
with id
to display detailed user information, including username, group memberships, and user ID (UID): whoami && id
Explanation: This provides a comprehensive view of your user context, ensuring you have the necessary privileges for specific tasks.
Scenario 2: You’re collaborating on a project where multiple users need access to certain files. How can you manage user permissions effectively to prevent unauthorized modifications?
Solution: Use chown
and chgrp
to change file ownership and group ownership, respectively. Additionally, use chmod
to adjust file permissions (read, write, execute) for different user groups.
Explanation: Granular permission control ensures each user has the necessary access level, protecting data integrity and project security.
3.2) passwd
Change User Password: Modify your password for security purposes.
$ passwd
3.3) sudo
Execute Commands with Root Privileges: Perform actions requiring administrator permissions by preceding the command with sudo
and entering your password.
$ sudo apt update
4. Process Management Linux Commands for Beginners
Want your computer to run smoother and handle more tasks at once? Learn some basic Linux commands to control the running processes. You can make your computer run faster.
4.1) ps
List Running Processes: View information about active processes on your system.
$ ps
Scenario 1: Your system performance seems sluggish, but you’re unsure which processes are consuming resources. How can you identify potential bottlenecks and resource-intensive tasks?
Solution: Use ps aux
to display detailed process information, including CPU usage, memory usage, and command name. Look for processes with high CPU or memory utilization.
Explanation: Analyzing process information helps you pinpoint resource hogs and take appropriate actions, like optimizing applications or terminating unnecessary processes.
Scenario 2: You’re running a long-running script, but there’s no visual indication of its progress. How can you monitor its execution and track its resource usage?
Solution: Use top
to continuously display all running processes and their resource usage. You can filter by process ID (PID) from ps
to focus on your specific script.
Explanation: top
provides real-time insights into process activity, allowing you to monitor progress and resource consumption effectively.
4.2) top
Monitor System Activity: Observe CPU, memory, and process usage in real time.
$ top
4.3) kill
Terminate Processes: End a running process by specifying its process ID (PID). Use with caution!
$ kill 12345 (replace 12345 with the actual PID)
Scenario 1: An application freezes and becomes unresponsive. How can you terminate it safely without losing unsaved data?
Solution: Use kill -TERM
to send a termination signal to the process, allowing it to gracefully shut down and potentially save data. If that fails, use kill -KILL
as a last resort.
Explanation: Understanding different termination signals helps you control processes responsibly, minimizing data loss and system disruptions.
Scenario 2: You’re managing a web server with multiple child processes. How can you terminate specific child processes associated with a parent process, instead of killing the entire parent process?
Solution: Use pgrep
to find child processes based on the parent process ID and then kill
those specific child PIDs.
Explanation: Targeting specific child processes allows for more granular control, preventing unnecessary disruptions to the main service.
5. Text Manipulation and Scripting
Learning Linux commands for text manipulation and scripting can make beginners work more efficiently. Let’s find out which commands can help you do this.
5.1) grep
Search Text Files: Find lines matching a specific pattern within a file.
$ grep "error" system.log
5.2) sed
Stream Editor: Modify text files line by line using simple commands.
$ sed 's/old/new/g' file.txt
5.3) awk
Advanced Text Processing: Perform complex text manipulation and data extraction.
$ awk '{print $1, $3}' data.csv
5.4) sh
Shell Scripting: Automate repetitive tasks by writing shell scripts.
#!/bin/bash
echo "Hello, world!"
Remember:
- Practice is key. Experiment with these commands to gain more confidence.
- Read manual pages (
man
) to explore detailed info and options. - Take a backup of your system before running any deletion commands.
- Join online Linux communities for further learning.
Summary – Linux Commands for Beginners
If you’re new to Linux, this tutorial will help. Mastering basic Linux commands is the first step for beginners. It boosts your system knowledge and file management skills. This foundation will guide you in the future. When you need to automate tasks, this knowledge will help you work faster.
Happy Learning Linux,
Team TechBeamers