Course


Display contents of a file

How to display the contents of a file in Linux?

Displaying the contents of a file

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type clear. This will clear all text and leave you with the prompt at the top of the window.

  • Screencast Interactive Shell

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type: cat file if the file is longer than than the size of the window, so it scrolls past making it unreadable.

less

The command less writes the contents of a file onto the screen a page at a time. Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to cat for long files.

  • Screencast Interactive Shell

head

The head command writes the first ten lines of a file to the screen.

Bash (5.0.0)
  • Input  

By default the head command will only print the first few (10) lines, if you want more pass the command with option e.g, head -n 20 file.

tail

The tail command writes the last ten lines of a file to the screen. It will take similar options as the head.

Bash (5.0.0)
  • Input  

Wildcards

The * wildcard

The character * is called a wildcard, and will match against none or more characters in a file or directory name. For example, type:

Bash (5.0.0)
  • Input  

The ? wildcard

The character ? will match exactly one character.

Bash (5.0.0)
  • Input  

In naming files, characters with special meanings such as / * & % , should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with _ underscore and . dot`.

Scientific Computing Essentials