Linux users and administrators can’t really live by the GUI alone. By only learning how to work with your tool, can you get the most out of Linux. Hence, we’ve brought together a list of useful Linux commands into this convenient guide, which will be of help no matter which Linux Curriculum you choose to learn from.
So, I’ve categorized these commands into the following segments:
- Linux Basic Commands
- Commands for Working with Files
- Commands for Working with Directories
- Commands for Working with User Permissions
- Commands for Working with Zipped Files
- Working with Secure Shell For Remote Machine Access
Linux provides a CLI (Command Line Interface) to communicate with the OS. Here are the most basic of the Linux Commands.
1. pwd
Whenever you feel lost in the filesystem, call the pwd command to know where you are. It will print the current folder path.
syntax:
$ pwd
$ pwd
/home/mmkernel
$ █
2. echo
The echo command does one simple job: it prints to the output the argument passed to it.
syntax:
$ echo "<text>"
$ echo "Hello mmkernel readers!"
Hello mmkernel readers!
$ █
This command is used to switch to root-user so that superuser permissions can be used to execute commands.
3. man
Every time I don’t know how to use a command, I type man <command> to get the manual:
syntax:
$ man
$ man cat
CAT(1) User Commands CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
Manual page cat(1) line 1 (press h for help or q to quit)
4. su <username>
This command is used to switch to a different user whose name is passed as the argument.
syntax:
$ su <username>
$ su admin
Password:
5. sudo
sudo is commonly used to run a command as root. You must be enabled to use sudo, and once you do, you can run commands as root by entering your user’s password (not the root user password). The permissions are highly configurable, which is great especially in a multi-user server environment, and some users can be granted access to running specific commands through sudo.
syntax:
$ sudo <command>
$ sudo useradd newuser
6. clear
Type clear to clear all the previous commands that were ran in the current terminal. The screen will clear and you will just see the prompt at the top:
syntax:
$ clear
Linux Commands: Working with Files
7. cp
You can copy a file using the cp command:
syntax:
$ cp <flag> {filename} /pathname/
$ cp file another_file
$ █
To copy folders you need to add the -r option to recursively copy the whole folder contents:
$ mkdir blockchain
$ cp -r blockchain crypto
$ █
8. mv
Once you have a file, you can move it around using the mv command. You specify the file current path, and its new path:
syntax:
$ mv <flag> {filename} /pathname/
$ mv pear new_pear
$ █
The pear file is now moved to new_pear. This is how you can rename files and folders. If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files, and they will all be moved in the folder path identified by the last parameter:
$ touch pear
$ touch apple
$ mkdir fruits
$ mv pear apple fruits
$ █
9. rm
This command removes files from a directory. By default, the rm command does not remove directories. Once removed, the contents of a file cannot be recovered.
syntax:
$ rm <flag> {filename}
$ ls
ada bitcoin bnb eth new_file.txt solana
$ rm new_file.txt
$ ls
ada bitcoin bnb eth solana
$ █
rm -r: Removes even non-empty directories.
rm -rp: Removes non-empty directories including parent and subdirectories.
10. grep
This command is used to search for a particular string/ word in a text file. This is similar to “Ctrl+F”, but executed via a CLI.
syntax:
$ grep <flag or element_to_search> {filename}
grep -i: Returns the results for case-insensitive strings
grep -n: Returns the matching strings along with their line number
grep -v: Returns the result of lines not matching the search string
grep -c: Returns the number of lines in which the results matched the search string
11. cat
This command can read, modify or concatenate text files. It also displays file contents.
syntax:
$ cat <flag> {filename}
$ cat btc.txt
Bitcoin is a decentralized digital currency that can be transferred on the peer-to-peer bitcoin network. Bitcoin transactions are verified by network nodes through cryptography and recorded in a public distributed ledger called a blockchain.
$ █
You can print the content of multiple files: cat file1 file2 and using the output redirection operator > you can concatenate the content of multiple files into a new file:
$ cat file1 file2 > file3
Using >> you can append the content of multiple files into a new file, creating it if it does not exist:
$ cat file1 file2 >> file3
When watching source code files it’s great to see the line numbers, and you can have cat print them using the -n option:
$ cat -n file1
You can only add a number to non-blank lines using -b, or you can also remove all the multiple empty lines using -s.
cat is often used in combination with the pipe operator | to feed a file content as input to another command:
$ cat file1 | anothercommand
Linux Commands: Working with Directories
12. ls
Inside a folder you can list all the files that the folder contains using the ls command:
syntax:
$ ls <flag>
$ ls
ada bitcoin bnb eth solana
$ █
If you add a folder name or path, it will print that folder contents.
ls accepts a lot of options. One of my favorite options combinations is -al . Try it:
$ ls -al /usr
total 96
drwxr-xr-x 14 root root 4096 Apr 23 2020 .
drwxr-xr-x 19 root root 4096 Oct 28 11:33 ..
drwxr-xr-x 2 root root 36864 Sep 18 16:30 bin
drwxr-xr-x 2 root root 4096 Apr 15 2020 games
drwxr-xr-x 7 root root 4096 Sep 2 11:51 include
drwxr-xr-x 73 root root 4096 Aug 23 15:56 lib
drwxr-xr-x 2 root root 4096 Apr 23 2020 lib32
drwxr-xr-x 2 root root 4096 Apr 23 2020 lib64
drwxr-xr-x 3 root root 4096 Apr 23 2020 libexec
drwxr-xr-x 2 root root 4096 Apr 23 2020 libx32
drwxr-xr-x 10 root root 4096 Apr 23 2020 local
drwxr-xr-x 2 root root 12288 Aug 23 15:56 sbin
drwxr-xr-x 114 root root 4096 Sep 18 16:30 share
drwxr-xr-x 2 root root 4096 Apr 23 2020 src
$ █
Compared to the plain ls , this returns much more information.
You have, from left to right:
– the file permissions (and if your system supports ACLs, you get an ACL flag as well)
– the number of links to that file
– the owner of the file
– the group of the file
– the file size in bytes
– the file modified datetime
– the file name
This set of data is generated by the l option. The a option instead also shows the hidden files.
Hidden files are files that start with a dot ( . )
13. cd
This command is used to change the current working directory of the user.
syntax:
$ cd /pathname/
14. sort
This command sorts the results of a search either alphabetically or numerically. Files, file contents and directories can be sorted using this command.
syntax:
$ sort <flag> {filename}
15. mkdir
This command is used to create a new directory.
16. rmdir
This command is used to remove a specified directory. Although by default, it can only remove an empty directory, there are flags which can be deployed to delete the non-empty directories as well.
syntax:
$ rmdir <flag> {directoryname}
Linux Commands: Working with User Permissions
17. chmod
This command is used to change the access permissions of files and directories. Consider the example below.
On trying to run the newly created file named chmodtest.sh, an error is thrown. After modifying the permissions of the file using the said Linux command, it turns executable.
syntax:
$ chmod <permissions of user,group,others> {filename}
The permissions associated with each digit is as follows.
Linux Commands: Installing Packages
Stable versions of most software’s will already be available in Linux repositories. Here are the Linux Commands to install them.
18. install packages
For an RHEL based system;
syntax:
$ sudo yum install package-name
For a Debian based system;
syntax:
$ sudo apt-get install package-name
For a Fedora based system;
syntax:
$ sudo dnf install package-name
Linux Commands: Working with Zipped Files
When you download a package from the internet, the downloaded file comes in compressed form. Here are a few commands to decompress and compress files in Linux.
19. tar
The following command is used to zip files of .tar format.
syntax:
$ tar –cvf tar-filename source-folder-name
The following command is used to unzip files of .tar format.
syntax:
$ tar –xvf tar-file-name
Linux Commands: Working with Secure Shell For Remote Machine Access
20. ssh
This command refers to a cryptographic network protocol for operating network services securely over an unsecured network. Typical use-cases include remote command-line execution, but any network service can be secured with SSH.
The following command, on running at the slave node, will give remote access to the master.
syntax:
$ ssh <master's ip>
The following command, on running at the master, will give remote access to the slave node.
syntax:
$ ssh <slave's ip>