Renaming files as per directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming files as per directory
# 1  
Old 03-22-2008
Renaming files as per directory

I have many files with duplicate names spread out over several tens of directories. I would like to mv them to the parent directory, but to avoid conflicting filenames I'd like to prefix each filename with the name of the directory it was in.

For example, if this is my directory structure:
/pictures/dotan/1.jpg
/pictures/dotan/2.jpg
/pictures/dotan/3.jpg
/pictures/ety/1.jpg
/pictures/ety/2.jpg
/pictures/ety/3.jpg

Then I'd like to end up with this:

/pictures/dotan-1.jpg
/pictures/dotan-2.jpg
/pictures/dotan-3.jpg
/pictures/ety-1.jpg
/pictures/ety-2.jpg
/pictures/ety-3.jpg

Assuming that pwd is /pictures, I figure that I'll need two expressions, one to rename and another to move. However, I don't know where to start. I've googled the cp (for renaming) and file (for going through all the directories recursively) however I don't know how to access the name of the directories. As for recusively moving the files afterwards, I will probably use file again.
# 2  
Old 03-22-2008
A possibility for the given directory srtucture:

Code:
find /pictures -name "*.jpg" |
awk  -F/ '{print "mv "$0,FS$2FS$3"-"$4}' | sh

Try it first without the last pipe command to sh (in blue) to be sure it gives you the proper commands.


Regards
# 3  
Old 03-22-2008
Thank you, that got me close enough to turn it into this:
Code:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh

However, now I've added yet another tool to my box! I've been avoiding awk for the time being, until I get a handle on more basiv stuff. Although I now have working code, I would still like this to be a positive learning experience. Is it possible to do this with regular bash tools: find, mv, grep, and so forth? I would really like to know how to do this with the commands that I'm currently learning, without getting into awk or perl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Copying files from one directory to another, renaming duplicates.

Below is the script i have but i would like simplified but still do the same job. I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file. ... (1 Reply)
Discussion started by: Gilljambo
1 Replies

3. UNIX for Dummies Questions & Answers

Renaming files with part of their pathname and copying them to new directory

Hi I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this: ./myAnalysis/item1/round1/myoutput.txt ./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Discussion started by: jullee
2 Replies

4. Shell Programming and Scripting

UNIX :renaming the files present in the directory

Hi all, I am looking for a script which renames all the files from the present directory. Eg.: In unix directory contains the below files linux001.txt linux002.txt linux003.txt ...... ....... Now the files should be renamed to unix001.txt unix002.txt unix003.txt Could anyone... (8 Replies)
Discussion started by: scriptscript
8 Replies

5. Shell Programming and Scripting

Moving and renaming multiple files in a directory

Hi. I am trying to automate the movement and renaming of a number of files in a directory. I am using the 'mv' command as I do not have access to 'rename'. I have the following scripted FILES=$(ls /transfer/move/sys/mail/20130123/) if ; then for i in ${FILES} ; do mv... (4 Replies)
Discussion started by: jimbojames
4 Replies

6. Shell Programming and Scripting

Renaming multiple files in a directory

Hello, I would like to rename all available files in a directory from Filename to Filename_Normal. I tried to use below script but it is giving some error: #!/bin/sh for i in `ls` do echo Changing $i mv $i $i_Normal done Error received: Usage: mv src target or: mv ... (10 Replies)
Discussion started by: manishdivs
10 Replies

7. Shell Programming and Scripting

Copying subdirectories of a directory to some other directory and renaming them

Hi, I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory. please... (8 Replies)
Discussion started by: sholay
8 Replies

8. UNIX for Dummies Questions & Answers

Renaming files after their directory name in multiple sub directories

So I am not sure if this should go in the shell forum or in the beginners. It is my first time posting on these forums. I have a directory, main_dir lets say, with multiple sub directories (one_dir through onehundred_dir for example) and in each sub directory there is a test.txt. How would one... (2 Replies)
Discussion started by: robotsbite
2 Replies

9. UNIX for Dummies Questions & Answers

renaming files in the directory

suppose i have a few file like "a b c.txt" , "hello world.c" etc...(files that have space between them in their filenames). how do i change their filenames to "a_b_c.txt" and "hello_world.c" respectively? heres what i have tried...but failed $ find -name "*" -exec mv "{}" "`echo {} | tr "... (4 Replies)
Discussion started by: c_d
4 Replies

10. Shell Programming and Scripting

renaming files in a directory to lowercase

can anybody help me in renaming all the file in a directory to lowercase? script will be helpful. (1 Reply)
Discussion started by: vhariprasad
1 Replies
Login or Register to Ask a Question