how to find files and replace them in a directory in Shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find files and replace them in a directory in Shell scripting
# 1  
Old 11-29-2011
how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data
In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory)
-68395#svg.xml
-56789#ghi.xml
-67894#gjk.org
-56734#gil.txt
I need to find all the such files having above file names as mentioned above and want to replace or cut them.
Expected Output:
I mean i want the files to renamed and processed in the same directory as below:
/java/unix/data
svg.xml
ghi.xml
ghk.org
gil.txt
I mean to say anything which can cut them on the the basis of (#)
Moderator's Comments:
Mod Comment Please use code tags!

Last edited by vbe; 11-29-2011 at 01:45 PM..
# 2  
Old 11-29-2011
Code:
ls | perl -e 'while(<>){chomp;$x=$_;$y=$x;($x=~/#/) && $x=~s/.+?#//;rename $y, $x}'

# 3  
Old 11-29-2011
It seems to be very complicated for me.
Is it possible to do using simple unix/linux commands.
# 4  
Old 11-29-2011
You could do something like:
Code:
for fname in *.xml *.org *.txt
do
   nname=$(echo "${fname}" | sed 's/^[^#]*#//')
#   mv "${fname}" "${nname}"
   echo "${fname}" "${nname}"
done

# 5  
Old 11-29-2011
In the directory there are hundreds of files having several extensions.
i wwant a result which finds all the files which starts with "-" or contains '#'
and then rename it

---------- Post updated at 10:28 PM ---------- Previous update was at 10:26 PM ----------

Will this work?
New_FILE=(`find ${DIRECTORY} -iname "*.*" -print0 | xargs -0 grep "-"`)
will this command will give me all the desired files.

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by vbe; 11-29-2011 at 01:45 PM..
# 6  
Old 11-29-2011
Use *#* as the filename for the for loop.

ksh (see File Name Generation).
# 7  
Old 11-29-2011
"*.*" is a DOS thing. Why not search for the files you want anyway instead of "all files". "-*#*" would be any name starting with - and including # in it.

Cramming everything into backticks won't do what you want. It'll just give you a big list but not do anything to the files. It'll also include directories, not just files. I don't know why you used xargs to grep.

It'd be nice to know what your shell is. Assuming you have bash or ksh.

Code:
find /path/to/dir -type f -name '-*#*' | while read LINE
do
        # Everything after last /
        FILE=${LINE##*/}
        # Everything before last /
        DIR="${LINE%/*}
        echo mv "$LINE" "${DIR}/${FILE##*#}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Shell Programming and Scripting

Shell Scripting on files in directory

Hi, The requirement is below: There are 5 files in a unix directory and i need to pick up latest file from the directory that i can do like this ls -lrt | tail -1 and this file can be processed. now the actual requirement is some times what will happen the second third...on files i.e(ls -lrt... (4 Replies)
Discussion started by: prawinmca
4 Replies

3. Shell Programming and Scripting

Shell script to find the GB files in /tmp directory in remote server

Hi, i need help on shell scripting. Main intention of the script is step 1: ssh to remote server Step 2: cd /tmp in remote server Step 3: in tmp i want to grep only files and directories which are in GB sizes All the servers list file is - tmpsrv.txt vi tmpsrv.txt ... (17 Replies)
Discussion started by: kumar85shiv
17 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. UNIX for Dummies Questions & Answers

Find and replace in all files using shell scripting

Hi all, I'm looking to find and replace a string in all HTML files within a certain directory, including subdirectories. Normally, I would play with this a little to get it to work, but I can't mess this up, so I'm going to ask here. Basically, I want to find "<title>" in all *.htm* files... (11 Replies)
Discussion started by: slothario
11 Replies

6. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

7. Shell Programming and Scripting

Shell:Find a word in files in a directory and subdirectories

I'm looking to write a ksh code with will be alble to find a word like 'toto' in all files going from my current directory. eg. /doc ----------->have: text.c which "toto" /doc/usr-------->have: build.pc, help.java which "toto" /doc/usr/cach -->have: test.sh which "toto" /doc/build... (4 Replies)
Discussion started by: yeclota
4 Replies

8. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

shell script to find and copy the files creted in the year 2006 to another directory

Hi All, I am new to UNIX. I will be thankful if some one helps me. I have to write a shell script for one of the requirement. I have files created from Jan 2006 to March 2008. My requirement is to write a script in such a way that 1) To find and copy(not Moving) the files created in the... (2 Replies)
Discussion started by: manas6
2 Replies

10. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies
Login or Register to Ask a Question