Sunday 7 February 2016

How to create a TEXT FILE using terminal and how to use the commands cp, mv & more

This post shows you how you can create text files using terminal and text editor and the uses of cp, mv and more commands.

First of all to create or edit text files you need a text editor installed in your Linux system. In Linux Mint the text editor 'pluma' and in Ubuntu the text editor 'gedit' are installed by default.

To create a text file:

1. Open the terminal by pressing Ctrl+Alt+T.
2. When the terminal shows up type the name of the text editor installed in your system followed by the name you want to give to your text file and press Enter.
Eg. FOR LINUX MINT: pluma mytext
FOR UBUNTU: gedit mytext
3. The text editior will open up and you can type your text into it and save it either by going to FILE-> SAVE or by pressing Ctrl+S.
4. Close the file (Alt+F4)

Using cp (Copy) command:

The cp command is used for copying the contents of one file to another.
  1.  Open the terminal and create two text files called mytext1 and mytext 2 in your home directory by following the above steps. Enter some data into mytext1 and mytext2.
  2.  Open a new terminal (Crtl+Alt+T) and type ls . This will list the names of the files in your home directory. Check if mytext1 and mytext2 are present in the list.
  3.  Now type cp followed by the name of the text file to be copied followed by the name of the destination text file where it has to be copied and press enter.
  4. Eg. cp mytext1 mytext2 -This will copy the contents of the file mytext1 to the file mytext2
  5.  Now check the contents of the files: TO CHECK THE CONTENTS OF A FILE:

  • (i) You can either open the file in the text editor by typing the name of the text editor followed by the file name. Eg. pluma mytext1 or gedit mytext1  OR
  • (ii) You can use the 'more' command to view the contents of the files in the terminal itself. Type more followed by the name of the file. Eg. more mytext1 -This will display the contents of mytext1 in the terminal window.  more mytext2 -This will display the contents of mytext2 in the terminal window.

 Check the contents of the files mytext1 and mytext2 by either of the two methods explained above and you will find that the contents of mytext1 is copied to mytext1 and hence both the files have the same data.

Using mv (Move) command:

The move command is used to move the contents of one file to another. Unlike the cp command mv commands overwrites the contents of the destination file.
Follow the first two steps of the above (cp) process to create two files (say newtext1 and newtext2)
  1.  Type mv followed by the name of the file to be moved followed by the name of the destination file and press Enter.      Eg. mv newtext1 newtext2
  2. Now using ls check the files present in your home directory. You can also check using the GUI as you usually do.
  3.  You will find that the file newtext1 does not exist anymore but its contents are moved to newtext2 overwriting the contents of newtext2.
  4. Use the more command to check it. Eg. more newtext2
For more detailed explanation please check out the second half of my video demonstration below:









No comments:

Post a Comment