renaming all files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming all files
# 1  
Old 02-09-2011
renaming all files

requirement:

There are many files inside a directory and all are named like this "out_1".

The file names goes on itearation like out_2, out_3 etc....

Now i want to rename all the file names based on the text inside the file.

the text in all the files follow a pattern like it satrts with "table.shipment"

The text 'shipment' must be captured and the file has to be renamed with 'shipment' .


can anyone help me out????Smilie


thanks in advance,
Arun Manas
# 2  
Old 02-09-2011
Code:
$> ls -la
total 16
drwxr-xr-x 2 root root 4096  9. Feb 08:16 .
drwxr-x--- 4 root root 4096  9. Feb 08:16 ..
-rw-r--r-- 1 root root   15  9. Feb 08:16 f1
-rw-r--r-- 1 root root   16  9. Feb 08:16 f2
$> head *
==> f1 <==
table.shipment

==> f2 <==
table.transport
$> for f in *; do mv $f $(head -1 $f| cut -d"." -f2); done
$> ls -1
shipment
transport

# 3  
Old 02-09-2011
Hi Zaxxon,

Thanks for your valuable reply.

The syntax we get an error and the error message is
Code:
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

can you please let us know what is the -f2 in the code doing after cut -d"."
Code:
for f in *; do mv $f $(head -1 $f| cut -d"." -f2); done

Thanks in advance,
arunmanas

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-09-2011 at 11:27 AM..
# 4  
Old 02-09-2011
A quick man cut would have explained you that too. It means to cut field 2 while using a dot as delimeter.

Seems list of values in the for loop is empty or not correct. Go into the directory where the files are and do a
Code:
for f in *; do echo $f; done

This should list all directory entries in there but those starting with a dot. If you have those "out_*" files mixed in there with others, you might want to refine that and filter it with something like
Code:
for f in out_*; do echo $f; done

# 5  
Old 02-09-2011
hi Zaxxon,

that really worked very well.

But now a new issue like in my file i have the text like this

CREATE OR REPLACE FUNCTION "OWNER"."TABLE_SHIPMENT" RETURN VARCHAR2 IS

but when i use the query that you have provided if the text after tehd elimiter dot has space in between the below error will occur.

mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

the code should be modified such that it doesnt care about the space and picks only the word TABLE_SHIPMENT from the text.

we cant take by position also because each words before the text varies.

Kindly help me out.

thanks for ur valuable replies and for the future solution too Smilie

Arunmanas
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming files

Hello, I am looking for a command line that will rename name files : f700_abc_o_t_MASTERID_AS_AE_20130323.csv like this f700_abc_o_t_MASTERID_AS_AE_20130324.csv The great idea could be to get the date stamp 20130323 and change any part of it, instead of just change the... (4 Replies)
Discussion started by: Aswex
4 Replies

3. Shell Programming and Scripting

renaming files

Hi, I have a list of files in a folder with the same name ending (over 1000 files) joe.jpy.jpeg joe1.jpy.jpeg joe2.jpy.jpeg jon3.jpy.jpeg jor5.jpy.jpeg .....jpy.jpeg etc. I want to change jpy to hhk So the output will be: joe.hhk.jpeg joe1.hhk.jpeg joe2.hhk.jpeg jon3.hhk.jpeg... (3 Replies)
Discussion started by: kylle345
3 Replies

4. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

5. Shell Programming and Scripting

renaming files

Hi all, using a utility image file was named starting with blank space and a blank space in between. I want to rename the files. file names are in the format " sb 12.tif"," sb 13.tif"," sb 14.tif" the files are in thousands. i want to rename as 12.tif, 13.tif, 14.tif.... thanks. (3 Replies)
Discussion started by: ahkverma
3 Replies

6. UNIX for Dummies Questions & Answers

renaming files

I have a list of files named ab_*.csv I would like to remane them all by removing the ab_ and have *.csv I did the following but I am surely missing something. /* wrong script */ for i in `ls -1 ab_*`; do mv ab_$i $i; done Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies

7. UNIX for Dummies Questions & Answers

Renaming files

Hello! I am not familiar with UNIX and I have this problem: I need to move files from a UNIX machine to a PC. UNIX file names contain ":" as special character which is not recognized in a PC. How can I change ":" for "_" in the name of a bunch of files in UNIX? Thanks for your help. (7 Replies)
Discussion started by: Tygoon
7 Replies

8. UNIX for Dummies Questions & Answers

renaming files

directory name = /usr/tom/1997 files - ABC_1997_ST1_BCD.SQL BCD_1997_ST1_EFG_SAB.SQL TTT_EBC_1997_ST1_A.SQL sub directory - /usr/tom/1997/jan a) I want to just rename the all files ending with '.SQL' and also its contents in the 1997 directory(excluding subdirectories eg... (3 Replies)
Discussion started by: systemsb
3 Replies

9. UNIX for Dummies Questions & Answers

renaming the files

Hi All, Today I got a small problem while handling zipped files in PROD support. There are files in this format and I had to grep them reading some contents A.B.gz.C.D where A,B,C and D stand for variables (like FIRST.NAME.gz.MIDDLE.LAST). I know that these files are zipped files and If I... (1 Reply)
Discussion started by: adurga
1 Replies

10. UNIX for Dummies Questions & Answers

renaming files

i have a set of *.lst files. now i want to change the names from "lst" to "dat". how to do it? ex.: -rw-r--r-- 1 rram group 22 Sep 21 13:10 a.lst -rw-r--r-- 1 rram group 22 Sep 21 13:09 b.lst -rw-r--r-- 1 rram group 22 Sep 21 13:10 c.lst... (4 Replies)
Discussion started by: raguramtgr
4 Replies
Login or Register to Ask a Question