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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming files with part of their pathname and copying them to new directory
# 1  
Old 04-23-2014
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
./myAnalysis/item2/round1/myoutput.txt
./myAnalysis/item2/round2/myoutput.txt

I would like to search the myAnalysis directory for all the myoutput.txt files, rename them to include the item and round number from their pathnames and then COPY them to another directory (e.g. leaving the original files/structure intact as that in turn is configured for input into another program).

so I want a new folder with the following files

./newFolder/item1_round1_myoutput.txt
./newFolder/item1_round2_myoutput.txt
./newFolder/item2_round1_myoutput.txt
./newFolder/item2_round2_myoutput.txt

Any suggestions?

Thanks!
# 2  
Old 04-23-2014
Code:
#!/bin/bash
cd /path/to/myAnalysis
[ -d ../new_folder ] || mkdir ../newfolder   # make directory if needed
find .  -type f -name 'myoutput.txt' |
while read fname
do
  newname=$(echo "$fname" | sed 's#/#_#g' | sed 's/^\._//' )
  cp fname ../new_folder/${newname}
done

Start with this example. This can be done several other ways.
# 3  
Old 04-23-2014
Thanks for this jim mcnamara

I had a few problems with the while loop so I ran it in a for a loop and it worked perfectly:

Code:
for i in `find .  -type f -name 'myoutput.txt'`
do newname=$(echo $i| sed 's#/#_#g'| sed 's/^\._//')
cp $i ../new_folder/${new name}
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. UNIX for Beginners Questions & Answers

Add part of pathname/subfolder name to beginning of filename

Hello everyone, I need some help renaming files. I have a directory with a bunch of subfolders in it like so with DWI being the working directory and the 3 levels after it being subdirectories home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz... (8 Replies)
Discussion started by: azurite
8 Replies

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

4. Shell Programming and Scripting

Copying and renaming files

hi, source directory as /home/home01 target directort as /home/home02 I have below files in source directory: FrontOf_history.dat FrontOf_history1.dat In target directory have many files as: Kront_2014.dat Kront_2015.dat Kront_2016.dat Now i want to copy the two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Copying files and renaming

The goal is to read names of files defined ouside in upload.conf and rename them using date, time and proper extension. I have made short script while read; do cp "$RELAY" "$RELAY(date +%Y-%m-%d_%H:%M:%S)_DEPLOYED.ear" done < upload.conf but unfortunatelly it fails printiong the... (9 Replies)
Discussion started by: Michal Janusz
9 Replies

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

7. Shell Programming and Scripting

Need help copying and renaming files

I would like to copy files from one directory to another directory while renaming them at the same time. Does anyone have any ideas on how to do this in a script? Basically, would like to copy files from /user/data/prod/*.txt to /user/data/bck/*.txt.bck (3 Replies)
Discussion started by: thunderkiss65
3 Replies

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

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