LFCS - Editing and Creating Files with Nano and Touch

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
340
Reaction score
367
Credits
11,754
In the LFCS exam you may be asked to create or modify files. One simple way is to use the program ‘Nano’. Other programs can be used, but it is up to you which program you use. Remember that the point of the exam is not to use any specific tool, but to get the task completed that you are given.

It is also important to not waste too much time on getting tasks completed. Do the task correctly and move on to the next job given to you to perform.

Before covering the commands of ‘touch’ and ‘nano’ it would be best to cover file timestamps.

Timestamps

Every file and folder on a system has a timestamp. Actually, there are three timestamps for a file:
  1. Access Time (atime)
  2. Change Time (ctime)
  3. Modification Time (mtime)
To see the timestamps for a file you can use the command:

stat [file name]

The output will be similar to what is shown in Figure 1.

Figure 01.jpg

FIGURE 1

The Access Time is the date and time when the file was last read. The Change Time is when the file permissions have been modified as well as the file itself. The Modification Time is changed when the file is modified. In most cases the Change and Modification Times will be the same. Now that you understand what the timestamps are about we can continue with the ‘touch’ command.

Touch

The ‘touch’ command is a simple one to use. Basically, you can create empty files with ‘touch’ or even update or set Timestamps.

To create a file use the command:

touch [file name]

The file name can include a folder to place the new file in the specific folder. For example, you can create a file named ‘empty.txt’ in the folder ‘/tmp’ with the command ‘touch /tmp/empty.txt’. An empty file will be created if the file name given does not exist. If the file name does exist then the timestamps will be modified and the file contents will remain as they were.

If you want to change only the Access time of a file, then use the option ‘-a’. The following command will change the Access Time of the file ‘empty.txt’ to the current Date and time.

touch -a empty.txt

To change only the Change Time use he parameter ‘-m’. The following command will change the Modification Time of the file ‘empty.txt’ to the current Date and time.

touch -m empty.txt

You can set a specific Date and Time with the parameter ‘-d’ and specifying the Date and Time in single quotes. The Date and time will be changed for both the Modification and Change Times. To set the Date and Time to October 23, 1974 at 2:48 P.M. for the file ‘empty.txt’ the command is:

touch -d '23 October 1974 14:48 empty.txt

NOTE: The Time is given in military Time, or the 24 hour clock.

If you want to specify to only change the Modification or Change Time only then add in the specific option for the required time. For instance, to only change the Access Time to October 23, 1974 at 2:48 P.M. for the file ‘empty.txt’ the command is:

touch -a -d '23 October 1974 14:48' empty.txt

To see the version of your ‘touch’ command file you can use the command:

touch --version

The ‘touch’ command is for creating a file or changing the timestamps, but if you want to edit the file contents you need to use an editor like ‘nano’.

Nano

If you do not have ‘nano’ installed on your system then you can run the following commands for your Linux distro:

Debian: sudo apt-get install nano
Red Hat: sudo yum install nano

‘Nano’ is a basic text editor and can be seen in Figure 2.

Figure 02.jpg

FIGURE 2

There is a basic menu at the bottom of the screen. The commands are:
  • ^G – Get help
  • ^O – Write out the file (save)
  • ^W – Where is (find)
  • ^K – Cut Text (remove line)
  • ^J - Justify
  • ^X – Exit
  • ^R – Read (insert another file)
  • ^\ - Replace (find and replace)
  • ^U – Uncut Text (paste line)
  • ^T – To Spell (spell check)
If you do not want to see the two menu lines at the bottom then you should add the option ‘-x’ when starting ‘nano’.

The mouse does not allow you to use it to interact with the ‘nano’ interface. To use the mouse to move the cursor as needed use the option ‘-m’ when starting ‘nano’.

To open the file ‘empty.txt’ in the current folder with the use of the mouse the command is:

nano -m empty.txt

The best option is to learn the use of ‘touch’ as well as edit files with ‘nano’ and use the menu items as you can. You will need to be proficient with both for the creation and editing of files. The ‘nano’ editor is very useful when modifying configuration files.
 

Staff online

Members online


Top