Top 25 Basic Linux Commands for Beginners

Basic Linux Commands for Beginners
Basic Linux Commands for Beginners

Linux commands are a set of instructions used to interact with a Linux operating system. These commands can be entered through a command line interface, also known as a terminal, and are used to perform various tasks such as navigating the file system, managing files and directories, configuring system settings, and managing processes.

Linux commands are case sensitive, and the options and arguments used with each command may vary depending on the distribution of Linux being used. Some commands may require root privileges, which can be obtained by using the sudo command.

Learning the basic Linux commands is essential for anyone working with a Linux system, whether as a system administrator, developer, or casual user. In this way, users can effectively manage the system, troubleshoot issues as they arise, and perform routine maintenance tasks.

Basic Linux Commands

There are hundreds of Linux commands, each with its own set of options and arguments. The exact number of commands available may vary depending on the distribution of Linux being used and the software packages installed. You can read Hundreds of Linux Commends in a pdf book which wrote Mohidul Islam

As a Linux server beginner, it’s important to familiarize yourself with the basic commands used in the Linux terminal. Here are 25 basic Linux commands that can help you get started:

1. ls

The ls command is used to list the contents of a directory. By default, it displays the contents of the current directory. Here are some examples of how to use the ls command:

  • ls: Display the contents of the current directory.
  • ls /home/user/Documents: Display the contents of the directory /home/user/Documents.
  • ls -l: Display the contents of the current directory in a long format, which includes details such as permissions, ownership, and size.
  • ls -a: Display all files, including hidden files, in the current directory.
  • ls -lh: Display the contents of the current directory in a long format with sizes in human-readable format.
  • ls /home/user/Documents/*.txt: Display all files with a .txt extension in the directory /home/user/Documents.
  • ls -R /home/user: Display the contents of the directory /home/user recursively, including all subdirectories.

These are just a few examples of how to use the ls command. You can also combine options to get more specific results, such as ls -lha to display all files, including hidden files, in a long format with human-readable sizes.

2. pwd

The pwd command stands for “print working directory” and is used to display the current working directory in the terminal. Here are some examples of how to use the pwd command:

  • pwd: Display the current working directory.
  • cd /home/user/Documents then pwd: Change the current directory to /home/user/Documents and display the current working directory.
  • echo "The current working directory is $(pwd)": Display a message with the current working directory included.

The pwd command is useful when navigating through the file system and can help you keep track of your current location. It can also be used in scripts and other commands to reference the current directory.

3. cd

The cd command stands for “change directory” and is used to change the current working directory in the terminal. Here are some examples of how to use the cd command:

  • cd: Change the current directory to the home directory of the current user.
  • cd /home/user/Documents: Change the current directory to /home/user/Documents.
  • cd ..: Change the current directory to the parent directory of the current directory.
  • cd -: Change the current directory to the previous working directory.
  • cd ~/: Change the current directory to the home directory of the current user.

The cd command is a fundamental command used to navigate through the file system in a Linux/Unix shell. It is important to know how to use it to effectively navigate and work with files and directories on a Linux server.

4. mkdir

The mkdir command is used to create a new directory (folder) in the file system. Here are some examples of how to use the mkdir command:

  • mkdir new_dir: Create a new directory named “new_dir” in the current directory.
  • mkdir -p /home/user/Documents/new_dir: Create a new directory named “new_dir” in the directory /home/user/Documents. The -p option is used to create any missing parent directories in the path, if they do not exist.
  • mkdir dir1 dir2 dir3: Create multiple directories named “dir1”, “dir2”, and “dir3” in the current directory.

The mkdir command is useful for creating new directories when organizing files on a Linux server. The -p option can be particularly useful when creating nested directories, as it will create all parent directories that do not exist.

5. rmdir

The rmdir command is used to remove an empty directory from the file system. Here are some examples of how to use the rmdir command:

  • rmdir empty_dir: Remove the directory “empty_dir” from the current directory, but only if it is empty.
  • rmdir -p /home/user/Documents/empty_dir: Remove the directory “empty_dir” from the directory /home/user/Documents, along with any parent directories that become empty as a result of the removal. The -p option is used to remove parent directories recursively.

It is important to note that the rmdir command will only remove a directory if it is empty. If the directory contains any files or other directories, the command will fail. In that case, the rm command with the -r option can be used to remove a directory and all its contents recursively. However, be cautious when using the rm command, as it can cause irreversible data loss.

6. cp

The cp command is used to copy files or directories from one location to another. Here are some examples of how to use the cp command:

  • cp file.txt /home/user/Documents/: Copy the file “file.txt” to the directory /home/user/Documents/.
  • cp -r dir1 /home/user/Documents/: Copy the directory “dir1” and all its contents to the directory /home/user/Documents/. The -r option is used to copy directories recursively.
  • cp -i file.txt backup/: Copy the file “file.txt” to the directory “backup/”, but prompt the user for confirmation if the file already exists in the destination directory. The -i option is used to enable interactive mode.

The cp command is useful for making backups or duplicating files and directories on a Linux server. The -r option is particularly useful for copying directories recursively, while the -i option can help prevent accidental overwriting of files.

7. mv

The mv command is used to move or rename files and directories on a Linux server. Here are some examples of how to use the mv command:

  • mv file.txt /home/user/Documents/: Move the file “file.txt” to the directory /home/user/Documents/.
  • mv dir1 /home/user/Documents/: Move the directory “dir1” and all its contents to the directory /home/user/Documents/.
  • mv file.txt new_name.txt: Rename the file “file.txt” to “new_name.txt” in the current directory.

The mv command can be used to both move and rename files and directories. When moving a file or directory, the destination can be either a new location or a new name in the same location. The mv command can also be used to move or rename multiple files and directories at once by specifying multiple sources.

It is important to use caution when using the mv command, as moving or renaming files and directories can have unintended consequences if done incorrectly.

8. rm

The rm command is used to remove files or directories from the file system. Here are some examples of how to use the rm command:

  • rm file.txt: Remove the file “file.txt” from the current directory.
  • rm -r dir1: Remove the directory “dir1” and all its contents recursively. The -r option is used to enable recursive removal.
  • rm -i file.txt: Remove the file “file.txt”, but prompt the user for confirmation before actually removing the file. The -i option is used to enable interactive mode.

It is important to use caution when using the rm command, as it can cause irreversible data loss if used incorrectly. Always double-check the files and directories to be removed before executing the command, and use the -i option if you want to be prompted for confirmation before removing files.

9. cat

The cat command is used to display the contents of a file on the Linux terminal. Here are some examples of how to use the cat command:

  • cat file.txt: Display the contents of the file “file.txt” in the terminal window.
  • cat file1.txt file2.txt: Concatenate the contents of the files “file1.txt” and “file2.txt” and display them in the terminal window.
  • cat file.txt > new_file.txt: Create a new file “new_file.txt” with the contents of “file.txt”. The > symbol is used to redirect the output of the cat command to a new file.

The cat command is useful for quickly viewing the contents of a file in the terminal window, or for combining the contents of multiple files into a single output stream. The > symbol can be used to redirect the output of the cat command to a new file, allowing you to quickly create a new file with the contents of an existing file.

10. head

The head command is used to display the first few lines of a file on the Linux terminal. Here are some examples of how to use the head command:

  • head file.txt: Display the first 10 lines of the file “file.txt” in the terminal window. By default, the head command displays the first 10 lines of the file.
  • head -n 5 file.txt: Display the first 5 lines of the file “file.txt” in the terminal window. The -n option is used to specify the number of lines to display.
  • head -c 50 file.txt: Display the first 50 bytes of the file “file.txt” in the terminal window. The -c option is used to specify the number of bytes to display instead of lines.

The head command is useful for quickly previewing the contents of a file without having to open it in a text editor. By default, the head command displays the first 10 lines of a file, but this can be changed using the -n option. The -c option can also be used to display a specific number of bytes instead of lines.

11. tail

The tail command is used to display the last few lines of a file on the Linux terminal. Here are some examples of how to use the tail command:

  • tail file.txt: Display the last 10 lines of the file “file.txt” in the terminal window. By default, the tail command displays the last 10 lines of the file.
  • tail -n 5 file.txt: Display the last 5 lines of the file “file.txt” in the terminal window. The -n option is used to specify the number of lines to display.
  • tail -f file.txt: Display the last 10 lines of the file “file.txt” in the terminal window, and then continue to display any new lines that are added to the file. The -f option is used to enable “follow mode”.

The tail command is useful for monitoring log files or other files that are constantly being updated. By default, the tail command displays the last 10 lines of a file, but this can be changed using the -n option. The -f option can be used to continue displaying new lines that are added to the file in real-time.

12. grep

The grep command is used to search for a specific pattern of text in a file or group of files on the Linux terminal. Here are some examples of how to use the grep command:

  • grep "search_term" file.txt: Search for the string “search_term” in the file “file.txt” and display any lines that contain the string in the terminal window.
  • grep -r "search_term" /path/to/directory: Search for the string “search_term” in all files in the directory “/path/to/directory” and its subdirectories recursively. The -r option is used to enable recursive search.
  • ps aux | grep "process_name": Search for the process with the name “process_name” in the list of running processes. The ps command is used to list all running processes, and the | symbol is used to pipe the output to the grep command for searching.

The grep command is a powerful tool for searching for specific patterns of text in files or streams of data. The -r option is useful for searching through large directories or multiple files at once. The | symbol can be used to pipe the output of one command into the grep command for searching, allowing you to search through a wide variety of data sources.

13. chmod

The chmod command is used to change the permissions of a file or directory on the Linux terminal. Here are some examples of how to use the chmod command:

  • chmod +x script.sh: Add execute permissions to the file “script.sh” so that it can be run as a script. The +x option is used to add execute permissions.
  • chmod -w file.txt: Remove write permissions from the file “file.txt” so that it cannot be modified. The -w option is used to remove write permissions.
  • chmod 644 file.txt: Set the permissions of the file “file.txt” to “rw-r–r–“, which means that the owner has read and write permissions, and everyone else has read-only permissions. The 644 notation is used to set the permissions.

The chmod command is used to set the permissions for a file or directory. The permissions are represented by a series of letters and numbers that indicate which users have read, write, and execute permissions. The letters r, w, and x represent read, write, and execute permissions, respectively. The numbers 4, 2, and 1 are used to represent read, write, and execute permissions in binary form. For example, rwx would be represented as 111 in binary form, which is equivalent to the decimal number 7.

The chmod command can be used to set the permissions for the owner, group, and other users separately, using the notation u for the owner, g for the group, and o for other users. The + and - symbols can be used to add or remove permissions, respectively. The = symbol can be used to set the permissions to a specific value.

14. chown

The chown command in Linux is used to change the ownership of a file or directory. Ownership refers to the user and group who own a file or directory and can control access to it.

The basic syntax of the chown command is:

chown [OPTIONS] USER:GROUP FILE/DIRECTORY

Here, USER is the new owner of the file or directory, GROUP is the new group owner, and FILE/DIRECTORY is the path to the file or directory to be modified.

Some common options used with the chown command include:

  • -R: Recursively change the ownership of the directory and all its contents.
  • -v: Verbose output, prints the name of each file or directory as it is modified.

For example, to change the ownership of a file named example.txt to the user john and the group users, the following command can be used:

sudo chown john:users example.txt

Note that the chown command requires root privileges, so it must be run with sudo or as the root user. Changing ownership can be useful in situations where a user needs access to a file or directory that is currently owned by another user or group. It can also be used to set up file permissions and access control for a shared file system or web server.

15. whoami

The whoami command is used to display the username of the current user on the Linux terminal. Here’s an example of how to use the whoami command:

  • whoami: Display the username of the current user in the terminal window.

The whoami command is useful for verifying the identity of the current user when logged into a Linux system. It can be used in scripts and other command-line utilities to perform actions based on the identity of the current user.

16. su

The su command is used to switch to another user account on the Linux terminal. Here are some examples of how to use the su command:

  • su: Switch to the root user account. You will be prompted to enter the root user’s password.
  • su -l username: Switch to the user account “username” and set the environment variables for that user. The -l option is used to load the user’s environment variables.

The su command is used to gain access to another user account. It is commonly used to gain administrative privileges by switching to the root user account.

The su command can also be used to switch to other user accounts, which can be useful for testing or troubleshooting purposes. When switching to another user account, you will be prompted to enter the password for that user account. The -l option is used to load the environment variables for the user account, which can be necessary for some applications to work correctly.

17. sudo

sudo stands for “superuser do” and is a command used in Linux and Unix-based systems to allow a user to run commands with the privileges of the system’s superuser or another specified user.

When a user runs a command with sudo, they are prompted to enter their own password, and if authenticated, the command is executed with elevated privileges. This allows a user to perform system-level tasks that would otherwise be restricted.

For example, to install a package using the apt-get command in Ubuntu, a user must have root privileges. By running the command with sudo, the user can temporarily gain root privileges to install the package:

sudo apt-get install <package-name>

It is important to use sudo with caution as it provides full administrative access to the system. Improper use of sudo can cause damage to the system or compromise its security.

18. df

The df command is used to display disk space usage information on the Linux terminal. Here are some examples of how to use the df command:

  • df: Display disk space usage information for all mounted file systems in a human-readable format.
  • df -h: Display disk space usage information for all mounted file systems in a human-readable format, showing sizes in gigabytes, megabytes, or kilobytes as appropriate.
  • df -i: Display disk space usage information for all mounted file systems, showing the number of inodes used and available.
  • df /dev/sda1: Display disk space usage information for the file system mounted at “/dev/sda1” in a human-readable format.

The df command is used to display information about the amount of disk space used and available on mounted file systems. The output of the df command shows the file system, total size, used space, available space, percentage of disk space used, and mount point for each mounted file system.

The -h option is used to display sizes in a human-readable format, making it easier to understand the amount of disk space used and available. The -i option is used to display information about the number of inodes used and available on the file system. The df command can be useful for monitoring disk space usage and identifying file systems that are running out of space.

19. du

The du command in Linux is used to estimate the space used by a file or directory. The term “du” stands for “disk usage”.

The basic syntax of the du command is:

du [OPTIONS] FILE/DIRECTORY

Here, FILE/DIRECTORY is the path to the file or directory to be analyzed.

Some common options used with the du command include:

  • -h: Human-readable output, displays file sizes in a more easily readable format (e.g. 1K, 2M, 3G).
  • -s: Display only a summary of the total space used by the file or directory.
  • -c: Include a grand total of the space used for all files or directories.

For example, to display the space used by a directory named example_directory, including a grand total, the following command can be used:

du -hc example_directory
The du command can be useful in situations where a user needs to free up disk space by identifying large files or directories that can be safely deleted.

20. top

The top command is used to display information about the processes running on a Linux system in real-time. Here are some examples of how to use the top command:

  • top: Display a real-time list of processes running on the system, sorted by CPU usage.
  • top -u username: Display a real-time list of processes running on the system, sorted by CPU usage, only showing processes owned by the user “username”.
  • top -d 5: Display a real-time list of processes running on the system, sorted by CPU usage, updating the list every 5 seconds.

The top command is useful for monitoring the performance of a Linux system and identifying processes that are using a large amount of system resources. The output of the top command shows the process ID, user, CPU usage, memory usage, and other information about each running process.

The top command can be configured to sort the output by different criteria, such as CPU usage or memory usage, and can be used to filter the output to show only processes owned by a specific user. The top command can also be configured to update the output at a specific interval, making it useful for monitoring system performance over time.

21. ps

The ps command is used to display information about the processes running on a Linux system. Here are some examples of how to use the ps command:

  • ps: Display a list of processes running on the system.
  • ps -u username: Display a list of processes owned by the user “username”.
  • ps -ef: Display a detailed list of processes running on the system, including the user, process ID, parent process ID, CPU usage, and command line arguments.

The ps command is useful for identifying processes running on a Linux system and their associated metadata. The output of the ps command shows the process ID, parent process ID, user, CPU usage, memory usage, and other information about each running process.

The ps command can be configured to show different levels of detail and can be used to filter the output to show only processes owned by a specific user. The ps command can be useful for monitoring system performance, identifying processes that are using a large amount of system resources, and troubleshooting issues with specific processes.

22. kill

The kill command is used to terminate a running process on a Linux system. Here are some examples of how to use the kill command:

  • kill process_id: Terminate the process with the specified process ID.
  • kill -9 process_id: Forcefully terminate the process with the specified process ID.
  • killall process_name: Terminate all processes with the specified name.

The kill command is useful for stopping a running process that is causing problems on a Linux system. The kill command sends a signal to the specified process, causing it to terminate gracefully. The -9 option can be used to forcefully terminate a process that is not responding to a normal termination signal.

The killall command can be used to terminate all processes with a specific name, making it useful for stopping multiple instances of a problematic process. It is important to use the kill command with caution, as terminating a process can cause data loss or other unexpected behavior.

23. ifconfig

The ifconfig command is used to display information about network interfaces on a Linux system. Here are some examples of how to use the ifconfig command:

  • ifconfig: Display information about all network interfaces on the system.
  • ifconfig eth0: Display information about the network interface named “eth0”.
  • ifconfig -a: Display information about all network interfaces, including those that are currently down.

The ifconfig command is useful for troubleshooting network connectivity issues and verifying that network interfaces are configured correctly. The output of the ifconfig command shows information about each network interface, including its IP address, MAC address, and network mask.

The ifconfig command can be used to configure network interfaces, such as setting a static IP address or configuring a network interface to use DHCP. Note that the ifconfig command has been deprecated on some Linux distributions in favor of the ip command, so it may not be available on all systems.

24. ping

The ping command is used to test the connectivity between two network devices by sending ICMP echo requests and receiving ICMP echo replies. Here are some examples of how to use the ping command:

  • ping hostname: Send ICMP echo requests to the specified hostname or IP address.
  • ping -c count hostname: Send a specified number of ICMP echo requests to the specified hostname or IP address.
  • ping -s size hostname: Send ICMP echo requests with a specified packet size to the specified hostname or IP address.
  • ping -i interval hostname: Send ICMP echo requests with a specified interval between packets to the specified hostname or IP address.

The ping command is useful for troubleshooting network connectivity issues and verifying that network devices are reachable. The output of the ping command shows information about the ICMP echo requests and replies, including the round-trip time, number of packets sent and received, and any errors encountered.

The ping command can be used to test connectivity between different devices on a local network or between devices on different networks. Note that some network devices may be configured to block ICMP echo requests, so the ping command may not always be successful in testing connectivity.

25. ssh

The ssh command is used to establish a secure shell (SSH) connection to a remote server or device. Here are some examples of how to use the ssh command:

  • ssh user@hostname: Connect to a remote server with the specified username and hostname.
  • ssh -p port user@hostname: Connect to a remote server using a specified port number.
  • ssh -i keyfile user@hostname: Connect to a remote server using a specified private key file.
  • ssh -X user@hostname: Connect to a remote server with X11 forwarding enabled.

The ssh command is useful for securely accessing a remote server or device and executing commands or transferring files. The SSH protocol encrypts all traffic between the local and remote devices, making it a secure method for remote access.

The ssh command can also be used to create a secure tunnel for forwarding traffic between the local and remote devices, such as for accessing a remote database server or web server. Note that the ssh command requires that the remote device has an SSH server running and is configured to allow SSH connections.

Know More: Linux Vs. Windows: Which Is Perfect for Your Web Site?

In Summary

Linux server commands are essential for managing and maintaining a Linux system. They enable users to perform various tasks such as navigating the file system, creating and modifying files and directories, managing processes, and configuring network interfaces.

While there are many Linux server commands available, the ones covered in this conversation are some of the most frequently used and fundamental ones. Learning these commands and how to use them effectively is a critical skill for anyone working with Linux systems, whether as a system administrator or a developer.

With practice and experience, users can become proficient in using these basic Linux commands to efficiently manage Linux servers and troubleshoot issues as they arise.