Lesson 4 – Viewing/Searching for Files

The more Command

Use the more command to read a file (without having the edit option):

    >more filename.txt
  • To scroll down one line at a time, use the “Enter” button on your keyboard.
  • To scroll down one page at a time, use the space bar on your keyboard.
  • To exit out of the command, hit the “q” on your keyboard.

The tail Command

Use the tail command to read the end of a file (without having the edit option):

    >tail filename.txt

The default is to show you the last 10 lines of the file. If you would like to see a different number of lines, use the command in the following format:

    >tail -50 filename.txt

The command above will show you the last 50 lines of the file, named “filename.txt“.

If you want to read the end of a file that is added to often (for instance, a log file), you can add the “-f” switch, to be able to see the new entries as it is updated:

    >tail -f filename.txt

The find Command

There are many ways to use the find command to search for files. You can search according to the name of the file, the date it was last modified, the size of the file, the owner of the file, etc. The command below will search in my home directory for a file called, “filename.txt“:

    >find ~ -name filename.txt -print

The format of this command is:

    >find directory_path -name filename -print

where you fill in the “directory_path” that you would like the search to start in (the find command is recursive, which means that it will continue searching the subdirectories until all filenames have been checked) and “filename” is the name of the file you are looking for (remember that Linux is case sensitive, so it will only search for files named “filename“, and not files named “Filename“; you also have to make sure that you have the complete name listed, so if you are not sure about the complete name, you will have to use wildcards. So, if you want to find all the files that begin with the letters “file“, you will put “file*” (with quotes) as your filename.). For more information about the searches you can perform with the find command, read the man pages:

    >man find

The grep Command

The grep command lets you search for a certain word or phrase inside a file or files. The format of the command is the following:

    >grep "phrase to search for" filename

where “phrase to search for” is in quotes and is what you are searching for and “file” is the name of the file that you are searching within. Once again, case matters (unless you use the “-i” switch “grep -i "word" file“). The default output is the line(s) that contain the text you are looking for. As always, check out the man pages for more information.

Continue to Lesson 5 – Editing Files
Return to index of lessons.

Last revised May 9, 2002.