Learning never exhausts the mind
Home >  Technology > Linux > Beginners guide to Reading and Finding Files in Linux

Published 21st August 2016 by

Linux contains many, many, ways for accessing files, reading and writing to them. In this article, we look at a few ways to read and write files using basic Linux commands.
Introduction to Linux Series
  1. Installing Linux Step by Step
  2. Linux Tips for Beginners
  3. Beginners guide to Reading and Finding Files in Linux
  4. Using Grep to Search for Text in Linux
  5. Understanding Linux File Permissions
  6. How to Archive, Compress and Extract files in Linux
  7. Linux Piping and Redirection
  8. Linux Hardlinks and Softlinks
  9. How to Create and Use Bash Scripts
  10. Basic Data Recovery in Linux
  11. Apache Administration on Linux
  12. MySql Administration on Linux
  13. Switching from Windows to Linux

Reading Files

These are easy commands for reading short files and displaying the contents on the command screen.

cat /etc/hosts	localhost	Megatron

You can also use this command to show the output of two or more files in the same command.

cat /etc/hosts /etc/hostname	localhost	Megatron


There is a nifty command which will show you the word count of a file, and also the line count.

  • wc /etc/services - Word Count for file
  • wc -l /etc/services - Line Count for file

These commands work for very small files, with no more than a few lines. For larger files, we can use the head and tail commands. These commands show the first n lines and last n lines respectively.

tail /var/log/apache/access.log

This command will show the last 10 lines from the Apache access log. Both commands have the same parameters, the most often used is the -n option to change the number of lines shown.

tail -n 20 /var/log/apache/access.log

This command shows the last 20 lines.

For viewing larger files in a friendly screen which allows you to page up and page down, the less commands is most often used. Less allows paging through the file using the page down and page up keys. It also allows for searching.

less /var/log/apache/access.log
hit / to search for text
  :?  to search backwards
:n gives next match
:q to quit

Finding Files

In Linux there are also many commands for searching for files on a hard drive. The most commonly used is the find command. This command will recursively scan a directory for matching files.

To find all the PDF documents in /usr/share/doc use this command:

find /usr/share/doc -name '*.pdf'

You can also use this command to perform actions on the file, for example find all the PDF documents and copy them to another directory.

find /usr/share/doc -name '*.pdf' -exec cp {} . ;

This will find all the PDF files in /usr/share/doc and execute the copy command. the {} represent the current matching file and the dot is the current directory. The line is terminated with an escaped semicolon. If you don't specify the folder in the find command it will start from the current working directory.

You can also delete files using the find command

find -name '*.pdf' -delete

You can even search for large files taking up space on your hard drive.

find /boot -size +10000k -type f -exec du -h {} ;

This will show all the files above 20 MB of type file, outputted through to the du command

38M	/boot/initrd.img-4.4.0-21-generic

Comparing Files with Diff

Sometimes you may need to compare two files to see if there are any differences or if they are the same. This can be done using the diff command. For this example, I'll create two test files and show cat output of each.

diff file1 file2
< 1
> 2
> it is different

This indicates that line 6 in both files has changed and the second file has an additional 7th line. The left angle bracket indicates a change in the first file (left) and the right angle bracket a change in the second file (right). So, line 6 changed from 1 to 2, and the second file adds "it is different".

The code 6c6 gives the left line number, an action code and the right line number. The action codes are as follows:

  • a - add
  • d - delete
  • c - change

Compare Binary Files

Comparison of binary files cannot be done visually like this due to binary nature, the output is not meaningful and very difficult to understand. Instead, we can compare the md5 checksum hash of a file. This is useful when downloading software from the internet, to compare the download files checksum with the published value to identify if the download has completed successfully or if it has been modified from the original.

md5sum plexmediaserver_0. 
9a6f3486be986bc7f8b6f4c3de2c20d0  plexmediaserver_0.

If the two checksums are the same then the file is correct, if there is a difference then the file has been altered in some way, either an incomplete download, corrupted or modified.

Tutorial Series

This post is part of the series Introduction to Linux. Use the links below to advance to the next tutorial in the couse, or go back and see the previous in the tutorial series.

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.