Viewing and editing files

Viewing and editing files in a Linux shell.

Display the contents of a file

Full contents

The cat <file> command can be used to display the entire contents of a file in the Terminal application.

For instance:

cat file2.csv 

Displaying the contents of a file.

First lines

The head <file> command be used to display the first few lines of a file.

The number of lines displayed can be controlled using the -n option. In the absence of option, the first 10 lines are shown by default.

Example usage:

head file2.csv
head -n 3 file2.csv

Displaying the first lines of a file.

Last lines

The tail <file> command be used to display the last few lines of a file.

This command works very much like the head command.

tail -n 3 file2.csv

Displaying the lanes lines of a file.

Interactively scroll through files

more

The more <file> command can be used to scroll through a file unidirectionally from top to bottom.

Interactively scrolling through a file using the &lsquo;more&rsquo; command.

While the viewer is active in a Terminal application, pressing the Space bar scrolls down one screen worth down the contents of the file.

In the bottom left corner of the Terminal application, a status bar indicates the fraction of the file that has been scrolled through.

Once the end the file is reached, the interactive viewer automatically terminates and returns the Linux prompt to the user.

Alternatively, pressing the Q key can be used to close the interactive viewer before the end of the file is reached.

less

The less <file> command provides similar yet more extensive functionality over the more command (“less is more”).

Interactively scrolling through a file using the &rsquo;less&rsquo; command.

While the viewer is active, the Up and Down arrow keys can be used to scroll one line up or down the contents of the file.

The Left and Right arrow keys can be used to scroll one screen worth left or right across the contents of the file.

Similarly to the more command, the Space bar can be used to scroll one screen worth down the contents of the file.

The Q key must be pressed to close the interactive viewer (reaching the end of the file will not automatically close the interactive viewer).

Editing files in the Terminal

nano

The nano <file> command can be used to open an interactive text editor in the Terminal application.

nano file2.csv

The &rsquo;nano&rsquo; text editor.

In particular:

  • The arrow keys can be used to move the cursor through the file.
  • Common keyboard shortcuts are displayed at the bottom of the editor.
  • The ^ symbol represents the Control key.
  • For instance, ^X indicates that pressing the Control and X keys simultaneously will exit the interactive text editor.

To save edits made to a file:

  • Press the Control and X keys simultaneously to initiate the exit from the editor.
  • When prompted whether to ‘Save modified buffer’, press Y to confirm.
  • When prompted for the ‘File Name to Write’, immediately press the Return key to use the current file name.

Saving changes in the &rsquo;nano&rsquo; text editor.

To close a file without saving the edits:

  • Press the Control and X keys simultaneously to initiate the exit from the editor.
  • When prompted whether to ‘Save modified buffer’, press N to discard the changes.

To save the new version of the file under a different name:

  • Press the Control and X keys simultaneously to initiate the exit from the editor.
  • When prompted whether to ‘Save modified buffer’, press Y to confirm.
  • When prompted for the ‘File Name to Write’, edit the file name as needed, and press the Return key to write the modified file under the new filename. The original file will be left unchanged.

emacs

The emacs <file> command can be used to open an interactive text editor in the Terminal application.

The &rsquo;emacs&rsquo; text editor.

In particular:

  • The arrow keys can be used to move the cursor through the file.
  • The GNU Emacs Reference Card list many keyboard shortcuts that make Emacs one of the most efficient text editors in Terminal applications.

To save edits made to a file:

  • Press the Control and X keys simultaneously to initiate a command, then Control and S simultaneously to save the file.

Saving changes in the &rsquo;emacs&rsquo; text editor.

To close a file:

  • Press the Control and X keys simultaneously to initiate a command, then Control and C simultaneously to close the file.

vim

The vim <file> command can be used to open an interactive text editor in the Terminal application.

The &lsquo;vim&rsquo; text editor.

In particular:

  • The arrow keys can be used to move the cursor through the file.
  • The editor initially opens the file in Read-only mode. To edit the file, press the I key to enter Edit mode.
  • To disable Edit mode, press the Esc key to return to Read-only mode.

To save edits made to a file:

  • Press the : key to open a prompt for commands within the editor. Then, type wq to write the file and quit the application (press the Return key to execute the command).

Saving changes in the &lsquo;vim&rsquo; text editor.

To close a file without saving the edits:

  • Press the : key to open a prompt for commands within the editor. Then, type q! force quit the application (the ! symbol forces the command to override warnings of unsaved changes).

Creating new files

Text editors such as nano, emacs, and vim can be used to create new files, immediately opening those files in an interactive text editor.

To create new files, those commands must be given a filename that does not exist yet.

For instance:

nano new_file.txt

Creating a new file using &rsquo;nano&rsquo;.