Essential Linux Commands: A Comprehensive Guide for Beginners

Introduction

Linux is a free and open-source operating system based on Unix that is widely used in servers, supercomputers, and embedded devices. Linux provides a powerful command-line interface, which allows users to perform tasks quickly and efficiently without the need for a graphical user interface.

In this tutorial, we will cover the most useful basic Linux commands with examples.

Table of Content

  1. Basic Navigation Commands


  2. File Manipulation Commands


  3. Text Manipulation Commands


  4. System Management Commands


  5. Network Commands


  6. Text Editing Commands


  7. User Management Commands


  8. Miscellaneous Commands



1. Basic Navigation Commands

  1. pwd (print working directory)

The pwd command is used to print the full path of the current working directory. This can be useful when you need to know the exact location of a file or directory.

Syntax: pwd

Example:

bash
$ pwd /home/user/Documents

In this example, the current working directory is /home/user/Documents.

  1. ls

The ls command is used to list the contents of a directory. By default, it displays the names of files and directories in the current directory.

Syntax: ls [options] [directory]

Examples:

  • ls: This will list the contents of the current directory.
  • ls /home/user/Documents: This will list the contents of the directory /home/user/Documents.

Some useful options for ls are:

  • -a: List all files, including hidden files.
  • -l: Use a long listing format, which shows additional information such as permissions and ownership.
  • -h: Display file sizes in a human-readable format.

  1. cd: Change Directory

The cd command is used to change the current working directory.

Syntax: cd [directory]

Examples:

  • cd /home/user/Documents: This will change the current working directory to /home/user/Documents.
  • cd ..: This will move up one directory level.

  1. mkdir: Make Directory

The mkdir command is used to create a new directory.

Syntax: mkdir [options] directory

Examples:

  • mkdir new_directory: This will create a new directory named "new_directory" in the current working directory.
  • mkdir -p /home/user/Documents/new_directory: This will create a new directory named "new_directory" inside the "Documents" directory in the home directory of the user "user".

Some useful options for mkdir are:

  • -p: Create parent directories if they don't exist.

  1. rmdir

The rmdir command is used to remove an empty directory.

Syntax: rmdir [options] directory

Examples:

  • rmdir old_directory: This will remove the directory named "old_directory" from the current working directory, but only if it is empty.
  • rmdir -p /home/user/Documents/old_directory: This will remove the directory named "old_directory" from the "Documents" directory in the home directory of the user "user", but only if it is empty.

Some useful options for rmdir are:

  • -p: Remove parent directories if they become empty after the directory is removed.

  1. touch

The touch command is used to create a new empty file. If the file already exists, it will update its modification time.

Syntax: touch [options] file(s)

Examples:

  • touch new_file.txt: This will create a new empty file named "new_file.txt" in the current working directory.
  • touch -t 202201010000.00 new_file.txt: This will change the modification time of the file named "new_file.txt" to January 1, 2022, at midnight.

Some useful options for touch are:

  • -a: Change only the access time of the file.
  • -c: Do not create the file if it does not exist.
  • -m: Change only the modification time of the file.

2. File Manipulation Commands

1. cp: Copy Files or Directories

The cp command is used to copy files or directories. It takes two arguments: the source file or directory, and the destination file or directory. If the destination is a file, the source file will be copied to that file. If the destination is a directory, the source file will be copied to that directory.

Syntax:

bash
cp [OPTIONS] SOURCE DESTINATION

Examples:

To copy a file named file1.txt to another file named file2.txt:

bash
cp file1.txt file2.txt

To copy a directory named dir1 and its contents to a new directory named dir2:

bash
cp -r dir1/ dir2/

2. mv: Move or Rename Files or Directories

The mv command is used to move or rename files or directories. It takes two arguments: the source file or directory, and the destination file or directory. If the destination is a file, the source file will be moved to that file. If the destination is a directory, the source file will be moved to that directory.

Syntax:

bash
mv [OPTIONS] SOURCE DESTINATION

Examples:

To rename a file named file1.txt to file2.txt:

bash
mv file1.txt file2.txt

To move a file named file1.txt to a directory named dir1:

bash
mv file1.txt dir1/

3. rm: Remove Files or Directories

The rm command is used to remove files or directories. It takes one or more arguments, which can be file or directory names. By default, rm will not remove directories, unless the -r option is used.

Syntax:

bash
rm [OPTIONS] FILENAME

Examples:

To remove a file named file1.txt:

bash
rm file1.txt

To remove a directory named dir1 and its contents:

bash
rm -r dir1/

4. cat: Concatenate Files and Print to Standard Output

The cat command is used to display the contents of one or more files to the terminal. It takes one or more file names as arguments and prints their contents to the terminal.

Syntax:

bash
cat [OPTIONS] FILENAME

Examples:

To display the contents of a file named file1.txt:

bash
cat file1.txt

5. head: Print First N Lines of a File

The head command is used to display the first N lines of a file. By default, head will display the first 10 lines of a file, but this can be changed with the -n option.

Syntax:

bash
head [OPTIONS] FILENAME

Examples:

To display the first 5 lines of a file named file1.txt:

bash
head -n 5 file1.txt

6. tail: Print Last N Lines of a File

The tail command is used to display the last N lines of a file. By default, tail will display the last 10 lines of a file, but this can be changed with the -n option.

Syntax:

bash
tail [OPTIONS] FILENAME

Examples:

To display the last 5 lines of a file named file1.txt:

bash
tail -n 5 file1.txt


7. less: Display File Content One Screen at a Time

The less command is used to view the contents of a file one screen at a time. This is useful when viewing large files as it allows you to navigate through the file quickly and easily. To view a file with less, simply type:

bash
less /path/to/file

Once you are in less, you can navigate through the file using the arrow keys or the page up and page down keys. You can also search for text within the file by typing / followed by the search term.

8. chmod: Change File Permissions

The chmod command is used to change the permissions of a file or directory. The basic syntax for the chmod command is as follows:

bash
chmod [permissions] /path/to/file

The permissions are represented by three digits, each representing the permissions for the owner, group, and others respectively. The permissions can be specified using the following table:

PermissionNumeric ValueDescription
Read4Allows reading of the file or directory
Write2Allows writing to the file or directory
Execute1Allows executing the file or traversing the directory
No Permission0No permission granted

For example, to give the owner read and write permissions and the group and others read-only permissions, you can use the following command:

bash
chmod 644 /path/to/file

9. chown: Change File Ownership

The chown command is used to change the ownership of a file or directory. The basic syntax for the chown command is as follows:

bash
chown [owner]:[group] /path/to/file

To change the owner of a file, you can use the following command:

bash
chown newowner /path/to/file

To change the owner and group of a file, you can use the following command:

bash
chown newowner:newgroup /path/to/file

3. Text Manipulation Commands

1. echo: Print Text or Variables to Standard Output

The echo command is used to print text or variables to the standard output. The basic syntax for the echo command is as follows:

css
echo [options] [text or variables]

For example, to print the text "Hello, World!", you can use the following command:

bash
echo "Hello, World!"

To print the value of a variable, you can use the following command:

bash
echo $VAR

2. grep: Search Text for Patterns and Print Matching Lines

The grep command is used to search text for patterns and print the matching lines. The basic syntax for the grep command is as follows:

css
grep [options] [pattern] [file or directory]

For example, to search for the pattern "hello" in a file named "file.txt", you can use the following command:

perl
grep "hello" file.txt

To search for the pattern "hello" in all files in a directory named "dir", you can use the following command:

bash
grep "hello" dir/*

3. sed: Stream Editor for Modifying Text

The sed command is used to modify text in a file or stream. The basic syntax for the sed command is as follows:

css
sed [options] [commands] [file]

For example, to replace all occurrences of the word "foo" with the word "bar" in a file named "file.txt", you can use the following command:

arduino
sed 's/foo/bar/g' file.txt


4. awk: Pattern Scanning and Processing Language

The awk command is used for text processing, and it's a powerful tool for extracting information from files. It's commonly used for data manipulation, text processing, and generating reports.

The basic syntax of awk is as follows:

arduino
awk 'pattern { action }' file

Here, the pattern is a regular expression that defines a matching pattern in the input file. The action is the command that awk will execute when it finds a match. If no pattern is provided, awk will perform the specified action on every line in the input file.

For example, the following command will print all the lines in the file that contain the word "error":

arduino
awk '/error/ { print }' file.txt

The following command will print the second field of each line in the file:

arduino
awk '{ print $2 }' file.txt

We can also use awk to do simple calculations. For example, the following command will calculate the total size of all files in the current directory:

bash
ls -l | awk '{ total += $5 } END { print "Total size: ", total }'

The END keyword in the above command specifies that the action should be executed at the end of the input file. Here, we're adding up the size of each file and printing the total size at the end.

Overall, awk is a powerful tool for manipulating and processing text data, and it's worth taking the time to learn its syntax and features.


4. Text Editing Commands

In addition to the basic text manipulation commands, there are several text editing commands available in Linux. These commands allow you to create and edit text files directly in the terminal.

1. nano

Nano is a user-friendly text editor that is included in most Linux distributions. It is a basic editor that provides syntax highlighting and keyboard shortcuts for common tasks.

To open a file with nano, simply type "nano" followed by the name of the file:

nano filename.txt

This will open the file in the nano editor. You can then make changes to the file as needed using the various keyboard shortcuts.

2. vi/vim:

Vi and Vim are two of the most popular text editors in Linux. They are powerful editors that offer many advanced features, but they have a steeper learning curve than nano.

To open a file with vi, simply type "vi" followed by the name of the file:

vi filename.txt

This will open the file in vi. To switch to insert mode and start editing the file, press the "i" key. To save and exit the file, press the "Esc" key followed by ":wq" (write and quit).

3. emacs:

Emacs is a popular and extensible text editor. It can be used to create and edit text files, source code, and much more.

Example usage:

To open an existing file in emacs, simply run the following command in the terminal:

bash
emacs /path/to/file.txt

This will open the file in emacs, allowing you to edit and save changes.


5. User Management Commands

Linux has several commands for managing user accounts and groups. These commands allow you to add, modify, and remove user accounts and groups.

1. useradd

The useradd command is used to create a new user account. To create a new user, simply type "useradd" followed by the username:

useradd username

This will create a new user account with the specified username. You can then set a password for the user using the passwd command.

2. passwd

The passwd command is used to set or change a user's password. To change a user's password, simply type "passwd" followed by the username:

passwd username

You will then be prompted to enter and confirm the new password for the user.

3. usermod

The usermod command is used to modify an existing user account. This command allows you to change the user's username, home directory, default shell, and other settings.

To modify an existing user account, simply type "usermod" followed by the username and the desired options:

bash
usermod username -s /bin/bash -d /home/newdir

This command will change the user's default shell to /bin/bash and their home directory to /home/newdir.

4. userdel

The userdel command is used to delete an existing user account. To delete a user account, simply type "userdel" followed by the username:

userdel username

This will delete the user account and all associated files and directories.

5. groupadd

The groupadd command is used to create a new group. To create a new group, simply type "groupadd" followed by the group name:

groupadd groupname

This will create a new group with the specified name.

6. groupmod

The groupmod command is used to modify an existing group. This command allows you to change the group's name or GID.

To modify an existing group, simply type "groupmod" followed by the group name and the desired options:

groupmod groupname -n newgroupname

This command will change the group's name to "newgroupname".

7. groupdel

The groupdel command is used to delete an existing group. To delete a group, simply type "groupdel" followed by the group name:

groupdel groupname

This will delete the group and remove all users from the group.


6. System Management Commands

1. ps

ps command is used to display information about the running processes on a Linux or Unix-like operating system. It provides information about the process ID (PID), user ID (UID), memory usage, CPU usage, etc.

The basic syntax of the ps command is:

css
ps [options]

Some commonly used options of the ps command are:

  • aux: Show all processes for all users with more details
  • u: Display process owner and other details
  • x: Show processes that are not attached to a terminal

Example:

ps aux | less

This command will display all processes running on the system, one page at a time using the less command.

2. top

top command is used to display real-time information about the system processes and resource usage. It provides information about the total number of processes, memory usage, CPU usage, load average, and more.

The basic syntax of the top command is:

css
top

When you run this command, you will see a real-time display of the system processes and resource usage. Press q to exit.

3. systemctl

systemctl command is used to control the systemd system and service manager on Linux systems. It is used to start, stop, and restart system services and daemons.

The basic syntax of the systemctl command is:

css
systemctl [command] [unit]

Some commonly used commands of the systemctl command are:

  • start: Start a service
  • stop: Stop a service
  • restart: Restart a service
  • status: Display the status of a service
  • enable: Enable a service to start at boot time
  • disable: Disable a service from starting at boot time

Example:

lua
sudo systemctl status apache2

This command will display the status of the Apache web server service.

4. ifconfig

ifconfig command is used to configure and display network interfaces on Linux systems. It provides information about network interface configuration, such as IP address, netmask, and MAC address.

The basic syntax of the ifconfig command is:

css
ifconfig [interface] [options]

Some commonly used options of the ifconfig command are:

  • up: Enable the specified interface
  • down: Disable the specified interface
  • inet: Display IPv4 address
  • inet6: Display IPv6 address
  • netmask: Display netmask
  • hw: Display MAC address

Example:

ifconfig eth0

This command will display the configuration of the eth0 network interface.

5. netstat

netstat command is used to display network connection information on Linux systems. It provides information about network connections, routing tables, and network interface statistics.

The basic syntax of the netstat command is:

css
netstat [options]

Some commonly used options of the netstat command are:

  • a: Show all connections
  • t: Show TCP connections
  • u: Show UDP connections
  • n: Display numerical addresses instead of resolving hostnames
  • p: Display process that owns the socket
  • r: Display routing table

Example:

netstat -tulpn

This command will display all the listening TCP and UDP ports on the system along with the process ID that owns them.

6. sudo apt-get: Package Management for Debian-Based Systems

In Debian-based Linux distributions, apt-get is the command-line tool for package management. It allows you to install, upgrade, and remove packages on your system.

Here are some of the most commonly used apt-get commands:

a. apt-get update: This command updates the list of available packages and their versions from the configured repositories.

Example:

sql
sudo apt-get update
  • b. apt-get upgrade: This command upgrades all the installed packages to their latest versions.

Example:

arduino
sudo apt-get upgrade
  • c. apt-get install: This command installs one or more packages from the repositories.

Example:

arduino
sudo apt-get install package_name
  • d. apt-get remove: This command removes one or more packages from the system.

Example:

arduino
sudo apt-get remove package_name
  • e. apt-get autoremove: This command removes any packages that were automatically installed as dependencies and are no longer required.

Example:

arduino
sudo apt-get autoremove

7. Find: Search for Files and Directories

The find command is a powerful tool for searching for files and directories on your system. It allows you to specify search criteria based on file name, type, size, modification time, and other attributes.

Here are some commonly used options for the find command:

  • -name: Search for files or directories with a specific name.

Example:

arduino
find /home/user -name myfile.txt
  • -type: Search for files or directories of a specific type, such as regular files or directories.

Example:

arduino
find /home/user -type d
  • -size: Search for files of a specific size, such as greater than or less than a certain size.

Example:

arduino
find /home/user -size +10M
  • -mtime: Search for files based on their modification time, such as newer or older than a certain date.

Example:

arduino
find /home/user -mtime -7

These are just a few examples of what you can do with the find command. It's a powerful tool that can be used in many different ways to search for files and directories on your system.

7. Network Commands

1. ping

The ping command is used to test network connectivity by sending packets to a specific host or IP address and waiting for a response. It can be used to check if a host is up and reachable, and to measure the response time.

Here's an example of using ping:

$ ping google.com PING google.com (142.251.36.14) 56(84) bytes of data. 64 bytes from fra16s29-in-f14.1e100.net (142.251.36.14): icmp_seq=1 ttl=117 time=7.36 ms 64 bytes from fra16s29-in-f14.1e100.net (142.251.36.14): icmp_seq=2 ttl=117 time=8.15 ms 64 bytes from fra16s29-in-f14.1e100.net (142.251.36.14): icmp_seq=3 ttl=117 time=7.53 ms

This command sends packets to the Google website and waits for a response. It will continue to do so until you interrupt it with Ctrl + C. You can also specify the number of packets to send using the -c option, and the interval between packets using the -i option.

2. traceroute

traceroute is a command-line tool that is used to trace the route of packets across a network. It sends packets with varying Time-To-Live (TTL) values to the target host and listens for the ICMP Time Exceeded message returned by each router in the path. By analyzing the TTL values of these messages, traceroute can determine the path taken by the packets. The syntax for the traceroute command is:

css
traceroute [options] host

Here, host refers to the IP address or domain name of the target host. Some commonly used options are:

  • -I: Use ICMP echo requests instead of UDP packets.
  • -p: Use a specific destination port for the packets.
  • -q: Number of packets to send per TTL value.
  • -t: Maximum TTL value to use.

For example, to trace the route of packets to the Google DNS server (8.8.8.8), with a maximum TTL of 20, you can use the following command:

traceroute -t 20 8.8.8.8

3. ssh

The ssh command is used to connect to a remote server securely using the SSH protocol. It provides a secure encrypted connection between the local and remote computers, and allows you to run commands and transfer files securely.

Here's an example of using ssh:

sql
ssh user@192.168.0.100

This command connects to a remote server at IP address 192.168.0.100 using the username user. You will be prompted for the password for the specified user, and then you will be logged in to the remote server. From there, you can run commands on the remote server as if you were sitting at the console.

Let's continue with the remaining commands.

4. curl

Curl is a command-line tool that allows you to transfer data to or from a server using various protocols such as HTTP, FTP, and more.

Example usage:

To download a file using curl, run the following command in the terminal:

arduino
curl -O http://example.com/file.txt

This will download the file.txt from the given URL and save it to the current directory.


8. Miscellaneous Commands

1. whoami

The whoami command is used to display the username of the current user. This is particularly useful when you're logged in as a different user, and need to know the current user's username.

shell
$ whoami johndoe

2. uptime

The uptime command displays how long the system has been running since the last reboot, as well as the current system load average for the past 1, 5, and 15 minutes.

shell
$ uptime 14:47:12 up 2:32, 1 user, load average: 0.08, 0.04, 0.05

3. history

The history command is used to view a list of the previously executed commands in the current session. This is particularly useful when you want to repeat a command that you've used before.

bash
$ history 1 ls 2 cd .. 3 pwd 4 history

You can also use the history command with a numerical argument to display a specific number of commands. For example, history 5 will display the last 5 commands that were executed.

4. clear

The clear command is used to clear the terminal screen of all previous commands and output.

arduino
$ clear

This will clear the terminal screen, making it easier to view and work with new commands and output.

5. man

The man command is used to view the manual pages for a particular command. This provides detailed information on how to use the command, its options, and its arguments.

shell
$ man ls

This will display the manual pages for the ls command, providing you with detailed information on how to use it.

6. exit

The exit command is used to exit the current terminal session.

shell
$ exit

This will exit the terminal session, closing the terminal window or tab.

7. scp

scp (secure copy) is a command-line tool for securely copying files between remote and local hosts. It uses the SSH (Secure Shell) protocol to transfer files with encryption and authentication.

Here are some common uses of scp:

  • To copy a file from a remote host to your local machine, run scp username@remote:/path/to/file /path/to/destination.
  • To copy a file from your local machine to a remote host, run scp /path/to/file username@remote:/path/to/destination.
  • To copy a directory and its contents, add the -r flag to the command: scp -r /path/to/directory username@remote:/path/to/destination.

Here is an example of copying a file from a remote host to your local machine:

ruby
scp user@remote:/path/to/file /path/to/destination

8. crontab

crontab is a command-line tool for scheduling tasks or commands to run automatically at specified times. It is useful for automating repetitive tasks or for running scripts at specific intervals.

Here are some common uses of crontab:

  • To edit your user's crontab, run crontab -e.
  • To list your user's crontab, run crontab -l.
  • To remove your user's crontab, run crontab -r.
  • To create a new crontab file, run `cront

Here is an example of creating a crontab entry that runs a script every hour:

  1. Open the crontab editor by running crontab -e.
  2. Add the following line to the crontab file:
javascript
0 * * * * /path/to/script.sh

This entry will run the script /path/to/script.sh every hour at the beginning of the hour (i.e., at 1:00, 2:00, 3:00, etc.).

9. df

df (disk free) is a command-line tool for displaying information about disk usage on a Linux system. It shows the amount of disk space used and available for each file system.

Here are some common uses of df:

  • To display disk space usage in human-readable format, run df -h.
  • To show disk space usage for a specific file system, run df /path/to/filesystem.
  • To display disk space usage for all file systems, run df -a.

Here is an example of running df -h:

bash
Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 788M 2.1M 785M 1% /run /dev/sda1 98G 8.1G 85G 9% / tmpfs 3.9G 29M 3.9G 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda2 197G 131G 56G 71% /data tmpfs 788M 24K 788M 1% /run/user/1000

10. tar

The tar command is used to create, manipulate, and extract tar archives.

Example usage:

To create a tar archive of a directory, run the following command:

bash
tar -czvf archive.tar.gz /path/to/directory

This will create a compressed tar archive of the specified directory. The options used are:

  • -c: create a new archive
  • -z: compress the archive using gzip
  • -v: verbosely list the files processed
  • -f: use archive file specified

To extract a tar archive, run the following command:

tar -xzvf archive.tar.gz

This will extract the contents of the tar archive to the current directory. The options used are:

  • -x: extract files from the archive
  • -z: uncompress the archive using gzip
  • -v: verbosely list the files processed
  • -f: use archive file specified

11. date

The date command is used to display the current date and time in the terminal.

Example usage:

To display the current date and time in the terminal, simply run the following command:

bash
date

This will display the current date and time in the format:

yaml
Sun Apr 17 16:23:40 EDT 2022

12. free

The free command is used to display the amount of free and used memory in the system.

Example usage:

To display memory usage in the system, simply run the following command:

c
free -h

This will display memory usage in human-readable format.

13. uname

The uname command is used to display information about the system.

Example usage:

To display system information such as kernel name, version, and release, run the following command:

bash
uname -a

This will display the system information in the format:

graphql
Linux hostname 5.11.0-41-generic #45~20.04.1-Ubuntu SMP Wed Nov 10 10:20:10 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Remember that Linux is a powerful and flexible operating system that can be customized to meet your needs. The command line interface is just one of the many tools available to you. There are also graphical user interfaces (GUIs) that you can use to manage your system. However, mastering the command line interface will give you a deeper understanding of how your system works and allow you to perform tasks more quickly and efficiently.

In conclusion, learning and using basic Linux commands is essential for anyone who wants to use the Linux operating system. With the commands we have covered in this tutorial, you can perform a variety of tasks such as navigating the file system, managing files and directories, manipulating text, and managing system services. With practice, you will become proficient in using the command line interface and will be able to perform more advanced tasks.


Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.

Comments

  1. This is a great comprehensive list of essential Linux commands for anyone interested in learning more about the Linux operating system. As someone who works in embedded software development, I appreciate how important it is to have a solid understanding of Linux and its commands, as it is a widely used operating system in embedded systems.

    In fact, many embedded systems rely on Linux as the underlying operating system, making it essential for developers to have a strong command over these essential Linux commands. These commands are particularly useful in debugging and troubleshooting issues in embedded systems, and can be used to optimize performance and improve reliability.

    Overall, this list of essential Linux commands is an excellent resource for anyone involved in embedded software development, and can help developers navigate the complexities of working with Linux-based systems.

    ReplyDelete

Post a Comment

Popular posts from this blog

How Tesla is Revolutionizing AI and Energy in 2025

What Role Graphic Design Services Play in Marketing

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

Top 11 Interview Blunders That Can Cost You the Job: Expert Tips to Avoid Them

17 Best Demo Websites for Automation Testing Practice

Top 10 Highly Paid Indian CEOs in the USA

Mastering Selenium Practice: Automating Web Tables with Demo Examples

Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface

Mastering Software Testing Techniques: Boundary Value Analysis to State Transition

Top Free YouTube Download Apps: Download YouTube Videos Software Free