Parsing all the files in the directory and replacing them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing all the files in the directory and replacing them
# 1  
Old 03-26-2012
Parsing all the files in the directory and replacing them

Hi,
I would like to parse all the files (*.c and *.h) in the sub-directories and if *.c or *.h files are found I would like to append a string and place it in the same directory. How do I do that?

For example:
I'm gonna operate on dir1 level.


$dir1/subdir1/test.h should be done as $dir1/subdir1/xyztest.h


like this I have to do all the sub directories in that directory.
# 2  
Old 03-26-2012
Not tested, but it should work.

Code:
dir="/some/directory"
prefix="xyz"
find $dir -type f \( -name "*.c" -o -name "*.h" \) | while read f
do
  dirname=${f%/*}
  basename=${f##*/}
  mv "$f" "$dirname/$prefix$basename"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable of Path directory is not parsing in awk

Hi All, i had to split one files into 10 equally. For that i have coded below awk. OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst DIR_NM=`dirname ${OUTPUT_FILE}` awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE} ... (7 Replies)
Discussion started by: looney
7 Replies

2. Shell Programming and Scripting

Replacing exact directory variable

I am making a script to relocate a project file. I have all of the variables in place and everything is working except: the first variable changes everytime it passes thru a loop. The second is a constant value. when I run that it does what I want...but incorrectly. It is finding... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

4. Shell Programming and Scripting

Replacing variable is files in same directory

Hi all, I'm writing a script to get values from the user and replace it in another file in the same directory. /usr/bin/sediff 's/$PROJECT/'$PROJECT'/' /ananth/TEMPLATE TEMPLATE is the file I want to replace the variable PROJECT from the script I'm writing but Im not getting it done. Is... (4 Replies)
Discussion started by: Ananthdoss
4 Replies

5. Shell Programming and Scripting

Help with directory path parsing

I've been working on a UNIX script (csh) that will be starting a java application The goal is to get the version number and location (needed by the application) from the path of the script Example: Location of the script= /apps/myapp/versionNum/script/start.csh I need:... (8 Replies)
Discussion started by: brianjbrady
8 Replies

6. UNIX for Dummies Questions & Answers

Help parsing and replacing text with file name

Hi everyone, I'm having trouble figuring this one out. I have ~100 *.fa files with multiple lines of fasta sequences like this: file1.fa >xyzsequence atcatgcacac...... ataccgagagg..... atataccagag..... >abcsequence atgagatatat..... acacacggd..... atcgaacac.... agttccagat.... The... (2 Replies)
Discussion started by: mycoguy
2 Replies

7. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies

8. Shell Programming and Scripting

replacing new lines in all files of a directory containing old lines

Hi all, I am trying to replace a few lines with other lines of all files in a directory which contain those few lines. say - there are some 10 files in a dir having the same 4 lines as 1.txt at the starting 1.txt line 1 line 2 line 3 line 4 ....................................... (1 Reply)
Discussion started by: rooster005
1 Replies

9. UNIX for Advanced & Expert Users

Replacing text inside a directory

This is my first post to this group.I am new to Unix.I have one requirement I need to replace a text in all the files in a directory and sub-directory.Eg i have a folder structure like /usr/local/abc , inside abc i have several directories and several files. All the files inside abc and all the... (1 Reply)
Discussion started by: vatsan
1 Replies

10. Shell Programming and Scripting

Replacing characters in a directory structure

I have a directory structure that contains "()". I need to recursivly replace the "(" with an "_" and remove the ")". Can I write a script to: Read -R C:\VCE If "(" exitsts replace with "_" elseif ")" exists, delete. (4 Replies)
Discussion started by: tmettie
4 Replies
Login or Register to Ask a Question