DEV Community

RaftLabs - AI App Dev Agency
RaftLabs - AI App Dev Agency

Posted on • Originally published at raftlabs.co

23 Must-Know Linux Commands for Beginners(in 2021)

Alt Text
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.

In this article, we will look at some frequently used Linux commands.

1. ls command

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:

ls -l: shows file or directory, size, modified date and time, file or folder name and owner of the file, and its permission.

ls -a: List all files, including hidden files starting with '. '.

ls -lh: List files in human-readable format.

ls -r: List files in reverse order.

ls -R: Recursively list sub-directories.

2. cd command

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.

If you want to go to a subdirectory of the directory you are in, type cd followed by the directory name.

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:

cd .. to move one directory up.

cd to go straight to the home folder.

cd- to move to your previous directory

3. PWD command

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.

4. rmdir command

rmdir command allows you to delete empty directories.

5. rm command

rm command allows you to delete directories with contents inside them. The options that can be used with rm are:

rm -i: will ask before deleting each file.

rm -r: will recursively delete a directory and all of its contents.

rm -f: will forcibly delete files without asking.

6. touch command

touch command allows you to create a new blank file with the given file name.

Usage:

touch hello.js will create a file called hello with the extension .js in your current directory.

touch home/user/documents/hello.html will create the given HTML file in the Documents folder.

7. cp command

cp command lets you copy files from your current directory to your desired directory.

Usage:

cp hello.js /home/user/documents will copy the hello.js from the current directory to the documents folder.

8. mv command

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.

Usage:

To move files: mv hello.js /home/user/documents will move the hello.js from the current directory to the documents folder.

To rename files, type: mv oldname.ext newname.ext

9. cat command

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.

Usage:

Display contents of a file

cat /etc/passwd: will show the contents of passwd.

View contents of multiple files in terminal

cat test test1: will show the contents of the test and test1 in the terminal.

Create a file

cat >test2: will create a file test2. It then awaits input from the user. Write in the desired text and press CTRL+D to exit.

cat with a redirection operator

cat test > test1: We can redirect the standard output into a new file else existing file with '> '. Existing contents of the test1 will be overwritten by the contents of the test file.

Appending standard output with a redirection operator

cat test >> test1: Appends an existing file with '>> '. Here, the contents of the test file will be appended at the end of test1 file.

10. locate command

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 file system. The locate command is much faster than the find command.

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.

Syntax: locate [OPTION]... PATTERN...

Usage:

locate sample.txt: will search for sample.txt.

locate "*.html" -n 20: will show 20 results for the searching of file ending with .html.

locate -c [.txt]*: will count files ending with .txt and display the result.

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.

11. find command

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.

  • To find files in the current directory use, find . -name notes.txt
  • To look for directories use, / -type d -name notes.txt

12. grep command

The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern.

Syntax: grep [options] pattern [files]

Usage:

grep -i "Unix" file.txt: Performs a case-insensitive search in the given file and returns the lines that contain the word Unix.

grep -c "Unix" file.txt: Display the number of lines that match the word Unix.

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.

grep -n "Unix" file.txt: show the line number of the file with the line matched.

grep "^unix" file.txt: Matches the lines that start with the given string.

grep "os$" file.txt: Matches the lines that end with the given string.

13. sudo command

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.

14. df command

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.

15. du command

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.

To see it in bytes, KBs, and MBs, add the -h argument to the command.

16. echo command

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.

Syntax: echo [option] [string]

Some options of echo command are mentioned below:

  • Input a line of text and display on standard output:

echo Hello User

  • echo value of a variable:

x=5

echo The value of x is $x

  • Using option' \b ' --- backspace with backslash interpreter '-e 'removes all the spaces between.

echo -e "This \bis \ba \btest \bmessage"

Output: Thisisatestmessage

  • 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.

echo -e "This \tis \ta \ttest \tmessage"

Output: This is a test message

17. zip command

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).

Syntax: zip [options] zipfile files list

Usage:

Creating a zip file:

zip myfile.zip filename.txt

-d option removes the file from the zip archive.

zip --d filename.zip file.txt

-u option is used to update the specified list of files or add new files to the existing zip file.

zip --u filename.zip file.txt

-r option is used to recursively zip the files in a directory.

zip --r filename.zip directory_name

-x option is used to exclude the specified files while creating the zip.

zip --x filename.zip file_to_be_excluded

18. help command

<command-name> --- 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.

19. whatis command

whatis <command-name> shows a single-line description for the given command.

20. man command

man <command-name> shows the manual page for the given command.

21. tar command

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.

Syntax:

tar [options] [archive-file] [file or directory to be archived]

Options:

-c : Creates Archive

-x : Extract the archive

-f : creates archive with given filename

-t : displays or lists files in archived file

-u : archives and adds to an existing archive file

-v : Displays Verbose Information

-A : Concatenates the archive files

-z : zip, tells tar command that creates tar file using gzip

-j : filter archive tar file using tbzip

-W : Verify an archive file

-r : update or add file or directory in already existed .tar file

Usage:

Creating an uncompressed tar Archive using option -cvf:

tar cvf file.tar *.c

This will create a tar file called file.tar, the Archive of all .c files in the current directory.

Extracting files from Archive using option -xvf :

tar xvf file.tar

gzip compression on the tar Archive, using option -z:

tar cvzf file.tar.gz *.c

This command creates a tar file called file.tar.gz, the Archive of .c files.

Extracting a gzip tar Archive *.tar.gz using option -xvzf:

tar xvzf file.tar.gz

Creating compressed tar archive file in Linux using option -j:

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.

Applying pipe to 'grep command' to find what we are looking for:

tar tvf file.tar | grep "text to find"

Or

tar tvf file.tar | grep "filename.file extension"

22. gzip command

gzip <filename> creates and extracts gzip archives. You can use its gzip -d <filename> to extract gzip archives.

23. unzip command

unzip <archive-to-extract.zip> unzips a given zip archive. You can use unzip -l <archive-to-extract.zip> 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.\

Originally posted at raftlabs.co

Top comments (0)