Copying a file based on a incremented folder name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copying a file based on a incremented folder name
# 1  
Old 01-15-2015
Copying a file based on a incremented folder name

I need to routinely cp a file out of the highest incremented folder named PROD_2_6.1_xxx where xxx (3 numerical values) is incremented only once a night starting at 100 and ending 999 over a period of time. I am not worried about copying a subset of a complete file while it is being created by some other process.

example:
Code:
/nightly/PROD_2_6.1_110/filename.txt 
/nightly/PROD_2_6.1_111/filename.txt
/nightly/PROD_2_6.1_112/filename.txt


I have only the most rudimentary understanding of how to copy and no practical experience despite searching for an answer. All I have managed to pull together is:

Code:
 cp -r /nightly/2.6.1/PROD_2_6.1_  ?????

Any help is appreciated :b Thanks everyone!

Last edited by sf_cb; 01-15-2015 at 04:37 PM.. Reason: added code tags - first time poster.
# 2  
Old 01-15-2015
Please use code tags as required by forum rules!

Any attempts from your side?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-15-2015
In addition to showing us what you have tried (as requested by RudiC), please also give us more information about your directory names. Is x in PROD_2_6.1_x always three digits? What range of x values does your script need to be able to process?

What do you mean by the "latest file" in a directory? (Modified most recently? Last filename in an alphabetic list of filenames? Something else?) How do you know you won't be copying a subset of a complete file while it is being created by some other process?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-15-2015
updated post as requested. I hope that is enough info for your consideration. Thanks
# 5  
Old 01-15-2015
The command:
Code:
ls -d /nightly/PROD_2_6.1_???

will give you a list of the directories you're interested in in increasing alphabetic order. Since all of the numbers at the end of these directory names contain 3 digits, this will also be in increasing numeric order. Adding the -r option to ls will give you the list in reverse order. Piping the output of ls through the command:
head -n 1 will give you just the first line, which will be the directory that you want. You can use save that directory name in a variable using command substitution and variable assignment:
Code:
last_dir=$(ls -rd /nightly/PROD_2_6.1_??? | head -n 1)

To copy a file named filename.txt from that directory to another directory (in this example, named /target/directory), you can then use:
Code:
cp "$last_dir/filename.txt" "/target/directory/filename.txt"
                      or
cp "$last_dir/filename.txt" "/target/directory/"

You can change the name of the file in the target directory using the first form above; the second form will copy the source file to the target directory using the same name as the final component of the source file pathname as the filename of the copy in the target directory.

Hope this helps...
# 6  
Old 01-15-2015
Thanks Don, appreciate your patience. This is perfect.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Copying section of file based on search criteria

Hi Guru's, I am new to unix scripting. I have a huge file with user details in it(file2) and I have another file with a list of users(file1). Script has to search a user from file1 and get all the associated lines from file2. Example: fiel1: cn=abc cn=DEF cn=xyx File 2: dn:... (10 Replies)
Discussion started by: Samingla
10 Replies

3. Programming

Perl - Moving file based upon filesize in folder

Hi I'm trying to look through a series of directories in A folder, lets just call it A: for example: A/1 A/2 A/3 Etc and I wish to move the files in the folder if they are bigger than a certain size into a structure like below: A/TooBig/1 A/TooSmall/1 A/TooBig/2 A/TooSmall/2... (1 Reply)
Discussion started by: PerlNewbRP
1 Replies

4. Shell Programming and Scripting

Compare files in a folder based on another file

I have a file named file.txt that looks as follows //class1.txt 45 234 67 89 90 //class2.txt 456 34 78 89 120 class1 and class2.txt are the names of files in a folder named folder1. The content of class1.txt file in folder1 67 9 89 5 234 9The content of class2.txt file in... (1 Reply)
Discussion started by: jaff rufus
1 Replies

5. Shell Programming and Scripting

Replace the content of file with incremented value

I have a file myfile with only one value 1000.I am using it in a shell script.Each time i run the script,the file shud get incremented by 1. I have used the below code for incrementing the value- curr=`cat myfile` echo $curr curr=`expr $curr + 1` But i am not sure how to save this replaced... (2 Replies)
Discussion started by: saga20
2 Replies

6. Shell Programming and Scripting

Rename folder based on containing XML file

Hi everyone. I'm in need of a solution where i need to rename a folder to a name that's inside an XML file in that folder. OS is Ubuntu 9.10 with Gnome. I've tried using grep, sed and xpath, but can't seem to find a solution. This is the simplified folder structure: FOLDER-NAME -... (4 Replies)
Discussion started by: CoolCow
4 Replies

7. Shell Programming and Scripting

Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix. I would basically like to know if i can do this: Lets say i have a folderA and folderB And i save something in folderA Can i make a script that checks folderA latest file, then compares it with the date of latest file in... (16 Replies)
Discussion started by: takissd
16 Replies

8. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

9. Shell Programming and Scripting

generate file incremented by 1

Hi I a really need help. I got simply shell script which store result into the specific file in this case xxx_001.txt And after another run script I would like to create the same file but value 001 is incremented by 1 -> xxx_002.txt a so on. If I gonna use awk utility then result... (4 Replies)
Discussion started by: mape
4 Replies
Login or Register to Ask a Question