23 Must-Know Linux Commands for Beginners(in 2021)
Published: 2021-10-07 19:37:33

<h1>23 Must-Know Linux Commands for Beginners(in 2021)</h1><img src="https://uploads-ssl.webflow.com/60d5a97381523a05b73c87c6/6128c0497ecd4b13867bf9f0_30.png"/><p>Linux is an entire family of open-source Unix operating systems that are based on the Linux Kernel. This includes all the most popular Linux-based systems like Ubuntu, Fedora, Mint, Debian, etc. More accurately, they're called distributions or distros. Linux seems a bit hard to use and understand for people coming from Windows or macOS, and most people give up on using Linux because they are unaware of the commands and shortcuts that Linux provides. Knowing the useful commands and their proper usage can significantly speed up your work on Linux than if you were using other operating systems. &nbsp; </p><p>In this article, we will look at some frequently used Linux commands. &nbsp; </p><p><strong> &nbsp; </strong> </p><h4><strong>1. ls command &nbsp;</strong> </h4><p>The ls is the list command in Linux. It will show the complete list or content of your directory. You can pair it with several options to get different results. Ls with no option list files and directories in a raw format where we won't be able to view details like file types, size, modified date and time, permission and links, etc. Some of the most common options are listed below: &nbsp; </p><ul><li>ls -l: shows file or directory, size, modified date and time, file or folder name and owner of the file, and its permission. &nbsp; </li><li>ls -a: List all files, including hidden files starting with '. '. &nbsp; </li><li>ls -lh: List files in human-readable format. &nbsp; </li><li>ls -r: List files in reverse order. &nbsp; </li><li>ls -R: Recursively list sub-directories. &nbsp;</li></ul><h4><strong>2. cd command &nbsp;</strong> </h4><p>cd stands for change directory. You can use the cd command to navigate through Linux files and directories. It requires either the full path or the directory's name, depending on the current working directory that you're in. &nbsp; </p><p>If you want to go to a subdirectory of the directory you are in, type cd followed by the directory name. &nbsp; </p><p>If you want to go to a directory outside the current directory, for example, from /home/user/downloads to /home/user/photos, you have to give the absolute path of the directory like 'cd /home/user/photos. There are also some shortcuts to navigate quickly: &nbsp; </p><ul><li>cd .. to move one directory up. &nbsp; </li><li>cd to go straight to the home folder. &nbsp; </li><li>cd- to move to your previous directory &nbsp; </li></ul><h4><strong>3. PWD command &nbsp;</strong> </h4><p>PWD command is used to find the path of the current directory. It will return the absolute path, which contains all the directories from the root that starts with a forward slash. An example of an absolute path is /home/user. &nbsp; <strong><br></strong></p><h4><strong>4. rmdir command </strong> &nbsp;</h4><p>rmdir command allows you to delete empty directories. &nbsp;</p><h4><strong>5. rm command &nbsp;</strong> </h4><p>rm command allows you to delete directories with contents inside them. The options that can be used with rm are: &nbsp; </p><ul><li>rm -i: will ask before deleting each file. &nbsp; </li><li>rm -r: will recursively delete a directory and all of its contents. &nbsp; </li><li>rm -f: will forcibly delete files without asking. &nbsp; </li></ul><h4><strong>6. touch command </strong> </h4><p>touch command allows you to create a new blank file with the given file name. &nbsp; &nbsp;</p><p>Usage: &nbsp; &nbsp;</p><ul><li>touch hello.js will create a file called hello with the extension .js in your current directory. &nbsp; </li><li>touch home/user/documents/hello.html will create the given HTML file in the Documents folder. &nbsp; &nbsp;</li></ul><h4><strong>7. cp command </strong> &nbsp;</h4><p>cp command lets you copy files from your current directory to your desired directory. &nbsp; </p><p>Usage: &nbsp; </p><ul><li>cp hello.js /home/user/documents will copy the hello.js from the current directory to the documents folder. &nbsp; </li></ul><h4><strong>8. mv command &nbsp;</strong> </h4><p>mv command is used to move files the same way the cp command is used to copy files. But it can also be used to rename files. &nbsp; </p><p>Usage: &nbsp; </p><ul><li>To move files: mv hello.js /home/user/documents will move the hello.js from the current directory to the documents folder. &nbsp; </li><li>To rename files, type: mv oldname.ext newname.ext &nbsp; &nbsp;</li></ul><h4><strong>9. cat command </strong> &nbsp;</h4><p>cat is one of the most frequently used commands in Linux. Cat is short for concatenate. Cat is used to concatenate and print the contents of files. Cat command allows us to create single or multiple files, view contents of a file, concatenate files, and redirect output in terminal or files. &nbsp; </p><p>Usage: &nbsp; </p><ul><li>Display contents of a file &nbsp; </li></ul><p>cat /etc/passwd: will show the contents of passwd. &nbsp; </p><ul><li>View contents of multiple files in terminal &nbsp; </li></ul><p>cat test test1: will show the contents of the test and test1 in the terminal. &nbsp; </p><ul><li>Create a file &nbsp; </li></ul><p>cat &gt;test2: will create a file test2. It then awaits input from the user. Write in the desired text and press CTRL+D to exit. &nbsp; </p><ul><li>cat with a redirection operator &nbsp; </li></ul><p>cat test &gt; test1: We can redirect the standard output into a new file else existing file with '&gt; '. Existing contents of the test1 will be overwritten by the contents of the test file. &nbsp; </p><ul><li>Appending standard output with a redirection operator &nbsp; </li></ul><p>cat test &gt;&gt; test1: Appends an existing file with '&gt;&gt; '. Here, the contents of the test file will be appended at the end of test1 file. &nbsp; </p><h4><strong>10. locate command &nbsp;</strong> </h4><p>locate command in Linux is used to find the files by name. An alternative to locating is find. But, the difference between both commands is that locate command is a background process and searches the file in the database whereas find command searches in the filesystem. The locate command is much faster than the find command. &nbsp; </p><p>If you cannot find a file with locate command, then it means that your database is out of date, and you can update it with the "updatedb" command. &nbsp; </p><p>Syntax: locate [OPTION]... PATTERN... &nbsp; </p><p>Usage: &nbsp; </p><ul><li>locate sample.txt: will search for sample.txt. &nbsp; </li><li>locate “*.html” -n 20: will show 20 results for the searching of file ending with .html. &nbsp; </li><li> locate -c [.txt]*: will count files ending with .txt and display the result. &nbsp; </li><li>locate -i *SAMPLE.txt*: Ignore Case Sensitive Locate Outputs. This command is configured to process queries in a case-sensitive manner. It means SAMPLE.TXT will show a different result than sample.txt. &nbsp; <br></li></ul><h4><strong>11. find command &nbsp;</strong> </h4><p>Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory. &nbsp; </p><ul><li>To find files in the current directory use, find . -name notes.txt &nbsp; </li><li>To look for directories use, / -type d -name notes.txt &nbsp; </li></ul><h4><strong>12. grep command &nbsp;</strong> </h4><p>The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. &nbsp; &nbsp;</p><p>Syntax: grep [options] pattern [files] &nbsp; </p><p>Usage: &nbsp; </p><ul><li>grep -i "Unix" file.txt: Performs a case-insensitive search in the given file and returns the lines that contain the word Unix. &nbsp; </li><li>grep -c "Unix" file.txt: Display the number of lines that match the word Unix. &nbsp; </li><li>grep -w "Unix" file.txt: By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words. &nbsp; &nbsp;</li><li>grep -n "Unix" file.txt: show the line number of the file with the line matched. &nbsp; &nbsp;</li><li>grep "^unix" file.txt: Matches the lines that start with the given string. &nbsp; </li><li>grep "os$" file.txt: Matches the lines that end with the given string. &nbsp; </li></ul><h4><strong>13. sudo command &nbsp;</strong> </h4><p>sudo stands for "superuser do!". If you prefix "sudo" with any Linux command, it will run that command with elevated privileges. Elevated privileges are required to perform specific administrative tasks. &nbsp; </p><h4><strong>14. df command &nbsp;</strong> </h4><p>df command is used to get a report on the system's disk space usage. It is shown in percentage and in KBs. To get the report in MBs, type df -m. &nbsp; <br></p><h4><strong>15. du command &nbsp;</strong> </h4><p>the du command is used to find the disk usage of a file or a directory. The disk usage summary is shown in disk block numbers instead of KBs. &nbsp; </p><p>To see it in bytes, KBs, and MBs, add the -h argument to the command. &nbsp; <br></p><h4><strong>16. echo command &nbsp;</strong> </h4><p>echo is a command that outputs the strings it is being passed as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline. &nbsp; &nbsp;</p><p>Syntax: echo [option] [string] &nbsp; </p><p>Some options of echo command are mentioned below: &nbsp; </p><ul><li>Input a line of text and display on standard output: &nbsp; </li></ul><p> echo Hello User &nbsp; </p><ul><li>echo value of a variable: &nbsp; </li></ul><p>x=5 &nbsp; </p><p>echo The value of x is $x &nbsp; </p><ul><li>Using option' \b '— backspace with backslash interpreter '-e 'removes all the spaces between. &nbsp; </li></ul><p>echo -e “This \bis \ba \btest \bmessage” &nbsp; </p><p>Output: Thisisatestmessage &nbsp; </p><ul><li>Option' \t '— horizontal tab with backspace interpreter '-e 'has horizontal tab spaces. You can use \n or a combination of \n and \t in the same way where \n inserts a new line. &nbsp; </li></ul><p> echo -e “This \tis \ta \ttest \tmessage” &nbsp; </p><p>Output: This is a test message. &nbsp; </p><h4><strong>17. zip command &nbsp;</strong> </h4><p>ZIP is used to compress files to reduce file size and is also used as a file package utility. The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). &nbsp; </p><p>Syntax: zip [options] zipfile files list &nbsp; </p><p>Usage: &nbsp; </p><ul><li>Creating a zip file: &nbsp; </li></ul><p>zip myfile.zip filename.txt &nbsp; </p><ul><li>-d option removes the file from the zip archive. &nbsp; </li></ul><p>zip –d filename.zip file.txt &nbsp; </p><ul><li>-u option is used to update the specified list of files or add new files to the existing zip file. &nbsp; </li></ul><p>zip –u filename.zip file.txt &nbsp; </p><ul><li>-r option is used to recursively zip the files in a directory. &nbsp; </li></ul><p>zip –r filename.zip directory_name &nbsp; </p><ul><li>-x option is used to exclude the specified files while creating the zip. &nbsp; </li></ul><p>zip –x filename.zip file_to_be_excluded &nbsp; </p><h4><strong>18. help command</strong> &nbsp; </h4><p>&lt;command-name&gt; --help lists all the available commands in the terminal. You can use '-h' or '–help' (help has two hyphens here) option with any command to get help for that specific command. &nbsp;</p><h4><strong>19. whatis command &nbsp;</strong> </h4><p>whatis &lt;command-name&gt; shows a single-line description for the given command. &nbsp; <br></p><h4><strong>20. man command &nbsp;</strong> </h4><p>man &lt;command-name&gt; shows the manual page for the given command. &nbsp; <br></p><h4><strong>21. tar command &nbsp;</strong> </h4><p>"tar" stands for tape archive, which is used by many Linux/Unix system administrators to deal with tape drives backup. The tar is the most widely used command to create compressed archive files that can be moved easily from one disk to another or machine to machine. &nbsp; </p><p>Syntax: &nbsp; </p><p>tar [options] [archive-file] [file or directory to be archived] &nbsp; </p><p>Options: &nbsp; &nbsp;</p><p>-c : Creates Archive &nbsp; &nbsp;</p><p>-x : Extract the archive &nbsp; &nbsp;</p><p>-f : creates archive with given filename &nbsp; &nbsp;</p><p>-t : displays or lists files in archived file &nbsp; &nbsp;</p><p>-u : archives and adds to an existing archive file &nbsp; &nbsp;</p><p>-v : Displays Verbose Information &nbsp; &nbsp;</p><p>-A : Concatenates the archive files &nbsp; &nbsp;</p><p>-z : zip, tells tar command that creates tar file using gzip &nbsp; &nbsp;</p><p>-j : filter archive tar file using tbzip &nbsp; &nbsp;</p><p>-W : Verify an archive file &nbsp; &nbsp;</p><p>-r : update or add file or directory in already existed .tar file &nbsp; &nbsp;</p><p>Usage: &nbsp; </p><ul><li>Creating an uncompressed tar Archive using option -cvf: &nbsp; </li></ul><p>tar cvf file.tar *.c — This will create a tar file called file.tar, the Archive of all .c files in the current directory. &nbsp; &nbsp;</p><ul><li>Extracting files from Archive using option -xvf : &nbsp; </li></ul><p>tar xvf file.tar &nbsp; &nbsp;</p><ul><li>gzip compression on the tar Archive, using option -z: &nbsp; </li></ul><p>tar cvzf file.tar.gz *.c — This command creates a tar file called file.tar.gz, the Archive of .c files. &nbsp; &nbsp; </p><ul><li>Extracting a gzip tar Archive *.tar.gz using option -xvzf: &nbsp; </li></ul><p>tar xvzf file.tar.gz &nbsp; &nbsp;</p><ul><li>Creating compressed tar archive file in Linux using option -j: &nbsp; </li></ul><p>tar cvfj file.tar.tbz example.CPP — This command compresses and creates archive files less than the gzip size, but it takes more time to compress and decompress. &nbsp; </p><ul><li>Applying pipe to 'grep command' to find what we are looking for: &nbsp; </li></ul><p>tar tvf file.tar | grep “text to find” &nbsp; </p><p>Or &nbsp; </p><p>tar tvf file.tar | grep “filename.file extension” &nbsp; <strong><br></strong></p><h4><strong>22. gzip command &nbsp;</strong> </h4><p>gzip &lt;filename&gt; creates and extracts gzip archives. You can use its gzip -d &lt;filename&gt; to extract gzip archives. &nbsp; <br></p><h4><strong>23. unzip command &nbsp;</strong> </h4><p>unzip &lt;archive-to-extract.zip&gt; unzips a given zip archive. You can use unzip -l &lt;archive-to-extract.zip&gt; to view the zip file's contents without extracting it. It's the same as you using an archive program to extract zip archives in GUI. &nbsp; <br>‍</p>