Technology blog::Life hacks::Linux::Hardware::Gaming

Beginners guide to Reading and Finding Files in Linux

How to read, write and find files within Linux

By , 21st August 2016 in 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.

Further Reading

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.