Viewing and Editing Files#

When working on research projects or data analysis in Linux, it’s common to view or edit text files directly from the command line. Here’s a quick guide to the basic tools you’ll encounter.

File Viewing Tools#

These commands allow you to view the contents of a file without opening it in a full editor:

  • cat filename – Outputs the entire file to the terminal. Best for small files.

  • less filename – Opens the file in a scrollable view. Use arrow keys to navigate, / to search, and q to quit. Recommended for long files.

  • head filename – Displays the first 10 lines of a file. Add -n to specify a different number (e.g., head -n 20 filename).

  • tail filename – Shows the last 10 lines of a file. Great for checking logs. Use tail -f filename to follow a log file in real time as it grows. See Launching Jobs on KLC for how to create log files when running scripts interactively.

Text Editors#

Text editors are important for writing batch scripts in high-performance computing environments. Users can choose between Nano (a beginner-friendly text editor) or Vim (a more powerful editor, especially in programming or large-scale text processing, but harder to learn) as their text editor on KLC.

Editing Files with Nano#

For quick edits, nano is a simple and beginner-friendly text editor:

  • Open a file: nano filename

  • Edit text directly in the terminal.

  • Use Ctrl + O to save, Ctrl + X to exit, and Ctrl + K to cut a line.

Nano is available by default on most Linux systems and is easy to use, especially for newcomers.

Editing Files with Vim#

Vim is a modal editor — it has separate modes for navigating and inserting text. It has a steeper learning curve than Nano but offers extensive keyboard shortcuts and scripting capabilities that make it a powerful choice for programming and large-scale text editing.

Basic Vim commands to get started:

  • Open a file: vim filename

  • Enter insert mode (to type): press i

  • Return to normal mode: press Esc

  • Save and quit: type :wq then Enter

  • Quit without saving: type :q! then Enter