Monday 17 February 2014

Editing Multiple Files in Linux

Editing multiple file in linux can enhance user's working speed , here I am discussing some basic commands which may be helpful for all of us. Please download the file from here.

Editing multiple files in linux for advanced users
Here the list of some commands by using this you can directly switch between multiple files. This is very useful for the frequent linux users, it can enhance your working speed.

1. Put all the file names with vi
vi file1 file2 .... (you can write name of files you want to edit or write something)
In terminal first file will open, by using normal vi commands like Esc : i you can easily insert, anything
Esc :w (for write the file)
       :n (you can switch to the next file)
Esc :wq (for save all the files)

2. No need to put all the names of the file, first you give only single file name
vi file1 (you can edit this file using normal vi commands)
Esc :w (for write the file)
      :e file2 (file2 is the name of file you want to open after file1)

3.  vi remember two file names at the same time current and alternative file names,
symbol % (current file name)
symbol # (alternative)
vi file1 file2
you wrote something in file1 using Esc -i, Esc :w write, than you move to the next file either by :n or :e here you made some changes in file2, but you don't want to save current changes and want to go back in first file, for this you can use the following command.
:e!#  this command will discard your edits in the current file and return to the last saved version of the current file (file1)
:w %.new  this command will make copy of your current edited file with suffix .new (output: file1.new)

You want to find and replace one string in multiple files in the same directory, you can do it with the following command in vi
vi *.txt (to open all the files)
:argdo %s/findme/replaceme/g | wq

you can use sed command also for the same

sed -i 's/findme/replaceme/g' *.txt

No comments:

Post a Comment