Linux Journey - 01 Command Line PART 1
Linux Journey
01. Command Line Part 1
Important:
My goal here is to fully understand what I am doing with Linux. Over the years I noticed that I learn best, if I document it and try to explain it to myself in different words. I know that the webpage https://linuxjourney.com/ is already written for beginners never the less I want to rephrase, document and repeat the commands here.
My summary may not be complete so I would recomment you that you actually learn Linux from https://linuxjourney.com/ yourself :-)
1.1 The Shell
What is the shell?
1
It is a program that takes your commands from the keyboard and sends them to the operating system.
Almost all Linux Distributions use as default bash
. (Bourne Again shell)
Other shells are:
1
ksh, zsh, tsch
The $
sign means, that we are a normal user using bash
. If there is a #
it means we are root user.
Our first command here:
1
2
root@PREDATOR-01:/home/linux_journey# echo "Hello, please use this text as output"
Hello, please use this text as output
1.2 pwd ( Print Working Directory)
Everything is a file, even folders. Every file is organized in a hierachical directory tree. The first directory
is the root directory
. The symbol here is /
. Lets see the whole tree.
1
2
3
root@PREDATOR-01:/home/linux_journey# ls /
bin boot dev etc home init lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin snap srv sys tmp usr var
That shows the directory tree from /
.
With pwd
we can find out, where we are at the moment.
```root@PREDATOR-01:/home/linux_journey# pwd /home/linux_journey
1
2
3
4
5
6
7
8
9
10
11
### 1.3 cd (Change Directory)
With the command `cd` we can move around the filesystem. We need paths to navigate. There are two different ways to specify a path - `absolute` and `relative` paths.
- Absolute path: This is the complete path from the root directory (`/`)
- Relative parth: This is the path from where you are currently in the filesystem
Lets try it a bit:
root@PREDATOR-01:/# cd /home root@PREDATOR-01:/home# cd linux_journey/ root@PREDATOR-01:/home/linux_journey#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Here some shortcuts:
- `cd .` (current directory) This is the directory you are currently in
- `cd ..` (parent directory) Takes you to the directory above your current
- `cd ~` (home directory) This directory defaults to your home directory. (User)
- `cd -` (previous directory) This will take you to the previous directory you were just at.
### 1.4 ls (List Directories)
The command `ls` lists the content in our current directory by default. You can also specify the path.
```bash
root@PREDATOR-01:/home/linux_journey# ls -la
total 8
drwxr-xr-x 2 root root 4096 Sep 5 15:45 .
drwxr-xr-x 3 root root 4096 Sep 5 15:11 ..
-rw-r--r-- 1 root root 0 Sep 5 15:45 .test1.txt
-rw-r--r-- 1 root root 0 Sep 5 15:44 test1.txt
The flag l
lists detailed informations about the file. For example permissions, size and date created.
The flag a
lists all files even hidden files in your directory (with “.” files are hidden normally)
1.5 touch
With touch
we can create new files.
1
2
3
root@PREDATOR-01:/home/linux_journey# touch test2.txt
root@PREDATOR-01:/home/linux_journey# ls
test1.txt test2.txt
1.6 file
In Linux filenames arent required to represent the contents of a file. You can write whatever ending on a filename which you like. With the file
command, we can find out, what a file really is.
1
2
root@PREDATOR-01:/home/linux_journey# file test2.txt
test2.txt: ASCII text
1.7 cat
With cat
you can read the contents of a file. Its not good for large files and for much content. There are other options for those files like less
or tail/head
Lets output from a file:
1
2
root@PREDATOR-01:/home/linux_journey# cat test2.txt
I wrote something...
1.8 less
Like mentioned in the 1.7 cat
we can use less
for display a large file paged, so it is besser accessible.
Commands for navigate trough less:
- q - quits the program
- g - moves to the beginning of the text file
- G - moves to the end of the text file
- / - searching inside the text file (/”Search”)
- h - help inside
less
- PG up and PG Down - next page or page back
1.9 history
We can use history
if we want to see which commands we have previously entered.
With CTRL + R we can also “reverse search” an entered command.
This is my history:
1
2
3
4
5
6
7
8
107 ls -la
108 touch test2.txt
109 ls
110 nano test2.txt
111 file test2.txt
112 cat test2.txt
113 less test2.txt
114 history
with clear
we can clear our terminal.
1.10 cp (copy)
With cp
we can copy files from A to B.
1
2
3
4
root@PREDATOR-01:/home/linux_journey# cp test1.txt /home/test2.txt
root@PREDATOR-01:/home/linux_journey# cd ..
root@PREDATOR-01:/home# ls
linux_journey test2.txt
I copied the file test1.txt
to /home/
and renamed it also to test2.txt
.
You can also copy multiple files with wildcards.
*
the wildcard of wildcards. represents all single characters or any string?
used to represent one character[]
used to represent any character within the brackets
1
2
3
4
root@PREDATOR-01:/home/linux_journey# cp *.txt /home/
root@PREDATOR-01:/home/linux_journey# cd ..
root@PREDATOR-01:/home# ls
linux_journey test1.txt test2.txt
The flag -r
is very usefull, it means “recursive” which tells the command to copy everything inside a directory, including subfolders and files.
1
cp -r folder1 folder2
This will copy all files and subfolders from folder1
to folder2
.
Also very good is the flag -i
for “interactive”. It asks you if you really want to overwrite or similar.