Essential Linux Commands

Spread the love

Directory Commands list with explanation

  1. ls: This command lists all files and directories in the current directory.

    • Explanation: The ls command has various options like -a (list all files including hidden), -l (long listing format), -h (human-readable file sizes), and more.
    • Example: ls -la (This will display all files including hidden ones and their details like permissions, number of links, owner, group, size, and time of last modification).
  2. pwd: Stands for “Print Working Directory”. This command is used to display the path of the current directory you are in.

    • Example: pwd (This will display the full path to the current directory).
  3. cd: Stands for “Change Directory”. This command is used to change the current directory.

    • Example: cd Documents (This will change the current directory to the “Documents” directory).
  4. mkdir: Stands for “Make Directory”. This command is used to create a new directory.

    • Example: mkdir new_directory (This will create a new directory called “new_directory”).
  5. rmdir: Stands for “Remove Directory”. This command is used to delete an empty directory.

    • Example: rmdir empty_directory (This will remove an empty directory named “empty_directory”).
  6. rm -r: Removes a directory and its contents.

    • Explanation: Be careful with this command as it deletes directories and files permanently.
    • Example: rm -r directory (This will remove a directory named “directory” and its contents).
  7. cp -r: Copies a directory.

    • Example: cp -r dir1 dir2 (This will copy a directory named “dir1” to a new directory named “dir2”).
  8. mv: Moves or renames directories.

    • Example: mv dir1 dir2 (This will move a directory named “dir1” into another directory named “dir2”).
    • Example: mv dir1 dir3 (This will rename “dir1” to “dir3”).

File Commands list with explanation

  1. cat: Concatenates and displays files.

    • Explanation: This command is used to display the content of files. It can also be used to create a new file or concatenate files.
    • Example: cat file.txt (This will display the content of “file.txt”).
  2. less: Views file content page by page.

    • Explanation: This command is used to view the content of files page by page, which is especially useful when dealing with large files.
    • Example: less largefile.txt (This will open the largefile.txt in a paging format).
  3. head: Displays the beginning of a file.

    • Explanation: This command displays the beginning of a file. By default, it shows the first 10 lines.
    • Example: head -n 20 file.txt (This will display the first 20 lines of “file.txt”).
  4. tail: Displays the end of a file.

    • Explanation: The opposite of head, tail displays the end of a file. It’s often used with the -f option to monitor the addition of new lines to a file.
    • Example: tail -f logfile.log (This will display the new lines being added to “logfile.log” in real time).
  5. touch: Creates a new file.

    • Explanation: This command is used to create a new empty file.
    • Example: touch newfile.txt (This will create a new file named “newfile.txt”).
  6. cp: Copies files or directories.

    • Explanation: This command is used to copy files or directories from one location to another.
    • Example: cp source.txt destination.txt (This will copy the contents of “source.txt” to “destination.txt”).
  7. mv: Moves or renames files.

    • Explanation: This command is used for moving or renaming files and directories from one location to another.
    • Example: mv oldname.txt newname.txt (This will rename the file “oldname.txt” to “newname.txt”).
  8. rm: Removes files or directories.

    • Explanation: This command is used to remove files or directories. Be careful when using this command, as it permanently deletes files.
    • Example: rm file.txt (This will remove “file.txt”).
  9. find: Searches for files in a directory hierarchy.

    • Explanation: This command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.
    • Example: find /home -name myfile.txt (This will search for a file named “myfile.txt” in the “/home” directory and its subdirectories).
  10. grep: Searches for a text pattern within files.

    • Explanation: This command is used to search inside files. With it, you can search for lines that contain certain strings within files.
    • Example: grep 'error' logfile.log (This will display lines that contain the string ‘error’ in the “logfile.log”).
  11. chmod: Changes file permissions.

    • Explanation: This command is used to change the permissions of a file or directory.
    • Example: chmod 755 script.sh (This will set the permission of “script.sh” to 755, which means the owner can read, write, and execute, and others can read and execute).
  12. chown: Changes file ownership.

    • Explanation: This command is used to change the ownership of a file or directory.
    • Example: chown username:groupname file.txt (This will change the ownership of “file.txt” to the user “username” and the group “groupname”).

File Content Commands list with explanation

  1. cat: Displays the content of a file.

    • Explanation: cat is a standard Unix utility that concatenates and lists files. It can be used to show the contents of a file, concatenate the contents of multiple files, or even create a new file.
    • Example: cat file.txt (This will display the content of “file.txt”).
  2. less: Allows you to peruse through the contents of a file page by page.

    • Explanation: less is used to view the content of files in a page by page manner, especially useful when dealing with large files. It also supports navigating forward and backward.
    • Example: less file.txt (This will allow you to scroll through the file “file.txt” page by page).
  3. more: Similar to less, allows file viewing page by page.

    • Explanation: more is a command used to view the contents of a file in a page by page manner. The difference is that more only allows forward navigation.
    • Example: more file.txt (This will display the content of “file.txt” page by page).
  4. head: Displays the beginning of a file.

    • Explanation: head command is used to display the first part of the files. By default, it prints the first 10 lines of each file to standard output.
    • Example: head -n 5 file.txt (This will display the first 5 lines of “file.txt”).
  5. tail: Displays the end of a file.

    • Explanation: tail command is used to display the last part of the files. By default, it prints the last 10 lines of each file to standard output. With -f option, it will keep the file open and display appended data in real time.
    • Example: tail -n 5 file.txt (This will display the last 5 lines of “file.txt”).
  6. grep: Searches for a specific pattern within a file or files.

    • Explanation: grep is used to search for a specific pattern in one or multiple files. It outputs the lines containing the pattern.
    • Example: grep 'search_term' file.txt (This will display lines in “file.txt” containing ‘search_term’).
  7. sed: Stream editor for filtering and transforming text.

    • Explanation: sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language. It can perform text substitutions, deletions, insertions and more.
    • Example: sed 's/test/TEST/' file.txt (This will replace the first occurrence of ‘test’ with ‘TEST’ in each line of “file.txt”).
  8. awk: Powerful text-processing language.

    • Explanation: awk is a powerful scripting language that’s excellent at processing structured text/data. It can be used to manipulate data and generate reports.
    • Example: awk '{print $1}' file.txt (This will print the first field (column) from each line in “file.txt”).

User Commands list with explanation

  1. whoami: Displays the username of the current user.

    • Explanation: This command is used to print the username associated with the current effective user ID.
    • Example: whoami (This will display the username of the current user).
  2. id: Displays the user and group IDs for a user.

    • Explanation: This command is used to print the real and effective user ID and group IDs.
    • Example: id username (This will display the user and group IDs of “username”).
  3. su: Switches the current user to another user.

    • Explanation: This command is used to change the current user ID or become superuser during a login session.
    • Example: su username (This will switch the current user to “username”).
  4. sudo: Executes a command as another user.

    • Explanation: This command is used to run programs with the security privileges of another user (by default, as the superuser). It stands for “superuser do”.
    • Example: sudo apt-get update (This will run the “apt-get update” command with root privileges).
  5. useradd: Adds a new user.

    • Explanation: This command is used to create a new user or update default new user information.
    • Example: sudo useradd newusername (This will add a new user named “newusername”).
  6. usermod: Modifies a user account.

    • Explanation: This command is used to modify or change any attributes of a already created user account via command line.
    • Example: sudo usermod -aG sudo username (This will add the user “username” to the “sudo” group).
  7. userdel: Deletes a user account.

    • Explanation: This command is used to delete a user account and related files.
    • Example: sudo userdel username (This will delete the user account “username”).
  8. passwd: Changes the password for a user.

    • Explanation: This command is used to change the password of a user account. A normal user can only change the password for their own account, but the superuser can change the password for any account.
    • Example: passwd (This will prompt you to enter a new password for the current user).
  9. chown: Changes the owner of a file/directory.

    • Explanation: This command is used to change the user and/or group ownership of a given file, directory, or symbolic link.
    • Example: sudo chown username:groupname file.txt (This will change the owner of “file.txt” to “username” and the group to “groupname”).
  10. groups: Shows the groups a user is a member of.

    • Explanation: This command is used to show the groups a user is a member of.
    • Example: groups username (This will show the groups that “username” is a part of).

Filter Commands list with explanation

  1. grep: Searches for a pattern in files or input, returning lines that contain the pattern.

    • Explanation: The grep command is one of the most frequently used UNIX command stands for ‘Global Regular Expression Print’. It searches the given file for lines containing a match to the given strings or words.
    • Example: grep "error" logfile.txt (This will print all lines in “logfile.txt” that contain the word “error”).
  2. awk: Scans and processes text line by line and field by field.

    • Explanation: awk is a powerful command for complex text processing. It scans text line by line, splitting each line into fields, and processes it according to provided script.
    • Example: awk '{print $2}' file.txt (This will print the second field of every line in “file.txt”).
  3. sed: Stream editor for filtering and transforming text.

    • Explanation: sed is a stream editor that is used to perform basic text transformations on an input stream (a file or input from a pipeline).
    • Example: sed 's/foo/bar/' file.txt (This will replace the first occurrence of “foo” with “bar” in each line of “file.txt”).
  4. sort: Sorts the lines in a text file.

    • Explanation: The sort command is used to sort lines in text files in a particular order.
    • Example: sort file.txt (This will sort the lines in “file.txt” in ascending order).
  5. uniq: Reports or omits repeated lines.

    • Explanation: The uniq command is used to filter out the repeated lines in a file. It is helpful for removing duplicate entries from a file.
    • Example: sort file.txt | uniq (This will remove duplicate lines in the sorted “file.txt”).
  6. cut: Removes sections from each line of files.

    • Explanation: The cut command is used to remove or “cut out” certain sections of each line in a file.
    • Example: cut -d':' -f1 /etc/passwd (This will display the usernames in the system, cutting at the delimiter “:” and displaying the first field).
  7. wc: Prints newline, word, and byte counts for files.

    • Explanation: The wc (word count) command in Unix/Linux is used to find out number of newline count, word count, byte and characters count in a files specified by the file arguments.
    • Example: wc -l file.txt (This will display the number of lines in “file.txt”).
  8. head: Outputs the first part of files.

    • Explanation: The head command, by default, shows the first 10 lines of each file name that is provided to it.
    • Example: head -n 5 file.txt (This will display the first 5 lines of “file.txt”).
  9. tail: Outputs the last part of files.

    • Explanation: The tail command, by default, shows the last 10 lines of each file name that is provided to it.
    • Example: tail -n 5 file.txt (This will display the last 5 lines of “file.txt”).
  10. tee: Reads from standard input and writes to standard output and files.

Utility Commands list with explanation

  1. ps: Lists the currently running processes.

    • Explanation: ps stands for process status. It provides information about the currently running processes, including their process identification numbers (PIDs).
    • Example: ps aux (This will provide a snapshot of all current processes).
  2. top: Provides a real-time, dynamic view of the processes running in a system.

    • Explanation: top command is used to get a dynamic real-time view of the running system. It provides a live, scrollable list of processes sorted by how much CPU they are using.
    • Example: top (This will display a real-time view of system processes).
  3. df: Reports the amount of disk space used on a file system.

    • Explanation: df stands for “disk filesystem”. It reports file system disk space usage.
    • Example: df -h (This will display disk usage in a human-readable format).
  4. du: Estimates the disk usage of files and directories.

    • Explanation: du stands for “disk usage”. This command is used to estimate and report the disk space used by files and directories.
    • Example: du -sh /home/user (This will display a summary, in human-readable format, of total disk space used within the specified directory).
  5. free: Shows the amount of free and used memory in the system.

    • Explanation: free command is used to check the amount of free, used, swap memory in the system, along with the buffers used by the kernel.
    • Example: free -m (This will display the amount of free and used memory in megabytes).
  6. uptime: Shows the current time, how long the system has been running, how many users are currently logged on, and the system load averages.

    • Explanation: uptime command gives a one-line display of the following information: The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.
    • Example: uptime (This will display the current uptime).
  7. history: Shows the command history.

    • Explanation: history command is used to view the previously executed command.
    • Example: history (This will display your command history).
  8. clear: Clears the terminal screen.

    • Explanation: clear command is used to clear the terminal screen. It doesn’t take any argument.
    • Example: clear (This will clear the terminal screen).
  9. date: Displays or sets the system date and time.

    • Explanation: date command is used to display the system date and time. date also can be used to set the date and time.
    • Example: date (This will display the current date and time).
  10. man: Displays the manual pages.

    • Explanation: man stands for manual. This command is used to display the user manual of any command that we can run on the terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS and SEE ALSO.
    • Example: man ls (This will display the manual pages for the ls command).
  1. echo: Outputs the strings it is being passed as arguments.

    • Explanation: echo is a command in Linux that outputs its arguments.
    • Example: echo "Hello, World!" (This will print “Hello, World!” to the terminal).
  2. which: Shows the full path of shell commands.

    • Explanation: which returns the pathnames of the files (or links) which would be executed in the current environment.
    • Example: which ls (This will show the full path of the ls command).
  3. clear: Clears the terminal screen.

    • Explanation: clear is used to clear your terminal if it gets filled up with too many previous commands.
    • Example: Just type clear and press Enter to clear your terminal.
  4. history: Shows the command history.

    • Explanation: history command is used to view the previously executed commands.
    • Example: Just type history and press Enter to view your command history.
  5. date: Displays the current date and time.

    • Explanation: date command is used to display the system date and time. date command is also used to set date and time of the system.
    • Example: Just type date and press Enter to view the current date and time.
  6. df: Reports the amount of available disk space.

    • Explanation: df stands for “disk filesystem”, it is used to get full summary of available and used disk space usage of the file system on Linux system.
    • Example: df -h (This will display disk space usage in human-readable format).
  7. du: Estimates file and directory space usage.

    • Explanation: du stands for “disk usage”, it is used to estimate and report file and directory space usage.
    • Example: du -sh /home/user (This will summarize the disk usage of the /home/user directory in human-readable format).
  8. free: Displays the amount of free and used memory in the system.

    • Explanation: free command is used to check the amount of free, used, swap memory in KB.
    • Example: free -h (This will display memory details in human-readable format).
  9. top: Provides a dynamic real-time view of the running system.

    • Explanation: top command is used to check the system process and load average on the system.
    • Example: Just type top and press Enter to view the real-time view of the running system.

Networking Command list with explanation

  1. ping: Sends ICMP ECHO_REQUEST packets to network hosts.

    • Explanation: ping command is used to check the network connectivity between the host that sends the ping and the destination host.
    • Example: ping google.com (This will ping google.com and report back the response time).
  2. ifconfig: Displays or configures a network interface.

    • Explanation: ifconfig (interface configurator) command is used to initialize an interface, assign IP Address to interface and enable or disable interface on demand.
    • Example: ifconfig (Running this without any options will display a list of network interfaces and their configuration).
  3. netstat: Network statistics. Shows network connections, routing tables, interface statistics, etc.

    • Explanation: netstat (network statistics) is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and a number of network interface statistics.
    • Example: netstat -tuln (This will display only listening sockets with numerical addresses).
  4. ssh: Secure shell remote login protocol.

    • Explanation: ssh (Secure Shell) is a protocol to log into another computer over a network, to execute commands in a remote machine, and to move files from one machine to another. It provides strong authentication and secure communications over unsecured channels.
    • Example: ssh user@remotehost (This will start a shell session on the remote host as user “user”).
  5. telnet: User interface to the TELNET protocol.

    • Explanation: telnet is a user command and an underlying TCP/IP protocol for accessing remote computers.
    • Example: telnet remotehost 23 (This will open a telnet connection to “remotehost” on port 23).
  6. traceroute: Print the route packets take to network host.

    • Explanation: traceroute is a network diagnostic tool for displaying the route (path) and measuring transit delays of packets across an Internet Protocol (IP) network.
    • Example: traceroute google.com (This will show the route that packets take to get to google.com from your machine).
  7. nslookup: Query Internet name servers interactively.

    • Explanation: nslookup is a network administration command-line tool available for querying the Domain Name System (DNS) to obtain domain name or IP address mapping, or other DNS records.
    • Example: nslookup google.com (This will display DNS information for google.com).
  8. dig: DNS lookup utility.

    • Explanation: dig (domain information groper) is a network administration command-line tool for querying DNS name servers.
    • Example: dig google.com (This will perform a DNS lookup for google.com).
  9. wget: Network downloader.

    • Explanation: wget is a free utility for non-interactive download of files from the web. It supports HTTP, HTTPS, and FTP protocols.
    • Example: wget https://example.com/file.txt (This will download the file “file.txt” from the specified URL).
  10. curl: Transfers data from or to a server.

    • Explanation: curl is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, IMAP, POP3, etc).
    • Example: curl https://example.com (This will fetch the content of the web page at the specified URL).