It doesn't matter if you are already working on Linux or not. In this post, you will learn some Linux commands. There are 25 of them absolutely everybody should know! What you will learn with this post (or add it to favorites and copy-paste) will boost you both as a user, as well as an admin.
What this command does is to browse throughout directories.
Example usage: cd /var
With this command you list files. You can use it both for your current directory with a simple ls
or you can use it to see the contents of different directory ls /desired/directory
. You can use the a flag to show hidden files(starting with .) ls -a
. If you want to get more information about the files, for example, what their permissions are, who the owner is, and at what time & date they have been updated/created, you can use the -l argument - ls -l
. You can of course merge both of the arguments and list all the hidden files, as well as get a lot of information - ls -la
.
With this command you can edit files - it is a text editor. However, you can use it to create files as well. If you open a not existing file with nano
you can save it and this way you create the file. Example usage: nano my-new-file
. You can save with ctrl+o & exit with ctrl+x.
With this command you create files.
Example usage: touch my-new-file
.
With this command, you print the contents of a file.
Example usage cat my-file
.
With this command, you can in a way 'open' the file and browse through it with the arrows. You can also search in the file with /
.
Example usage: less my-file
With this command, we print the finishing lines of a file. We can specify how many lines to print with the -n flag.
Example usage tail -n 15 my-file
to print 15 lines of a file.
This command is the same as tail
but from the start of the document.
With this command, we check what our storage usage is. For a more human-readable version of the number, we use the -h flag.
Example usage df -h
.
With this command, we move a file from one directory to another. Example usage mv /path1/file1 /path2/
. We also rename files with this command.
Example usage mv file-name-1 file-name-2
.
With this command, we test our connectivity to a server. We can also check for packet loss in the output of the command.
Example usage ping google.com
.
With this command, we remove/delete. We can delete files with rm my-file
or delete directories with rm -r my-directory
. There is one more flag we can is - f which FORCES the delete. This(rm -rf
is one of the most dangerous commands to use because it does not prompt you to ask if you want to delete the directory specified - it deletes it on the go. Careful!
With this command, we create a directory.
Example usage mkdir my-new-dir
.
With this command we copy. We can copy the contents of a file and output it to another, already existing one - cp existing-file1 existing-file2
, or we can copy the content of the file and create a new file and output it there cp existing-file my-new-file
.
With this command, we execute a command with administrative privileges. Of course, this is only possible if the user is in the sudoers group.
Example usage sudo <command>
.
With this command, we switch users.
Example usage su user1
.
With this command, we create a new user.
Example usage useradd username
.
With this command, we change the password of an existing user.
Example usage passwd user1
.
With this command, we change the permissions of a file/directory. Permissions are being calculated accordingly - 1 execute; 2 - write; 4 - read. When we assign permissions we use 3 digit number structured from the permissions for the owner, permissions for the group, and permissions for others. Example: chmod 754 file1
- in this command, we specified file1 has full read, write & execute access for the owner, read and execute permissions for the group, and read-only for everybody else. To understand more about permissions, please check this source.
With this command, we change the owner for the directory/files.
Example usage chown userA
.
With this command, we can filter data. We can, for example, use it while trying to cat
a file and we only need a specific word. Example scenario: We need to find the line where the word 'Dinosaur' exists in a file named 'myFile'.
Example command: cat myFile | grep 'Dinosaur'
.
Bonus: we use |
when we want to execute 2 commands at the same time to work together.
We use this command to clear the screen of the terminal. pssst, we don't use it. We use ctrl + l to clear the screen.
With this command, we can find the IP of our server. The command is pretty straight forward - ip a
.
Today you learned 25 crucial to your admin journey commands. I hope you feel safer in the terminal and I hope you are going to be careful with the rm -rf
one.
Happy browsing & be safe!