Linux Journey - 02 Text-Fu PART I
Linux Journey
02. Text-Fu PART I
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 :-)
2.1 stdout (Standard Out)
In Linux, stdout (standard output) is the place where a program sends its output. By default, this is your terminal or console screen.
Examples:
- Displays “Hello World” to the terminal.
1
echo "Hello World
- redirect stdout to a file
1
echo "Hello World!" > output.txt
- Append stdout to a file (adding line without erasing the original content)
1
echo "Hello World!" >> output.txt
2.2 stdin (Standard In)
We can input with keyboard or for example a file. Very useful is that redirect from a file.
1
echo < input.txt
2.3 sterr (Standard Error)
What about errors? In this example the error has its default output in the terminal instead in our file.
1
2
root@PREDATOR-01:/home# ls fake/directory >test2.txt
ls: cannot access 'fake/directory': No such file or directory
But we can redirect it into a file when we know how.
You can redirect stderr to a file using 2>
because 2 represents stderr (just like 1 represents stdout):
1
2
3
root@PREDATOR-01:/home# ls fake/directory 2>test2.txt
root@PREDATOR-01:/home# cat test2.txt
ls: cannot access 'fake/directory': No such file or directory
What if we want both? Write the error into the file and display it on the screen? Lets try it with following command: 2>&1
or &>
1
2
3
ls fake/directory 2>&1 test2.txt
ls: cannot access 'fake/directory': No such file or directory
test2.txt
And if you want to get rid of stderr
completly? You can redirect the error messsages into the linux build-in black hole /dev/null
Example:
1
2
root@PREDATOR-01:/home# ls fake/directory 2>/dev/null
root@PREDATOR-01:/home#
All error messages are gone :-)
2.4 pipe and tee
Lets say we check the contents of /etc
.
ls -la /etc
And we get a very long list of items. But with pipes
we can redirect the output to a file or another command.
root@PREDATOR-01:/home# ls -la /etc | head
This is very useful and you will use it everytime on a linux system.
With tee
you can redirect the output to two different streams. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@PREDATOR-01:/home# ls -la /etc | head | tee test3.txt
total 704
drwxr-xr-x 73 root root 4096 Sep 5 15:10 .
drwxr-xr-x 19 root root 4096 Sep 5 14:46 ..
-rw------- 1 root root 0 Nov 23 2023 .pwd.lock
drwxr-xr-x 2 root root 4096 Nov 23 2023 PackageKit
drwxr-xr-x 4 root root 4096 Nov 23 2023 X11
-rw-r--r-- 1 root root 3028 Nov 23 2023 adduser.conf
drwxr-xr-x 2 root root 4096 Aug 22 20:26 alternatives
drwxr-xr-x 3 root root 4096 Nov 23 2023 apparmor
drwxr-xr-x 8 root root 4096 Aug 22 20:26 apparmor.d
root@PREDATOR-01:/home# cat test3.txt
total 704
drwxr-xr-x 73 root root 4096 Sep 5 15:10 .
drwxr-xr-x 19 root root 4096 Sep 5 14:46 ..
-rw------- 1 root root 0 Nov 23 2023 .pwd.lock
drwxr-xr-x 2 root root 4096 Nov 23 2023 PackageKit
drwxr-xr-x 4 root root 4096 Nov 23 2023 X11
-rw-r--r-- 1 root root 3028 Nov 23 2023 adduser.conf
drwxr-xr-x 2 root root 4096 Aug 22 20:26 alternatives
drwxr-xr-x 3 root root 4096 Nov 23 2023 apparmor
drwxr-xr-x 8 root root 4096 Aug 22 20:26 apparmor.d
Do you see it? I redirect the output from ls
to show me just the first 10 lines with head
and redirect those 10 lines to output on the screen and also into my file test3.txt.
2.5 env (Environment)
Perhaps you heard about environment variables
? No? Okay, lets say the variables contain useful information that a program or shell can use.
One of the most important variables is the PATH
Variable. In there is a list of paths that the system searches when it runs a command!
Lets see ours:
with echo $PATH
we can see a long list of paths. Sometimes its necessary to add/modify entries there.
2.6 cut
The cut command in Linux is used to extract specific sections from lines of text. It works particularly well when data is organized into columns or fields.
- this cuts the characters from position 1-5 so the output is
Hello
1 2
root@PREDATOR-01:/home# echo "Hello, World!" | cut -c 1-5 Hello
- You can use
-d
to specify a delimiter (like a comma or space) and-f
to specify which field(s) you want
1
2
root@PREDATOR-01:/home# echo "apple,banana,cherry" | cut -d ',' -f 2
banana