How to create, view and edit simple file in Linux/Uinx using Terminal

Linux / Unix operating system provide many command line tools and text editors for creating single or multiple text files. We can use gedit, vi/vim text editor. It is a terminal based text editor in Linux/ Unix Systems, available under the GPL.  There are many, many text editors available and it is designed to be easy to use. These are follows.


  •   Cat command
  •    Touch command
  •    Echo or printf command
  •    Vi/vim editor
  •    Gedit editor

Now we see only commands

Create a Text file using cat command

  • Cat (concatenate) command is used to create a file and to display and modify the contents of file


To Create a file

# cat > filename ( say file1)
Hello world
Press CTRL+D  to save a file


[root@rahul ~]# cat > file1
Hello World


To display the content of the file

# cat filename


[root@rahul ~]# cat file1
Hello World
[root@rahul ~]#


To append the data in already existing file

# cat >>  filename
# cat >> file1
Ctrl+d  (to save the changes)


[root@rahul ~]# cat >> file1
Welcome to linux
[root@rahul ~]#



Creating multiple empty  files at the same time using touch command

# touch <filename> <filename> <filename> <filename>
# touch file1 file2 file3 file4
Note:  For Check the files use # ls command


[root@rahul ~]# touch file1 file2 file3 file4
[root@rahul ~]# ls
4raj                         file1        install.log.syslog  ktfile.softlink  raj
anaconda-ks.cfg     file2         kernal              Music            ram
Desktop                  file3        ktdir               Pictures         songs
Documents             file4        ktfile              Public           Templates


Create a text file using echo or printf

# echo ‘Hello World’ > filename


[root@rahul ~]# echo 'Hello world' > file1


OR

# printf  ‘Hello world’ > filename


[root@rahul ~]# printf 'Hello world\n' > file1


OR

# printf  ‘Hello World \n Another line’ > filename


[root@rahul ~]# printf 'Hello world\n another line ' > file1


To display the file contents, type:


[root@rahul ~]# cat file1



Post a Comment