Renaming files in bulk.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming files in bulk.
# 1  
Old 10-08-2013
Renaming files in bulk.

I have a bunch of files which are named something like:

Company Name~1234~X1234~X1-123.pdf

I need to get them renamed something like:

Company Name~1234(X1234)X1-123.pdf

Once I have the X1234 inside () I have a piece of software which can use the X1234 bit. I will be receiving hundreds of these files per week and I want a script to do it. Ultimately I'd like the files produced with () or the piece of software amended to look for ~'s as well as () but for now a script like this would solve my problem.

I'm hoping there are some command line prompts to achieve this.


Last edited by Scrutinizer; 10-08-2013 at 05:49 PM.. Reason: code tags
# 2  
Old 10-08-2013
On what OS should this be running? and what shell are you using?
# 3  
Old 10-08-2013
Renaming files in bulk

It's SCO Openserver 6.0 and sh.

# 4  
Old 10-08-2013
Hello jcborland,

Could you please use the following code, hope it may help you.

echo "Company Name~1234~X1234~X1-123.pdf" | awk 'sub("~X1234~","(X1234)")'

Output will be as follows.

Company Name~1234(X1234)X1-123.pdf

Let us know if we can help you further.

R. Singh
# 5  
Old 10-08-2013
Renaming files in bulk

Hi Ravinder,
That might work if X1234 was always the text between the ~'s. I need to get what ever is between the 2nd and 3rd ~ placed between a ( and a ). It won't be a constant "X1234". The length of the file name can vary the number of characters between ~'s will vary.

Thanks for help.

# 6  
Old 10-08-2013

try this :

 echo "Company Name~1234~X1234~X1-123-abc123.pdf" | sed 's/\([0-9a-zA-Z ]*~[0-9]*\)~\([^~]*\)~/\1(\2)/'
Company Name~1234(X1234)X1-123-abc123.pdf
echo "Company Name~1234~XYZ1234567az~X1~zxd123~abc123.pdf" | sed 's/\([0-9a-zA-Z ]*~[0-9]*\)~\([^~]*\)~/\1(\2)/'
Company Name~1234(XYZ1234567az)X1~zxd123~abc123.pdf

# 7  
Old 10-08-2013
sed 's/~/(/2; s/~/)/2'

-or, try-
ls "Company Name"~*~*~*.pdf | 
while IFS="~" read f1 f2 f3 f4
  mv "$f1~$f2~$f3~$f4" "$f1~$f2($f3)$f4"

(As long as the file names do not contain newlines)

Last edited by Scrutinizer; 10-08-2013 at 06:03 PM..
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. UNIX for Dummies Questions & Answers

Help with renaming the files.

Hi, I have 31 file as below.... .. .. I want to rename it like... .. .. How can I do it ? any suggestions ? I want to do it small... (5 Replies)
Discussion started by: mahesh shinde
5 Replies

3. Shell Programming and Scripting

Renaming bulk directories and subfiles

Hi, I have a directory with 100 subdirectories and each of these subdirectories has 1 file. Now I have to rename all these. The structure is "files directory has 100 SRR191639-SRR191718 subfolders and in each there is a file with the same name a subdirectory followed by .sra extension... (5 Replies)
Discussion started by: Diya123
5 Replies

4. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

5. Shell Programming and Scripting

bulk renaming of files in sftp using script

Hi, Am using sftp (dsa method) to transfer 20 files from one server(sftp) to another (local). After the transfer is complete the files in the sftp server has to be renamed from .txt extension to .done extension ( aa.txt to aa.done, bb.txt to bb.done and likewise...). I tried rename command... (4 Replies)
Discussion started by: Sindhuap
4 Replies

6. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Need help renaming bulk file extentions

Hello, I am trying to rename bulk files however i dont think the rename/mv command is giong to help me here. here is a quick snapshot of the files I need to rename: 75008040 -rw-r----- 1 root root 8716 May 8 05:00 75008041 -rw-r----- 1 root root 11700 May 8 05:00 (10 Replies)
Discussion started by: jallan
10 Replies

10. UNIX for Dummies Questions & Answers

Help with renaming files

Anyone out there know how, in a one line UNIX Command, I could rename(mv) a file that contains variable string as part of its name. The variable string is the process instance number. For example, I'd like to be able to rename XX_XXXXX_nnnnn.pdf (where nnnnn is a number and its value depends on... (4 Replies)
Discussion started by: mc_cool_e
4 Replies
Login or Register to Ask a Question