How to search and replace text within all UNIX files, filenames and directories (AIX 5.2)

Posted on 2007-07-20
Last Modified: 2013-11-17
I want to search thru every directory. filename and file, looking for a particular text (let's say we're looking for RECO).  Then, when RECO (either upper or lower case)  is found within the body of a file, I want to first make a copy of  that file,  to the same directory, with another extension added to the filename (original_name.txt.orig) and then replace RECO (within the original file) with UPGRD.  In addition, if there are directories or filenames that have the same text (RECO) in them, I want to write the original  directory name or filename to a file (in order to track what was changed),  then replace the text RECO in the directory or filename with UPGRD. I'm using UNIX on AIX 5.2.  I'm just coming back to using UNIX and I'm not quite to par with scripting.
Question by:cbetter
    1 Comment
    LVL 14

    Accepted Solution

    Are you sure you want to perform this operation on directories? I don't think so. Let's exclude them. After all directories contain filenames. If you replace text in a directory you are causing potential problems and you don't want that.

    find $DIR -type f | xargs grep RECO 2>/dev/null | cut -d: -f1 | sort | uniq | while read PATH
    sed <${PATH} >${PATH}.new -e 's/RECO/UPGRD/g'
    mv ${PATH} ${PATH}.orig
    mv ${PATH}.new ${PATH}

    This does the first part, though not quite as you specified. For the "in addition", are you asying that RECO can be in the file name also? If so then can the file contain RECO too? If so,

    find $DIR - type f | grep RECO | while read PATH
    NEWPATH=$(echo $PATH | sed -e 's/RECO/UPGRD/g')
    echo $PATH >/tmp/yourlogfile

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
    My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now