Search and Copy Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Copy Files
# 1  
Old 07-12-2016
Search and Copy Files

Hi All

Need your help, I am looking for a script to search for files with specific extension as .log and then copy the latest one to a different folder.

Here is the scenario

/dev/abc/xyz/a_2_122920131.log
/dev/abc/xyz/a_2_123020131.log
/dev/abc/xyz/b_2_12302013.log
/dev/abc/xyz/b_2_12312013.log
/dev/abc/xyz/c_2_12312013.log

I want to search for all such files for all folders under home and then copy the latest one for each one of them

So for above scenario I should be able to copy follwoin

/dev/abc/xyz/copy/a_2_12302013.log
/dev/abc/xyz/copy/b_2_12312013.log
/dev/abc/xyz/copy/c_2_12312013.log

Any inputs will help

Thanks...
# 2  
Old 07-12-2016
Once you get to the directory where there are .log files, you can do
Code:
file=`ls -tl *.log| head -1|sed "s/.* //"`
cp $file copy

That creates a list of all ".log" files, latest first.

The above assumes (probably incorrectly) there are no directories under the one you are currently in. In that case another sed command immediately follow the "ls -tl" command to remove all lines starting with total or the letter d.

A driver script would start the above code starting with "find .. -name '*.log', redirecting its output (full path to each log file). Lines containing "/copy/" would first need to be taken out. A dirname or other utility would be run on the resulting file to leave only the path to each file and it would then be sorted uniquely to provide a list of all directories where the script described in the first paragraph would be run.

Also, the code in the first paragraph should probably start with "cd $1" with each directory where this is to be done since this parameter will be provided by your driver script.
# 3  
Old 07-12-2016
I'm not sure if there should be ANY regular files in the /dev directory...
And, how can /dev/abc/xyz/a_2_123020131.log result in a copy /dev/abc/xyz/copy/a_2_12302013.log?

Latest compared by files' time stamps or file name elements? Can there be duplicate file names across directories, and what to do in such case?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to search and copy files

HI everyone, I been to this site before for help and found my answers on other threads now I am posting my own :). I have a list of file names with out extensions on an txt file. I need a way for the script to search on the server for each file name and copy the files over to a new directory.... (12 Replies)
Discussion started by: sergiol
12 Replies

2. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

3. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

4. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

5. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

6. Shell Programming and Scripting

shell script to search and copy files

Hello Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario: I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new... (4 Replies)
Discussion started by: c.watson
4 Replies

7. Shell Programming and Scripting

Search, copy and paste

Can i search in a file for more than one string at a time? And copy the next string after that and paste it in column style? Is it possible? Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

Search and copy to a new file-Help

Hi All, server16.na.in.com UNKNOWN ftpuser "CWD" dms-imrm/Delasco_Invoices_DayForward_Scan" 250 - server16.na.in.com UNKNOWN ftpuser "PWD" 257 - server16.na.in.com UNKNOWN ftpuser "CWD Private" 250 - server16.na.in.com UNKNOWN ftpuser "PWD" 257 - server16.na.in.com UNKNOWN... (7 Replies)
Discussion started by: Tuxidow
7 Replies

9. Shell Programming and Scripting

search files and copy them to a directory with datestamp attacched to it

Hi I need to search the some ftp files created in last 24 hours and copy them to a directory with date stamp attached to it. Iam using following command to search the files find $CA_OUT_PATH/*/ftp_out -type f -mtime -1 but now how to copy these files to some other directory one by one ... (1 Reply)
Discussion started by: sreenusola
1 Replies

10. UNIX for Dummies Questions & Answers

search files and copy them to a directory with datestamp attached to it

Hi I need to search the some ftp files created in last 24 hours and copy them to a directory with date stamp attached to it. Iam using following command to search the files find $CA_OUT_PATH/*/ftp_out -type f -mtime -1 but now how to copy these files to some other directory one by one with... (1 Reply)
Discussion started by: sreenusola
1 Replies
Login or Register to Ask a Question