Where technology and gadgets come together and play

Beginners guide to Reading and Finding Files in Linux

By on in Linux

723 words, estimated reading time 4 minutes.

Introduction to Linux Series
  1. Linux Tips for Beginners
  2. Beginners guide to Reading and Finding Files in Linux
  3. Using Grep to Search for Text in Linux
  4. Understanding Linux File Permissions
  5. How to Archive, Compress and Extract files in Linux
  6. Linux Piping and Redirection
  7. Linux Hardlinks and Softlinks
  8. Basic Data Recovery in Linux
  9. Essential Guide to Working with Files in Linux
  10. Apache Administration on Linux
  11. MySql Administration on Linux

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.

Reading Files

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

cat /etc/hosts

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

cat /etc/hosts /etc/hostname

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


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.

Last updated on: Saturday 17th June 2017



Have a question or suggestion? Please leave a comment to start the discussion.


Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.