GNU/Linux bilješke 2 - dio


Ovo je drugi dio mojih GNU/Linux bilješki koje prate moj aktuelni Udemy Linux LPIC-1 Certification Course Exam 101-102 kurs. Ove bilješke pokrivaju Standard Input/Output gdje sam imao priliku raditi sa manipulacijom datoteka koristeći komande tail i head. 

1. The command read is used to read from a file descriptor. Here's a quick sample.

# read ME
# echo 'This is a ME file' > ME
# echo $ME

In the above sample, ME file has been created. Afterwards, using echo command, a sample text was written into a ME file. Using echo command, we can display the ME file content in bash prompt. 

2. In the following sample, the content of a file output.txt is passed to previously created ME file.

# read ME < output.txt
# echo $ME
# This is a newly created file

Note: The read command can only read the first line of a file. 

3. We can pass a content of directory into a file using the following command.

# ls /etc > output.txt 

This command will display the content of /etc directory and save it into a output.txt file. 

4. In order to send standard output error into a file, instead of screen, we can use the following command.

# ls sample.txt 2> error

This command try to list the content of a file sample.txt. Since this file does not exist, the error message: 'No such file or directory' is going to be stored into a error file. 

5. The tail command outputs the last 10 lines of file. In order to monitore a file in realtime as it is being updated, we can use the following command.

# tail -f syslog.1 

As each new log is added to a log file, tail updates it and display into terminal window. The -f option in tail command it telling to output appended data as the file grows. 

6. To display last 15 line of a file, we can use the following command.

# tail -n 15 output.txt

Since we recently saved the content of a /etc directory into output.txt file, it will be robust to read it completely on bash prompt, therefore we tell the tail command to display only 15 last line of a file.

7. In order to append a content into a file using tail command, we can use double redirection signs >>. Here's the quick sample.

# tail -f error
# echo 'Testing tail append mode' >> error

Every time we heat the enter key on keyboard, the text from echo command shall be send into a error file, adding new lines of text. 

8. The head command display the first 10 lines of a file, while tail command displays the 10 last lines of a file.

9. In order to display the first 5 lines of a file using the head command, we can use the following command.

# head -n 5 output

10. To display the last 5 lines of a output file, we use the following command.

# tail -n 5 output


This was my second part of ongoing Udemy Linux LPIC-1 course which I'm enrolled in. If you like my article, make sure to share it on to social network, leave a comment down bellow and I see you in third part, so soon. 

Nema komentara:

Objavi komentar