Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Linux - Working with files contents

Introduction

In this session, we have covered the contents of text files with head, tail, cat, tac, more, less and strings. We will also get a glimpse of the possibilities of tools like a cat on the command line.

head

You can use the head command to print the first 10 lines of each FILE to standard output.

[email protected]:~$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
[email protected]:~$

The head command can also display the first n lines of a file.

[email protected]:~$ head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
[email protected]:~$

Head can also display the first n bytes.

[email protected]:~$ head -c4 /etc/passwd
[email protected]:~$

tail

You can use tail command to print the last 10 lines of each FILE to standard output.

[email protected]:~$ tail /etc/services
vboxd		20012/udp
binkp		24554/tcp			# binkp fidonet protocol
asp		27374/tcp			# Address Search Protocol
asp		27374/udp
csync2		30865/tcp			# cluster synchronization tool
dircproxy	57000/tcp			# Detachable IRC Proxy
tfido		60177/tcp			# fidonet EMSI over telnet
fido		60179/tcp			# fidonet EMSI over TCP

# Local services
[email protected]:~$

You can give tail the number of lines you want to see.

[email protected]:~$ tail -3 ajax-php-mysql-user-interface.html
[email protected]:~$

The tail command has other useful options, some of which we will use during this course.

cat

The cat command is one of universal tools. When followed by a file-name, this command displays line by line from that file, till the end of the file. If the file is longer than the screen, it will scroll to the end. Without the file-name however this takes its input from the standard input file and writes its output to the standard output file.

[email protected]:~$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
search local
[email protected]:~$

concatenate

cat is short for concatenate. One of the basic uses of cat is to concatenate files into a bigger (or complete) file.

[email protected]:~$ echo one > part1
[email protected]:~$ echo two > part2
[email protected]:~$ echo three > part3
[email protected]:~$ cat part1 part2 part3
one
two
three
[email protected]:~$

create files

You can use cat to create flat text files. Type the cat > winter.txt command as shown in the screenshot below. Then type one or more lines, finishing each line with the enter key. After the last line, type and hold the Control (Ctrl) key and press d.

[email protected]:~$ cat > MyTest/file1
this is demo text
^C
[email protected]:~$ cat MyTest/file1
this is demo text
[email protected]:~$

The Ctrl d key combination will send an EOF (End of File) to the running process ending the cat command.

custom end marker

You can choose an end marker for cat with << as is shown in this screenshot. This construction is called a here directive and will end the cat command.

[email protected]:~$ cat > xyz.txt <<stop
> What is your name?
>  My name is prasanta.
> stop
[email protected]:~$ cat xyz.txt
What is your name?
My name is prasanta.
[email protected]:~$

copy files

In the third example you will see that cat can be used to copy files. We will explain in detail what happens here in the bash shell chapter.

[email protected]:~$ cat xyz.txt
What is your name?
My name is prasanta.
[email protected]:~$ cat xyz.txt > mno.txt
[email protected]:~$ cat mno.txt
What is your name?
My name is prasanta.
[email protected]:~$

tac

The tac (as the opposite of cat) command is used to concatenate and print files in reverse.

[email protected]:~$ cat count
one 
two
three 
four

[email protected]:~$ tac count

four
three 
two
one 
[email protected]:~$

more and less

The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit. Some people prefer the less command to more.

strings

With the strings command you can display readable ascii strings found in (binary) files. This example locates the ls binary then displays readable strings in the binary file (output is truncated).

[email protected]:~$ which ls
/bin/ls
[email protected]:~$ strings /bin/ls | more
/lib/ld-linux.so.2
,cr<
libselinux.so.1
_ITM_deregisterTMCloneTable
__gmon_start__
_Jv_RegisterClasses
_ITM_registerTMCloneTable
_init
fgetfilecon
freecon
lgetfilecon
_fini
libacl.so.1
...

Exercise, Practice and Solution :

1. Display the first 15 lines of /etc/services.

Code:

head -15 /etc/services

2. Display the last line of /etc/passwd.

Code:

tail -1 /etc/passwd

3. Use cat to create a file named abc.txt that looks like this:

One
Two
Three
Four
Five

Code:

cat abc.txt
One
two
Three
Four
Five
(followed by Ctrl-d)

4. Use cp to make a backup of this file to xyz.txt.

Code:

cp abc.txt xyz.txt

5. Use cat to make a backup of this file to grt.txt.

Code:

cat abc.txt > grt.txt

6. Display abc.txt, but with all lines in reverse order (the last line first).

Code:

tac abc.txt

7. Use more to display /var/log/messages.

Code:

more /var/log/messages

8. Display the readable character strings from the /usr/bin/passwd command.

Code:

strings /usr/bin/passwd

9. Use ls to find the biggest file in /etc.

Code:

ls -lrS /etc

10. Use cat to create a file named summer.txt that contains the contents of summer.txt followed by the contents of /etc/passwd.

Code:

cat /etc/passwd  summer.txt

11. Use cat to create a file named summer.txt that contains the contents of summer.txt preceded by the contents of /etc/passwd.

Code:

mv summer.txt tmp.txt ; cat /etc/passwd tmp.txt > summer.txt

Previous: Linux - Working with files
Next: Linux - file tree