Originally published at webdeasy.de.
New to the Linux world and no idea which commands you need for what? Then you are not alone: All beginnings are difficult, but here I show you the top 20 Linux commands for Beginner!
Whether you’re a new system/server administrator, new to the computer science world, or simply switching to Linux from another operating system, you can’t get around the Linux commands in the console. Even when programming a Raspberry PI, Linux is ubiquitous. And let’s be honest: you feel pretty cool when you can handle the Linux command line quickly and confidently 😆. If you know these top 20 important Linux commands, you are well prepared.
1. Basic commands
- 1.1. cd – Navigate
- 1.2. ls – List files
- 1.3. nano/vim – Edit files
- 1.4. pwd – Show current directory
- 1.5. clear – Clear console
- 1.6. cat – Output content to the console
- 1.7. sudo – Run command as administrator
- 1.8. grep – Filter outputs
- 1.9. history – Show last commands
- 1.10. man – Help page
- 1.11. wget – Download files from the Internet
- 2. File Tools
- 3. Permissions management
For each command I have given different possible applications. This shows that you can combine and use the commands almost arbitrarily. Most commands can also be executed with additional parameters (settings). To display these, take a closer look at the
The most important thing is to navigate safely and quickly through the directory structure of a computer. Here I have listed several possibilities, also to change several directories at the same time:
# Go to "directory1" folder cd directory1 # Go to "directory1" and in this folder go to "subdir1" cd directory1/subdir1 # Go to ".ssh" in the root directory cd ~/.ssh # Go to the parent directory cd .. # Go two directories up cd ../..
ls command lists files in a directory. You will probably use this command by far the most.
# List files ls # List files in a vertical list with more detailsls ls -l # List files in the "dir1" folder ls -l dir1
To edit a file there are several text editors. Nano and Vim are two well-known ones that are preinstalled on most Linux distributions. Which one you want to use is up to you. Since there is still some explanation needed on how to use them, I have linked two articles for you. To open the different editors you use the following commands:
# Open yourfile.txt with nano nano yourfile.txt # Open yourfile.txt with vim vim yourfile.txt
pwd you can display the current directory, nothing more and nothing less:
# e.g. output: /var/www/vhosts/webdeasy.de/httpdocs pwd
clear command clears the console window. So no inputs are deleted, they are still accessible via
history, only the visible area is cleaned up.
This command prints the content of a file to the console. With large files you sometimes get very long outputs which spam the console. There you get sometimes fear what you have done. But don’t worry.
# print the content of .htaccess cat .htaccess # print the content of index.php in the httpdocs directory cat httpdocs/index.php
Did you know that you can cancel a running command with CTRL+C?>
Often you are not running as administrator in your terminal. If you then want to edit a file created by the administrator, for example, you have no permissions to do so. The
sudo command is a remedy for this. You can put this little word in front of every command to execute it with administrator rights. Be careful what you do with it. The first time you run it, you have to enter the administrator password:
# Print content of the .htaccess (with administrator permissions) sudo cat .htaccess # Go to dir2 sudo cd dir2 # Edit .htaccess sudo nano .htaccess
You have a 500MB log file and are looking for a specific IP address? With
grep you can save yourself hours of searching. The command can search all lines for a search pattern and print it highlighted. You can also use regular expressions:
# Get all lines with "AddType" from .htaccess grep AddType .htaccess # get all lines with "AddType" from the application mime-type grep "AddType application/.*" .htaccess # Alternative: Combine the cat and grep command with a pipe cat .htaccess | grep AddType
In the third example, I have added a pipe. That is this straight vertical line: |.
This allows us to pass output from one command to the next. There we take the complete output from the first command (
cat .htaccess) and only output lines with “AddType”. Cool, isn’t it? You can combine commands as you like, just try it with other commands.
Last week you made up a very important and long command, but forgot to write it down? Don’t worry, just type
history into the console. With this command you get a list of the last commands and can copy them out.
In many console applications, e.g. the terminal under macOS or Putty under Windows, text is copied as soon as you have marked it normally with the left mouse button and pasted when you simply click again in the console with the right mouse button.
Finally! We have arrived at my favorite command. Let’s see how many I can convince of him. 🙂
man you can display a help page for each command with all possible parameters and explanations. Also for e.g. C or C++ corresponding commands can be looked up:
# manual for ls man ls # manual for wget man wget # manual for the pthread function (c, c++) man pthread
You can exit the manual by pressing the “q” key.
So if you don’t remember exactly how a command works, you can just use this command and it can save you a lot of time.
This command can download any freely accessible file from the Internet or intranet to your server/computer. For what? Especially for server administration you often have only a console and no GUI available and then this command is the fastest and easiest way to download a file:
# download the thumbnail of this post wget https://webdeasy.de/wp-content/uploads/2021/04/linux-commands.jpg
To resolve such a domain (webdeasy.de) into an IP, DNS is needed. What DNS is and how it works, you will learn in this article about the domain name system.
With this command you can create directories – very easily:
# make directory "newDir" mkdir newDir
This command updates the timestamp of the specified file. This is needed relatively seldom. What you need rather more and why I also present this command is that you can also create files with it if they do not exist:
# create file phpinfo.php (if the file does not exist) touch phpinfo.php # update timstamp of phpinfo.php (if the file exists) touch phpinfo.php
This command calculates the size of a directory or a file. Here you can play around a lot with the parameters for a nice display. For my cases I mostly use the following commands:
# size of the htaccess in bytes. output e.g. 12 .htaccess du .htaccess # size of the htaccess (-h = human readable). output e.g. 12K .htaccess du -h .htaccess # size of EVERY SINGLE file inside the httpdocs directory (bad :c) du httpdocs/ # size of the httpdocs directory (-s = summarize, -h = human readable). output e.g. 876M httpdocs/ du -sh httpdocs/
This command is mostly used to perform two actions: Moving and renaming files. Because if you think about it, moving to the same directory with a different name is basically renaming a file. 😉
# rename file1.txt to file2.txt mv file1.txt file2.txt # rename file1.txt to file2.txt (in dir1 folder) mv dir1/file1.txt dir1/file2.txt # move file1.txt to httpdocs folder mv file1.txt httpdocs
Similar behavior as when moving/renaming files. Note the
-R parameter when copying directories:
# copy file1.txt to file1-copy.txt cp file1.txt file1-copy.txt # copy httpdocs directory to httpdocs_BAK directory cp -R httpdocs/ httpdocs_BAK # copy httpdocs directory to httpdocs_BAK directory in the parents directory cp -R httpdocs/ ../httpdocs_BAK
Files sometimes need to be deleted, for that there is this command. Again, the
-R parameter must be specified when deleting a directory:
# remove file1.txt rm file1.txt # remove BAK directory rm BAK/ -R
Attention: This command cannot be undone! Check the command before you execute it.
A .tar, .tar.gz or .tgz file is an archive file under Linux – equivalent to a .zip or .rar file under Windows. With the
tar command you can pack and unpack these archives. Feel free to look at all parameters with
man tar. The following commands are in a combination in which I use them:
# pack httpdocs into archive.tgz tar -czf archive.tgz httpdocs # unpack archive.tgz into the current directory tar -xvzf archive.tgz
With this command you can change the so-called file mode bits. These indicate whether a file is readable, writable and executable. They are divided into 8 levels:
- 0: No permission
- 1: Execute
- 2: Write
- 3: Write and execute
- 4: Read
- 5: Read and execute
- 6: Read and write
- 7: Read, write, and execute This allows you to precisely set permissions for files and folders:
# all permissions for all files and directories in this directory (don't use this in public! e.g. webservers) chmod 777 * -R # set default permissions (good solution :)) chmod 755 .htaccess
If you want to know more about this command, you can read it here.
With the last command you can adjust the owner and group of a file or directory. This is absolutely necessary for computers with multiple users:
# set the owner to user1 and the group psaserv to file1.txt chown user1:psaserv file1.txt # set the owner and group root to the httpdocs directory and all files and directories in it chown root:root httpdocs -R
Can you think of any other commands that should not be missing here? Feel free to write them in the comments. Thanks for reading! 🙂