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

Beginners guide to Reading and Finding Files in Linux

By Tim Trott, 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 though 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 semi-colon. 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


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

Leave a Reply

Your email address will not be published.