20 Bash Commands for Linux Beginners ⌨️😍
Tags: linux, webdev, beginners
Published: 2021-04-09 08:06:57

_Originally published at [webdeasy.de](https://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.

#Table of contents
* __1. Basic commands__
 * [1.1. cd – Navigate](#cd)
 * [1.2. ls – List files](#ls)
 * [1.3. nano/vim – Edit files](#nano)
 * [1.4. pwd – Show current directory](#pwd)
 * [1.5. clear – Clear console](#clear)
 * [1.6. cat – Output content to the console](#cat)
 * [1.7. sudo – Run command as administrator](#sudo)
 * [1.8. grep – Filter outputs](#grep)
 * [1.9. history – Show last commands](#history)
 * [1.10. man – Help page](#man)
 * [1.11. wget – Download files from the Internet](#wget)
* __2. File Tools__
 * [2.1. mkdir – Create directory](#mkdir)
 * [2.2. touch – Create file](#touch)
 * [2.3. du – Calculate file/directory size](#du)
 * [2.4. mv – Move/rename file](#mv)
 * [2.5. cp – Copy file/directory](#cp)
 * [2.6. rm – Delete file/directory](#rm)
 * [2.7. tar – Archiv packen/entpacken](#tar)
* __3. Permissions management__
 * [3.2. chmod – Customize permissions](#chmod)
 * [3.1. chown – Customize owner/group](#chown)

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 [`man` command](#man).


#1. Basic commands
##1.1. cd – Navigate <a name="cd"></a>
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:
```shell
# 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 ../..
```


##1.2. ls – List files <a name="ls"></a>
The `ls` command lists files in a directory. You will probably use this command by far the most.
```shell
# List files
ls

# List files in a vertical list with more detailsls 
ls -l

# List files in the "dir1" folder
ls -l dir1
```


##1.3. nano/vim – Edit files <a name="nano"></a>
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:
```shell
# Open yourfile.txt with nano
nano yourfile.txt

# Open yourfile.txt with vim
vim yourfile.txt
```
[Operate Nano](https://linuxize.com/post/how-to-use-nano-text-editor/)

[Operate Vim](https://www.computerhope.com/unix/vim.htm)


##1.4. pwd – Show current directory <a name="pwd"></a>
With `pwd` you can display the current directory, nothing more and nothing less:
```shell
# e.g. output: /var/www/vhosts/webdeasy.de/httpdocs
pwd
```


##1.5. clear – Clear console <a name="clear"></a>
The `clear` command clears the console window. So no inputs are deleted, they are still accessible via [`history`](#history), only the visible area is cleaned up.


##1.6. cat – Output content to the console <a name="cat"></a>
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.
```shell
# 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?>


##1.7. sudo – Run command as administrator <a name="sudo"></a>
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:

```shell
# Print content of the .htaccess (with administrator permissions)
sudo cat .htaccess

# Go to dir2
sudo cd dir2

# Edit .htaccess
sudo nano .htaccess
```


##1.8. grep – Filter outputs <a name="grep"></a>
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:
```shell
# 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.

##1.9. history – Show last commands <a name="history"></a>
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.

##1.10. man – Help page <a name="man"></a>
Finally! We have arrived at my favorite command. Let’s see how many I can convince of him. 🙂

With `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:
```shell
# 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.

## 1.11. wget – Download files from the Internet <a name="wget"></a>
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:
```shell
# 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](https://webdeasy.de/en/domain-name-system-dns-basics/).

#2. File Tools
##2.1. mkdir – Create directory <a name="mkdir"></a>
With this command you can create directories – very easily:
```shell
# make directory "newDir"
mkdir newDir
```
##2.2. touch – Create file <a name="touch"></a>
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:
```shell
# 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
```

##2.3. du – Calculate file/directory size <a name="du"></a>
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:
```shell
# 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/
```

##2.4. mv – Move/rename file <a name="mv"></a>
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. 😉
```shell
# 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
```
##2.5. cp – Copy file/directory <a name="cp"></a>
Similar behavior as when moving/renaming files. Note the `-R` parameter when copying directories:
```shell
# 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
```
##2.6. rm – Delete file/directory <a name="rm"></a>
Files sometimes need to be deleted, for that there is this command. Again, the `-R` parameter must be specified when deleting a directory:
```shell
# 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.

##2.7. tar – Archiv packen/entpacken <a name="tar"></a>
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:
```shell
# pack httpdocs into archive.tgz
tar -czf archive.tgz httpdocs

# unpack archive.tgz into the current directory
tar -xvzf archive.tgz
```
#3. Permissions management
##3.2. chmod – Customize permissions <a name="chmod"></a>
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:
```shell
# 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](https://www.makeuseof.com/tag/chmod-command-linux-file-permissions/).

##3.1. chown – Customize owner/group <a name="chown"></a>
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:
```shell
# 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! 🙂