Need help for a Shell script to rename multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help for a Shell script to rename multiple files
# 1  
Old 03-03-2010
Need help for a Shell script to rename multiple files

Hi!

I need help to create a shell script to search inside a file and then copy a portion of the search result as the new file name.

Basically I was hacked over the weekend and the genius wipe out my drive from my server. I was able to recover alot of files, but biggest problem Is now the files have no name and make it impossible to sort.

I have found the filename inside the files themself and need to search for it and then rename the file with the results.
Approx 12 000 files to be done.

Here is a sample of the source of the files I'm talking about:
Code:
%!PS-Adobe-3.1 EPSF-3.0
%ADO_DSC_Encoding: MacOS Roman
%%Title: carte_finale_GestionPhocusout.eps
%%Creator: Adobe Illustrator(R) 14.0
%%For: Porky
%%CreationDate: 09-11-09
%%BoundingBox: 0 0 360 489
%%HiResBoundingBox: 0 0 359.9981 488.5669
%%CropBox: 0 0 359.9981 488.5669
%%LanguageLevel: 3

This is the filename inside the file itself right after the "%%Title: "
Code:
%%Title: carte_finale_GestionPhocusout.eps

Now with this info, I want to rename the file itself to exactly this:
Code:
carte_finale_GestionPhocusout.eps

Can it be done?

Many thanks for taking the time to read and help me with this!

Last edited by Scott; 03-03-2010 at 06:14 PM.. Reason: Code tags please...
# 2  
Old 03-03-2010
Tools Maybe something like the following?

What are the files currently called?

Code:
# assuming the files are all called file_ something
for fn in `ls file*`
  do
# display first ten lines
  head -10 $fn
# or, more of what you need
  newnam=`head -10 $fn | grep "%%Title" | cut -d" " -f2"`
# now rename it 
  mv $fn $newnam
done

# 3  
Old 03-03-2010
Code:
Illustrator 9-10-04003
Illustrator 9-10-04004
Illustrator 9-10-04005
Illustrator 9-10-04006
and so on



---------- Post updated at 04:47 PM ---------- Previous update was at 04:38 PM ----------

here is the output of the ur script

Code:
 for fn in `ls *`
>   do
> # display first ten lines
>   head -10 $fn
> # or, more of what you need
>   newnam=`head -10 $fn | grep "%%Title" | cut -d" " -f2"`
> # now rename it 
>   mv $fn $newnam
> done
head: Illustrator: No such file or directory
-sh: command substitution: line 1: unexpected EOF while looking for matching `"'
-sh: command substitution: line 2: syntax error: unexpected end of file
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
head: 9-10-04003.eps: No such file or directory
-sh: command substitution: line 1: unexpected EOF while looking for matching `"'
-sh: command substitution: line 2: syntax error: unexpected end of file
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory


Last edited by Scott; 03-03-2010 at 06:15 PM.. Reason: Code tags please...
# 4  
Old 03-03-2010
borrowing from joey:
Code:
# assuming the files are all called file_ something
for fn in `ls file*`
  do
# display first ten lines
  head -10 "$fn"
# or, more of what you need
  newnam=`head -10 "$fn" | grep "%%Title" | cut -d" " -f2"`
# now rename it 
  mv "$fn" "$newnam"
done

# 5  
Old 03-03-2010
Here is the output

Code:
root# for fn in `ls illustrator*`;   do   head -10 "$fn"   newnam=`head -10 "$fn" | grep "%%Title" | cut -d" " -f2"`   mv "$fn" "$newnam"; done
ls: illustrator*: No such file or directory
root# ls
.DS_Store			Illustrator 9-10-04003.eps


Last edited by Scott; 03-03-2010 at 06:28 PM.. Reason: Code tags, PLEASE!
# 6  
Old 03-03-2010
I think both forms of the loops are choking because the whitespace in the file names. The shell uses the value of IFS to break apart fields, and this is usually a white space. Using a shell 'for' loop in the form of

Code:
for file in $( ls Illustrator* ) #modern form of: for file in `ls Illustrator*`

will break all the files names apart on white spaces (or more accurately on the values in $IFS). You will get the error of "[utility]: No such file or directory [file fragment]"

the other 'for' loop form of:

Code:
for file in '$( ls Illustrator* )' #or for file in "$( ls Illustrator* )"

also does not work because the shell loop returns a single monolithic paragraph with ALL the files names in it.

You can a) rename all the files with no white space, or b) use a form of loop that can use asciz strings for the loop, c) use Perl.

The general form of loop 'b' that can use ASCIZ strings in the loop is like this:

Code:
find [directory] -type f [-maxdepth X] -print0 | while read -d $'\0' file
do
   #rename the files
done

Note the "-print0" and the "while read -d $'\0'". The first reads the file names and puts them in a string that is terminated by a ASCII NUL; the second sets a while loop that reads the list with NUL terminators...

Good luck!

Last edited by drewk; 03-03-2010 at 06:52 PM..
# 7  
Old 03-03-2010
What would the script look like?

---------- Post updated at 05:47 PM ---------- Previous update was at 05:41 PM ----------

Code:
# assuming the files are all called file_ something
find [directory] -type f [-maxdepth X] -print0 | while read -d $'\0' file
do
# display first ten lines
  head -10 "$fn"
# or, more of what you need
  newnam=`head -10 "$fn" | grep "%%Title" | cut -d" " -f2"`
# now rename it 
  mv "$fn" "$newnam"
done

Gives me:
find: [-maxdepth: unknown option
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

3. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

4. Shell Programming and Scripting

Rename multiple files

Hi, In my directory I have many files, for e.g. file_123 file_124 file_125 file_126 file_127 Instead of renaming these files one by one, I would like to rename them at a same time using same command... they should appear like 123 124 125 126 127 What command(awk or ls or... (3 Replies)
Discussion started by: juzz4fun
3 Replies

5. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. Shell Programming and Scripting

Shell script to rename a group of files

Hello, I am having 1800 files in a directory with a specified format, like amms_850o_prod.000003uNy amms_850o_prod.000003u8x amms_850o_prod.000003taP amms_850o_prod.000003tKy amms_850o_prod.000003si4 amms_850o_prod.000003sTP amms_850o_prod.000003sBg amms_850o_prod.000003rvx... (12 Replies)
Discussion started by: atlantis
12 Replies

8. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

9. Shell Programming and Scripting

Shell script to rename files with .1,.2,.3 ....ext respectively

Hey Guys.... Just need some help as I am not proficient in Unix shell script... Doubt: --------------- Suppose there will be some of the following files inside a directory called OUT ... Path: - /appdb1/product/batch/rms/OUT files inside OUT directory:- POSU_75002_20090127_20090129035442... (4 Replies)
Discussion started by: satyajit007
4 Replies

10. Shell Programming and Scripting

rename multiple files

Hi, can anyone have a ksh script to rename multiple files (ie to remove .Z extension of the files) can someone correct this? for i in *.Z do var1 = substr($i, 1,at(".Z",$i)-1) mv $i $var1 done Thanks.. Antony (13 Replies)
Discussion started by: antointoronto
13 Replies
Login or Register to Ask a Question