Usage of tail command with various options


Environment and Prerequisite

  • Linux base system
  • Ubuntu 16.04
  • Bash shell(/bin/bash)


tail Command

tail [OPTION]... [FILE]...
  • tail: Print file contents from back. It can be combined with many options and also can be used with input via pipe. It is widely used for logging.
  • [FILE] is its argument but if it is omitted then standard inputs will be used.


tail Command Options

tail [OPTION]... [FILE]...

-c, –bytes=K

  • Print K bytes from back of file. If use + like +K, then it will print from Kth character to end.

-n, –lines=K

  • Print K lines from back of file. If use + like +K, then it will print from Kth line to end. Default value is 10 so if K is omitted then it will print 10 lines.

-f, –follow[={name|descriptor}]

  • Keep tracking file and prints new appended file contents. Its default value is descriptor.
  • Because its default option value is descriptor so if file name is changed then it prints changed file contents not previous file name contents.
  • It can track previous file(which is used in when tail command is starting) by using -F or --follow=name --retry

-q, –quiet, –silent

  • Never output headers giving file names

-v, –verbose

  • Always output headers giving file names

-F

  • Same as --follow=name --retry
  • It keeps tracking the file(which is used in when tail command is starting) even though file name is changed during tail command is running.

–max-unchanged-stats=N

  • With -f or --follow=name, reopen a file even if its size has not changed, after every N checks.

–pid=PID

  • With -f or --follow, terminate command when pid is killed.
  • It is useful when make specific process’s log.

–retry

  • Keep trying to open a file with --follow=name

-s, –sleep-interval=N

  • With -f or --follow, sleep for N seconds for file checking.
  • With --pid=PID, check process state at least N seconds.


Examples

Make file for examples

$ cat << EOF > test.txt
123
456
789
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839
EOF


Basic

  • Basic usage
  • One file
$ tail test.txt
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839
  • Also multiple files can be used.
$ tail test.txt test2.txt
==> test.txt <==
101112
131415
161718
192021
...

==> test2.txt <==
a
a
...


n Option Example

  • Print last 3 lines.
$ tail -n 3 test.txt
313233
343536
373839
  • Print from 3rd line to end.
$ tail -n +3 test.txt
789
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839


c Option Example

  • Print last 3 bytes.
  • It prints 3 and 9 because one byte new line character(0a) is included at back.
$ tail -c 3 test.txt
39
  • Print from 21th character(one byte size) to end.
$ tail -c +21 test.txt
31415
161718
192021
222324
252627
282930
313233
343536
373839


f Option Example

  • Before file input
$ tail -f test.txt
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839
  • Add one line
$ echo "new line" >> test.txt
  • After new lined is added.
$ tail -f test.txt
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839
new line
  • Left picture shows running tail command and right picture shows trying to add new line to file.
  • After new line is added then you can see new added line is shown up in left side of next picture.

f-option-example-before

f-option-example-after

  • Because its default option value is descriptor so if file name is changed then it prints changed file contents not previous file name contents.
  • It can track previous file(which is used in when tail command is starting) by using -F or --follow=name --retry


q Option Example

  • File name omission using q option.
  • You can use q option for omitting file names when using tail with multiple files.
$ tail -n 3 test.txt test2.txt
==> test.txt <==
343536
373839
new line

==> test2.txt <==
a
a
a
$ tail -n 3 -v test.txt test2.txt
343536
373839
new line
a
a
a


v Option Example

  • Print with file name.
$ tail -n 3 -v test.txt
==> test.txt <==
343536
373839
new line


F Option Example

  • In case which file name is changed or deleted but user wants to keep tracking such file.
  • It keeps printing file contents even though file name is not accessible.
$ tail -F test.txt
101112
131415
161718
192021
222324
252627
282930
313233
343536
373839
t
tail: 'test.txt' has become inaccessible: No such file or directory
tail: 'test.txt' has appeared;  following new file
t


pid Option Example

  • Make and run shell script for pid option test.
$ cat << EOF >> pid_test_shell.sh
#!/bin/bash
while true
do
	sleep 1
done
EOF

$ chmod +x pid_test_shell.sh
$ ./pid_test_shell.sh &
[1] 8201
  • Use tail command when running above shell script process.
$ tail -f test.txt --pid 8201
  • tail command will be finished when process dies.
$ sudo kill -9 8201


Reference

curl 명령어를 이용해 리소스(파일이나 웹페이지)를 받아보자.


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)
  • curl의 사용법


curl 명령어에 대해서

  • curl: Client URL의 약자로 지원하는 프로토콜들을 이용해 서버에 데이터를 보내거나 가져올 때 사용하는 도구입니다.
  • 사용자의 인터랙션 없이 사용할 수 있으며 많은 프로토콜과 메소드를 지원하며 다양한 옵션들을 이용한 많은 기능들을 사용할 수 있습니다.
  • 지원 프로토콜들: DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP and ETC.


curl을 이용해 웹에 있는 리소스 받기

(방법 1) -o 또는 –output 옵션을 이용하기

  • 해당 옵션은 현재 디렉토리에만 받을 수 있습니다.
curl [URL] -o [RESOURCE_NAME]

or

curl [URL] --output [RESOURCE_NAME]


(방법 2) Redirection 이용하기

  • 원하는 PATH에 다운받을 수 있습니다.
curl [URL] > [RESOURCE_NAME_WITH_PATH]


예제: curl을 이용해 cirros 이미지 받기

사용할 정보


(방법 1) -o 또는 –output 옵션 이용하기

  • 파일 다운로드
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -o cirros-0.4.0-x86_64-disk.img
  • MD5 해시값 확인
  • MD5 해시 명령어는 OS에 따라 다를 수 있습니다.
$ md5sum cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe  cirros-0.4.0-x86_64-disk.img


(방법 2) Redirection 이용하기

  • 파일 다운로드
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img > /home/twpower/test/cirros-0.4.0-x86_64-disk.img
  • MD5 해시값 확인
  • MD5 해시 명령어는 OS에 따라 다를 수 있습니다.
$ md5sum /home/twpower/test/cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe


참고자료

Download resources(files or web pages) by using curl command.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • Usage of curl command


About curl command

  • curl: It stands for Client URL. It is a tool to transfer data from or to a server, using one of the supported protocols
  • The command is designed to work without user interaction. It supports many protocols and methods also with many options.
  • Supported Protocols: DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP and ETC.


Download resource using curl command

(Method 1) Use -o or –output option

  • These options only support download to current working directory.
curl [URL] -o [RESOURCE_NAME]

or

curl [URL] --output [RESOURCE_NAME]


(Method 2) Use redirection

  • You can download resource to any directory.
curl [URL] > [RESOURCE_NAME_WITH_PATH]


Example: Download cirros image using curl command

Requirements


(Method 1) Use -o or –output option

  • Download file
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -o cirros-0.4.0-x86_64-disk.img
  • Check MD5 hash value
  • MD5 hash value could be different depends on OS.
$ md5sum cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe  cirros-0.4.0-x86_64-disk.img


(Method 2) Use redirection

  • Download file
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img > /home/twpower/test/cirros-0.4.0-x86_64-disk.img
  • Check MD5 hash value
  • MD5 hash value could be different depends on OS.
$ md5sum /home/twpower/test/cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe


Reference

쉘 스크립트에서 sudo 패스워드를 전달 받거나 스크립트에서 사용해보자


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)


방법

  • -S 옵션과 |를 이용
echo "PASSWORD" | sudo -S apt-get update
  • 파일을 이용하는 경우
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo -S apt-get update


  • --stdin 옵션과 |를 이용
echo "PASSWORD" | sudo --stdin apt-get update
  • 파일을 이용하는 경우
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo --stdin apt-get update


참고자료

Process file or result of command in while loop line by line in shell script.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)


Solution

  • Use -S option and |
echo "PASSWORD" | sudo -S apt-get update
  • Usage with file
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo -S apt-get update


  • Use --stdin option and |
echo "PASSWORD" | sudo --stdin apt-get update
  • Usage with file
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo --stdin apt-get update

Reference