TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Basic Linux Commands for Beginners With Examples
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

Basic Linux Commands for Beginners With Examples

Last updated: Feb 25, 2024 10:33 am
By Meenakshi Agarwal
Share
13 Min Read
Linux Commands for Beginners With Examples
SHARE

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.

Contents
1. Essential Navigation1.1) pwd1.2) cd1.3) ls1.4) mkdir1.5) rm2. File and System Management2.1) cat2.2) cp2.3) mv2.4) man3. User and Permissions3.1) whoami3.2) passwd3.3) sudo4. Process Management Linux Commands for Beginners4.1) ps4.2) top4.3) kill5. Text Manipulation and Scripting5.1) grep5.2) sed5.3) awk5.4) sh

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

You Might Also Like

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

20 Bash Script Code Challenges for Beginners with Their Solutions

Difference Between Git Repository and Directory

Directory in Computer Aside from Git Bash GitHub

TAGGED:Basic Commands for Beginners
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Sort List of Lists in Python Explained With Examples Sorting List of Lists in Python Explained With Examples
Next Article Pandas Get Average Of Column Or Mean in Python Pandas Get Average Of Column Or Mean in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x