Linux File Content

There are many commands which help to look at the contents of a file. Now we’ll look at some of the commands like head, tac, cat, less & more and strings.

 

Head

The ‘head’ command displays the starting content of a file. By default, it displays starting 10 lines of any file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
head {filename}
// -n option followed by an integer specifying the number of lines to be shown.
head -n {number} {filename}
// show content of multiple files to n line
head -n {number} {filename1} {filename2}
// -c option allows to print a specific number of bytes
head -c {number} {filename}
// show content of multiple files to number of bytes
head -c {number} {filename1} {filename2}
head {filename} // -n option followed by an integer specifying the number of lines to be shown. head -n {number} {filename} // show content of multiple files to n line head -n {number} {filename1} {filename2} // -c option allows to print a specific number of bytes head -c {number} {filename} // show content of multiple files to number of bytes head -c {number} {filename1} {filename2}
head {filename}

// -n option followed by an integer specifying the number of lines to be shown.
head -n {number} {filename} 

// show content of multiple files to n line 
head -n {number} {filename1} {filename2} 

// -c option allows to print a specific number of bytes 
head -c {number} {filename} 

// show content of multiple files to number of bytes 
head -c {number} {filename1} {filename2}

Examples

head {filename1} {filename2}…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % head test.txt test2.txt
==> test.txt <==
test
test1
sdf
ewr
sdfb
dcxzv
xcv
x
==> test2.txt <==
wer
qwe
rq
wer
qwe
rqwe
r
qwe
rq
wer
folaukaveinga@Folaus-MacBook-Pro-3 files % head test.txt test2.txt ==> test.txt <== test test1 sdf ewr sdfb dcxzv xcv x ==> test2.txt <== wer qwe rq wer qwe rqwe r qwe rq wer
folaukaveinga@Folaus-MacBook-Pro-3 files % head  test.txt test2.txt 
==> test.txt <==
test
test1
sdf
ewr

sdfb
dcxzv

xcv
x

==> test2.txt <==
wer
qwe
rq
wer
qwe
rqwe
r
qwe
rq
wer

 

head -n {filename1} {filename2}…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt
test
test1
sdf
ewr
folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt test2.txt
==> test.txt <==
test
test1
sdf
ewr
==> test2.txt <==
wer
qwe
rq
wer
qwe
folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt test test1 sdf ewr folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt test2.txt ==> test.txt <== test test1 sdf ewr ==> test2.txt <== wer qwe rq wer qwe
folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt
test
test1
sdf
ewr

folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt test2.txt 
==> test.txt <==
test
test1
sdf
ewr


==> test2.txt <==
wer
qwe
rq
wer
qwe

head -c {number} {filename1} {filename2}…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt
test
test1% folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt test2.txt
==> test.txt <==
test
test1
==> test2.txt <==
wer
qwe
rq%
folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt test test1% folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt test2.txt ==> test.txt <== test test1 ==> test2.txt <== wer qwe rq%
folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt         
test
test1%                                                                                                                                                                                                folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt test2.txt
==> test.txt <==
test
test1
==> test2.txt <==
wer
qwe
rq%

 

Tail

Linux tail command is used to display the last ten lines of one or more files. Its main purpose is to read the error message. By default, it displays the last ten lines of a file. Additionally, it is used to monitor the file changes in real-time. The default use of the tail command displays the last ten lines of the files.

tail -n {number} {filename1} {filename2}…

Prints the last ‘n’ lines. n is mandatory to be specified in command otherwise it displays an error

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -n 5 test.txt
asdf
asd
fasd
fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -n 5 test.txt asdf asd fasd fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -n 5 test.txt 
asdf
asd
fasd
fasd

tail -c {number} {filename1} {filename2}…

The ‘-c’ option displays the specified number of content for bytes from the last line.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -c 10 test.txt
asd
fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -c 10 test.txt asd fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -c 10 test.txt 
asd
fasd

tail -f {number} {filename1} {filename2}…

This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added. As new lines are written to the log, the console will update with the new lines.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -f test.txt
f
sad
f
i
asdf
asdf
asd
fasd
fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -f test.txt f sad f i asdf asdf asd fasd fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % tail -f test.txt 
f
sad
f
i
asdf
asdf
asd
fasd
fasd



Cat

The ‘cat’ command can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files, display the line number, display $ at the end of the line, etc.

 

cat {filename}

display all content of a file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test.txt
test
test1
sdf
ewr
sdfb
dcxz
fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test.txt test test1 sdf ewr sdfb dcxz fasd
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test.txt 
test
test1
sdf
ewr
sdfb
dcxz
fasd

cat > {filename}

create a file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat > test1.txt
easdf
asd
fasd
fasdf
^C
folaukaveinga@Folaus-MacBook-Pro-3 files % head test1.txt
easdf
asd
fasd
fasdf
folaukaveinga@Folaus-MacBook-Pro-3 files % cat > test1.txt easdf asd fasd fasdf ^C folaukaveinga@Folaus-MacBook-Pro-3 files % head test1.txt easdf asd fasd fasdf
folaukaveinga@Folaus-MacBook-Pro-3 files % cat > test1.txt
easdf
asd
fasd
fasdf
^C
folaukaveinga@Folaus-MacBook-Pro-3 files % head test1.txt 
easdf
asd
fasd
fasdf

cat >> {filename}

>> appends input to the end of the file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat >> test1.txt
test123
sdfsdf^C
folaukaveinga@Folaus-MacBook-Pro-3 files % cat >> test1.txt test123 sdfsdf^C
folaukaveinga@Folaus-MacBook-Pro-3 files % cat >> test1.txt 
test123
sdfsdf^C

 

cat {filename1} {filename2}… > {newfilename}

copy contents of one file(filename1} to another file {newfilename}

with (-) a new line will be inserted while concatenating multiple files by using a hyphen (-).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt > test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test test.txt test1.txt test2.txt test3.txt test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % head test4.txt
easdf
asd
fasd
fasdf
test123
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt test2.txt > test5.txt
// add new line \n to the end of test.txt content
folaukaveinga@Folaus-MacBook-Pro-3 files % cat - test1.txt test2.txt > test5.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt > test4.txt folaukaveinga@Folaus-MacBook-Pro-3 files % ls test test.txt test1.txt test2.txt test3.txt test4.txt folaukaveinga@Folaus-MacBook-Pro-3 files % head test4.txt easdf asd fasd fasdf test123 folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt test2.txt > test5.txt // add new line \n to the end of test.txt content folaukaveinga@Folaus-MacBook-Pro-3 files % cat - test1.txt test2.txt > test5.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt > test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test		test.txt	test1.txt	test2.txt	test3.txt	test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % head test4.txt 
easdf
asd
fasd
fasdf
test123

folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt test2.txt > test5.txt

// add new line \n to the end of test.txt content
folaukaveinga@Folaus-MacBook-Pro-3 files % cat - test1.txt test2.txt > test5.txt

 

cat -n {filename}

-n shows the number line

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -n test5.txt
1 easdf
2 asd
3 fasd
4 fasdf
5 test123
6 123
7 123
8 s
9 df
10 asd
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -n test5.txt 1 easdf 2 asd 3 fasd 4 fasdf 5 test123 6 123 7 123 8 s 9 df 10 asd
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -n  test5.txt
     1	easdf
     2	asd
     3	fasd
     4	fasdf
     5	test123
     6	123
     7	123
     8	s
     9	df
    10	asd

 

cat -e {filename}

The ‘cat-e’ option displays a ‘$’ sign at the end of every line. This is very useful when you are looking for the end of a line.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -e test.txt
test$
test1$
sdf$
ewr$
sdfb$
dcxzv$
$
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -e test.txt test$ test1$ sdf$ ewr$ sdfb$ dcxzv$ $
folaukaveinga@Folaus-MacBook-Pro-3 files % cat -e test.txt 
test$
test1$
sdf$
ewr$
sdfb$
dcxzv$
$

cat {filename} | more

This gives you an option to paginate what to display.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat test.txt | more
cat test.txt | more
cat test.txt | more

 

 

sort {filename}

Sort content of file alphabetically.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
folaukaveinga@Folaus-MacBook-Pro-3 Downloads % sort test.txt
as
asd
asd
asd
asd
asd
asdf
asdf
folaukaveinga@Folaus-MacBook-Pro-3 Downloads % sort test.txt as asd asd asd asd asd asdf asdf
folaukaveinga@Folaus-MacBook-Pro-3 Downloads % sort test.txt 

as
asd
asd
asd
asd
asd
asdf
asdf

 

 

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *