Viewing and editing files
Viewing files on the command line
Very often the contents of a text file need to be inspected. There are two useful commands for that in Linux.
Dump contents to screen: cat
The first command is cat
. You need to specify the filename as an argument:
username@habrok:zip-3.0 cat LICENSE This is version 2007-Mar-4 of the Info-ZIP license. The definitive version of this document should be available at ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely and a copy at http://www.info-zip.org/pub/infozip/license.html. Copyright (c) 1990-2007 Info-ZIP. All rights reserved. .... ....
This is a quick way to look into a file, but if the file is longer than a few lines it will just scroll by in your terminal window.
Scroll and search in a file: less
For longer files the command less
is useful.
username@habrok:zip-3.0 less LICENSE
This will open the file for reading in your terminal, starting at the top of the file.
This is version 2007-Mar-4 of the Info-ZIP license. The definitive version of this document should be available at ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely and a copy at http://www.info-zip.org/pub/infozip/license.html. Copyright (c) 1990-2007 Info-ZIP. All rights reserved. For the purposes of this copyright and license, "Info-ZIP" is defined as the following set of individuals: Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois, Jean-loup Gailly, Hunter Goatley, Ed Gordon, Ian Gorman, Chris Herborth, Dirk Haase, Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz, David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko, Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs, Kai Uwe Rommel, Steve Salisbury, Dave Smith, Steven M. Schweda, Christian Spieler, Cosmin Truta, Antoine Verheijen, Paul von Behren, LICENSE
When the window is open you can use the arrow keys on your keyboard to scroll up and down. You can also use the PageUp and PageDown keys.
Another useful feature is searching for text using the '/' key. If you for example type: /software
followed by the Enter key, the text software will be searched for and highlighted.
When you want to move to the next occurrence you use the 'n' key. To go back to the preivous occurrence you can use uppercase 'N'.
The question you now probably have is how to leave less. This can be achieved by using the 'q' (short for quit) key.
Editing files
MobaXterm editor
For editing files the MobaXterm program can be used. The program includes a file browser interface on the left side of the window.
When using the right mouse button on a file the option “Open with default text editor” will appear. This will temporarily store the file on your computer and fire up the MobaXterm text editor, which can be used to modify the file.
When clicking on 'save' the following window will appear.
After selecting 'Yes' the file will be copied back to Hábrók.
Note that, due to the temporary copy that has to transferred from and to your computer, this can take a while for larger files.
Command line editor: nano
There are several editors available for the Linux command line, which can be useful when you have to make a quick/simple change, or if you want to change a large file. In contrary to the graphical MobaXterm editor, the file does not have to be temporarily copied to your local machine in order to edit it.
The main advantage of nano
is that it is a relatively simply to use editor, available on the command line. So you can always use this if other options are not available.
nano
can be started with the name of a file as an argument. nano
will open this file, or start a new one if the file does not exist. So we can use the following to create a jobscript named mandelbrot.sh
:
[username@habrok mandelbrot]$ nano mandelbrot.sh
This will start nano, and you will be presented with the following screen:
GNU nano 2.3.1 File: mandelbrot.sh [ New File ] ^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell
In this screen you can just start typing at the cursor. You can move around the cursor using the arrow keys (given that there is text to move to). Note that the mouse will not work!
The main commands are listed at the bottom of the screen. The '^' symbol means the 'Ctrl' key. So 'Ctrl' + 'x' means Exit. 'Ctrl' + 'o' will write the file to disk (normally denoted as 'Save'). 'Ctrl' + 'G' will display some useful help information. This includes the way to search for text, the way to delete lines and much more.
End of line differences
A small problem you may run into is that there is a difference between Linux and Windows in the way the “end of line” is represented. Windows represents the “end of line” by two characters, namely “carriage return” and “linefeed” (CRLF), where Linux uses a single “linefeed”. When editing a file created on Linux with e.g. Notepad on Windows, the file may appear as a single line of text with characters where the line breaks should be. A file created on Windows may appear to have extra “^M” characters at the line break positions on Linux systems.
Many current applications do not have problems recognizing the different form of “end of line”. Note, however, that most shell interpreters like bash or csh will have problems when the wrong “end of line” characters are used.
A file with the Windows CRLF end of line can be detected on Linux by using the command file
. A Linux text file will result in the following output:
file testfiletestfile: ASCII text
A Windows text file will give the following:
file testfile testfile: ASCII text, with CRLF line terminators
A way to solve the problem is by using the tools dos2unix
or unix2dos
. To convert a file in the MS Windows format dos2unix
can be used like:
dos2unix yourfile
Where yourfile
should be the name of your file.
Alternatively, you could use MobaXterm's file editor and use the buttons in the toolbar (with the logos of the different operating systems) to convert one format to another.
Exercises
It is also good to now try out the first three exercises of the Basic Hábrók course at Exercises basic course This should help you getting familiar with the commands and tools that we've described in the last sections.
Next section: Software environment