Shell renaming files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell renaming files
# 1  
Old 12-05-2011
Shell renaming files

I'm completely new to Shell scripting and I need to make a script that renames files with names like this: "aaa-b_ccc" where aaa b and ccc can change but b is a single character into files with names like this one:

"ccc'_bbb'_aaa" where ccc', bbb', aaa' are values I set accordingly to the original values aaa,b,and ccc, but not the same values. It's not possible to "calculate" the new values so I'm planning to make some kind of a csv file where I list old value -> new value. I already figured out that I can use SED to make easier changes but I'm just not smart enough to solve my problem :P

Hope you can help me! Thanks
# 2  
Old 12-05-2011
Sorry, but your description sounds ambiguous.
Please post an example of input filenames and output filenames, thanks. Use code tags when doing so, thanks.
# 3  
Old 12-05-2011
filename now: "Groesse-h_Spanien.jpg"
new filename: "Spain_Size-in-km_horizontal.jpg"

many different countries, differen attributes, different stuff for the 'h' part.

PD: "Groesse" is the german word for size.

dunno why i wud use code-tags for that, theres no code in here..?
# 4  
Old 12-05-2011
Code tags are just as useful for logs, data and other snippets, as they separate that data from describing text and enhance readability as well as they preserve special characters and if somebody has something like a colon and a closing bracket, you would get a smiley but inside tags they will not substituted. They also preserve indention etc.
Your example is very small and readable, but generally it would be nice if you use them as most other users do, in case you have more extensive data or logs, code etc. next time.
Außerdem: Das Auge ißt mit Smilie

To your problem:
How are the new strings supplied? Do you have a list or something?
How is the relation between the entries in that list to the file names? Is there a translated definition of the countries already in that list?
# 5  
Old 12-05-2011
Alright, will use code tags for longer examples and try not to use slang Smilie I didn't even notice that I wrote 'wud', too used to doing so.

The list is not created yet and will preferably be created in an excel sheet -> can be saved as csv file then. In this list I would also specify the translation of every country and replace the country names within the script similiar to the way I replace the other parts of the filename.

Thanks for the help already!
# 6  
Old 12-05-2011
No problem, we will get it right^^

Ok, so assuming, as the substitution and keys etc. is still not clear, I suggest the following:

Have two lists; the one that is to be created (your csv file as you said) containing the stuff that will replace the placeholders or be added to the original file names and the other one be generated by listing the files that have to be renamed. The second list could be generated by a simple ls *.jpg or something more appropriate.

There will be some key or anything needed to bring the contents of both lists into relationship. Else the substitution would just be random or in an unwanted order, and that would be the last you want to have Smilie

When having the list you could do something like:
Code:
root@box> ls -1 *jpg
groesse-h.England.jpg
groesse-h.Germany.jpg
groesse-h.Spain.jpg
groesse-h.Uruguay.jpg
root@box> ./mach.ksh
root@box> ls -1 *jpg
England_400_horizontal.jpg
Germany_200_horizontal.jpg
Spain_100_horizontal.jpg
Uruguay_150_horizontal.jpg

Here is the script:
Code:
root@box> cat mach.ksh
#!/bin/bash

# Saving and altering the Input Field Separator
OLDIFS=$IFS
IFS=";"

# Reading in the 1st list "infile" and by IFS breaking it into two variables
while read COUNTRY SIZE; do
        # Listing the current file name that is identified by "$COUNTRY" and saving it to a variable
        FILE=$(ls *${COUNTRY}.jpg)
        # Actually renaming the file
        mv $FILE ${COUNTRY}_${SIZE}_horizontal.jpg
done < infile

# Changing to altered IFS back to it's default
IFS=$OLDIFS

exit 0

This will not work 1:1 in your case as many things are still not clear yet but I hope you get the clue what I did. It should be possible to be adopted to your case.
You can play around with it with (placing variables at different positions etc.) some example data so you don't destroy things^^
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for renaming and moving Files - Easy?

Hey guys, ive been working on this for about 2hrs now - without any solution. At first I need to say I dont have skills in linux bash scripting, but I tried to use some codesnippets and manuals from google. What I want to do: I have different folders including 2 different filestypes with... (15 Replies)
Discussion started by: peter1337
15 Replies

2. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

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

4. Shell Programming and Scripting

Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format : abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23 abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35 abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37 abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. UNIX for Dummies Questions & Answers

Renaming files

Hi all, I'm working in a specific directory and I have file names which I'd like to rename but in a way in which I can specify the new filenames as @ARGV or user input at prompt. Can someone shed some light on this? Cheers :) (7 Replies)
Discussion started by: pawannoel
7 Replies

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

7. Shell Programming and Scripting

Renaming multiple files with a shell script

Hey guys, I'm really new to UNIX and shell scripting in general. For my internship I need to rename a bunch of files. Specifically, I need to change the first letter of each of the files to lowercase and I have to change the endings so they all basically look like "file_cone.jpg". I know I... (4 Replies)
Discussion started by: jjzieve
4 Replies

8. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

9. UNIX for Dummies Questions & Answers

Renaming Files using Shell Script

Hi Gurus, I have some files(all ending with .out as extension). Ex: aa1.out aa2.out aa3.out I would like to append each file with the current date to the end of the file so that they should become aa1_20090504.out. So I am using rename as follows: for i in path/aa* ; do mv $i... (5 Replies)
Discussion started by: asmfloyd
5 Replies

10. Shell Programming and Scripting

copying files and Renaming them + shell script

Hi, I have a problem. I have some text files in a folder. The names can be like: emp.txt emp1.txt emp3.txt 32emp4.txt What i need is i have to copy all the files which have "emp" string in their filename to a different folder and those file names... (7 Replies)
Discussion started by: pathanjalireddy
7 Replies
Login or Register to Ask a Question