Rename files from multiple directories along with directory indicator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files from multiple directories along with directory indicator
# 1  
Old 03-16-2017
Rename files from multiple directories along with directory indicator

Hi,

Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator.
For eg:
test--is my parent directory and it has many files such as
a1.txt
a2.txt
a3.txt

test1--subdirectory
b1.txt
b2.txt

test2--subdirectory
c1.txt
c3.txt

now, i want all these files to be renamed in one directory say temp directory, along with directory name or some kind of indicator, so that the renamed file can reveal from which directory it was taken and after renaming i want to move those renamed files from temp directory back to original directories (from which files taken for processing)

**renaming process is handled by informatica etl tool, where i want to add some information in files names.
so here is the flow--
->move files into temp folder(add some indicator in file name)
->rename files in informatica
->move back renamed files from temp directory to original directory based upon the indicator provided in first step(also remove that extra indicator)

apologize for such a long post, tried to simplify as much i can, kindly let me know if require any other information..

TIA
# 2  
Old 03-16-2017
since you want to put them back in original context, why not simply do an archive?
# 3  
Old 03-16-2017
I'm lost - mayhap you oversimplified?

When doing the "rename files in informatica", what will be the resulting file name? Will it still hold the directory part?

How about using symbolic links in the "temp" directory?
# 4  
Old 03-16-2017
here is another detailed eg:

test--is my parent directory and it has many files such as
Code:
a1.txt
a2.txt
a3.txt

test1--subdirectory(under test)
Code:
b1.txt
b2.txt

test2--subdirectory(under test1 or test)
Code:
c1.txt
c3.txt

Now. my final file name should be like this:
Code:
<filename>_<tablename>_<clientname>.txt

i can read files from one directory, rename the files and put in desired folder but i cannot take files from multiple directories or sub directories in one go in informatica, either have to make different mappings for each directory(not a reliable option in long run).
So i want to take all files and put in one folder say temp:

Temp:
Code:
a1.txt
a2.txt
a3.txt
b1.txt
b2.txt
c1.txt
c3.txt

Now, i can process files in informatica all together and rename them according to the logic i mentioned above, after renaming files will be like:

Code:
a1_<tablename>_<clientname>.txt
a2_<tablename>_<clientname>.txt
a3_<tablename>_<clientname>.txt
b1_<tablename>_<clientname>.txt
b2_<tablename>_<clientname>.txt
c1_<tablename>_<clientname>.txt
c3_<tablename>_<clientname>.txt

after this, i want to put these files back to there original directory from temp directory, like:
test--is my parent directory and it has many files such as
Code:
a1_<tablename>_<clientname>.txt
a2_<tablename>_<clientname>.txt
a3_<tablename>_<clientname>.txt

test1--subdirectory(under test)
Code:
b1_<tablename>_<clientname>.txt
b2_<tablename>_<clientname>.txt

test2--subdirectory(under test1 or test)
Code:
c1_<tablename>_<clientname>.txt
c3_<tablename>_<clientname>.txt

**there will be 1000 files and 100s of directories, so i don't want to manually keep record of each file from which directory it belongs to, so here i would require 2 unix scripts, one script will put all files in temp directory and 2nd scipt should separate those files back to original directory.



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-16-2017 at 07:31 AM.. Reason: Added CODE tags.
# 5  
Old 03-16-2017
How about

Code:
find test -type f | awk '{T = $0; gsub ("/", ".", T); print " echo mv", $0, "temp/" T}' | sh
mv test/A3 temp/test.A3
mv test/test1/B2 temp/test.test1.B2
mv test/test1/B3 temp/test.test1.B3
mv test/test1/test3/D2 temp/test.test1.test3.D2
mv test/test1/test3/D1 temp/test.test1.test3.D1
mv test/test1/B1 temp/test.test1.B1
mv test/test2/C1 temp/test.test2.C1
mv test/test2/C2 temp/test.test2.C2
mv test/A1 temp/test.A1
mv test/A2 temp/test.A2

for the to- direction?
# 6  
Old 03-16-2017
as i told i might be getting 1000 files in 100 sub directories, i cannot write manual path for each and every file.
So i would need generic script which will handle any amount of files residing in one directory or sub directory..

*what ever file name or dir name have given just for understanding purpose.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy particular files from a multiple directories and paste in a new directory?

Dear all I have a multiple directories, say for example org1, org2, org3 ..... org100 and each directory having a file namely dnaG.fasta. I need to copy all the dnaG.fasta file from each directory and paste in another directory fastconcatg. Therefore, my script has to copy dnaG.fasta file from... (5 Replies)
Discussion started by: dineshkumarsrk
5 Replies

2. Shell Programming and Scripting

Move files from one folder to another along with directory indicator

Hi All, I have directory and it has multiple sub directories and all these sub directories contains many files. i want to move all these files to one different directory. But after moving files i should be able to recognize which file belongs to which directory. Is there any way to achieve... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

3. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

Copy files from multiple directories into one directory without overwriting them

I have several directories and all those directories have .dat files in them. I want to copy all those .dat files to one directory say "collected_directory" The problem is I don't want to overwrite files. So, if two file names match, I don't want the old file to be overwritten with a new one. ... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

7. Shell Programming and Scripting

bash script to rename multiple directories

Hello I have a directory structure with year in format 4 digits, e.g 2009, below which is month format 1 or 2 digits, e.g 1 or 12, blow which is day format 1 or 2 digits, e.g 1 or 31. I want to change the names of lots of directories to the be Year - 4 digits , e.g 2009 - No change here... (4 Replies)
Discussion started by: garethsays
4 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. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question