Find the answer to your Linux question:
Results 1 to 4 of 4
I'm completely new to scripting and I'm trying to figure out how to write a script that will get a list of all the files in a directory, drilling down ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    14

    Batch Process files in VI

    I'm completely new to scripting and I'm trying to figure out how to write a script that will get a list of all the files in a directory, drilling down through any subdirectories. When I have the list I want to open each file in VI and change the fileformat. So far all I have been able to figure out is that VI can do the batch processing and that "ls -R" gets me the recursive file list. I'm still pretty clueless on how to do the batch process with the VI editor. I think I'm supposed to use the Ex mode but I don't know how to get the list of arguments from the filelist into the editor so they can be processed.

    If it matters the files were all written in a Windows editor and have gotten the MS carriage returns so I want to do a :set ff=unix command on all the files without having to go into each file manually, there are over 300 files that need updated.

    Thanks for any advice.

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    If all you want to do is change the carriage return, then sed is your friend.


    Code:
    # Unix2DOS:
    sed 's/$/\r/' UNIX_file > DOS_file
    
    # DOS2Unix
    sed 's/^M//' DOS_file > UNIX_file
    Code:
    # Or run it 'in place'
    sed -i 's/^M//' file

    And with find /path -exec $command '{}' \;
    you can look through the directories and recursively convert all files in one go. Be sure to read the 'find' man page though.
    Can't tell an OS by it's GUI

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Your distro might have a dos2unix convertor already anyway. Try apropos to see if such a command exists, or installing it from your package manager.

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    14
    Thanks guys. I'm unable to use my linux box for a few days but I'll try sed when I get home.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...