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.

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

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

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

Head can also display the first n bytes.

datasoft@datasoft-linux:~$ head -c4 /etc/passwd
rootdatasoft@datasoft-linux:~$

tail

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

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

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

datasoft@datasoft-linux:~$ tail -3 ajax-php-mysql-user-interface.html
datasoft@datasoft-linux:~$

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.

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

concatenate

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

datasoft@datasoft-linux:~$ echo one > part1
datasoft@datasoft-linux:~$ echo two > part2
datasoft@datasoft-linux:~$ echo three > part3
datasoft@datasoft-linux:~$ cat part1 part2 part3
one
two
three
datasoft@datasoft-linux:~$

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.

datasoft@datasoft-linux:~$ cat > MyTest/file1
this is demo text
^C
datasoft@datasoft-linux:~$ cat MyTest/file1
this is demo text
datasoft@datasoft-linux:~$

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.

datasoft@datasoft-linux:~$ cat > xyz.txt <<stop
> What is your name?
>  My name is prasanta.
> stop
datasoft@datasoft-linux:~$ cat xyz.txt
What is your name?
My name is prasanta.
datasoft@datasoft-linux:~$

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.

datasoft@datasoft-linux:~$ cat xyz.txt
What is your name?
My name is prasanta.
datasoft@datasoft-linux:~$ cat xyz.txt > mno.txt
datasoft@datasoft-linux:~$ cat mno.txt
What is your name?
My name is prasanta.
datasoft@datasoft-linux:~$

tac

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

datasoft@datasoft-linux:~$ cat count
one 
two
three 
four

datasoft@datasoft-linux:~$ tac count

four
three 
two
one 
datasoft@datasoft-linux:~$

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).

datasoft@datasoft-linux:~$ which ls
/bin/ls
datasoft@datasoft-linux:~$ 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



Follow us on Facebook and Twitter for latest update.