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, andqto quit. Recommended for long files.head filename– Displays the first 10 lines of a file. Add-nto specify a different number (e.g.,head -n 20 filename).tail filename– Shows the last 10 lines of a file. Great for checking logs. Usetail -f filenameto 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#
See also
Introduction to the Text Editor: Nano (YouTube)
For quick edits, nano is a simple and beginner-friendly text editor:
Open a file:
nano filenameEdit text directly in the terminal.
Use
Ctrl + Oto save,Ctrl + Xto exit, andCtrl + Kto cut a line.
Nano is available by default on most Linux systems and is easy to use, especially for newcomers.
Editing Files with Vim#
See also
Introduction to the Text Editor: Vim (YouTube)
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 filenameEnter insert mode (to type): press
iReturn to normal mode: press
EscSave and quit: type
:wqthenEnterQuit without saving: type
:q!thenEnter