How to copy the two most recently added files to another directory - HP-UX?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy the two most recently added files to another directory - HP-UX?
# 1  
Old 02-23-2015
How to copy the two most recently added files to another directory - HP-UX?

Hello,

I am attempting to find and copy the two most recently added files to a specific directory, that fit a specific format.

I was able to find the command to list the two most recently added files in directory:
Code:
ls -1t | head -n 2

The command lists the two files names in a vertical list, top being most recent.

Q1: How can I extend this command to only select the most recent files with a specific format?

Q2: How can I copy over the files to a new directory once the proper files are chosen?

Format: 'OUS_*_*_*.html'

Attempt:

Code:
# Does not select two most recent files
find . -type f $fileNameFormat -exec cp -pf {} $testDestDir \;


Last edited by mattkoz; 02-23-2015 at 12:57 PM.. Reason: Added missing character in code example
# 2  
Old 02-23-2015
Well, you almost have it. Your logic just needs to extend and think "I can get the names of the files, now I need to copy each on in turn to the target area." This is repeating a process in a loop. There are several ways to code this:-

Code:
for file in `ls -1t | head -n 2`
do
   cp $file target_direcotry
done

Code:
ls -1t | head -n 2 | while read file
do
   cp $file target_directory
done

Do either of these help?


You might get away with a one-line command:-
Code:
cp `ls -1t | head -n 2` target_directory



For the second part, if you need to specify a file pattern, use it as an arguement to the ls command in whichever structure above helps. That part would then become:-
Code:
.... ls -1t OUS_*_*_*.html | head -n 2 ......



Let us know how you get on.

Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-23-2015
This is exactly what I am looking for, thanks Robin. This is my first script so I have yet to get down the proper syntax for control structures and variable handling.

I am having an issue while testing the given code:
Code:
# Select two most recent files with corresponding file name format and 
# copy to destination directory

for file in 'ls -1t OUS_*_*_*.txt | head -n 2'
do 
	cp "$file" $testDestDir
done

I am getting this error:

Code:
cp: cannot access ls -1t OUS_*_*_*.txt | head -n 2: No such file or directory

Do you know why I am getting this error?


Edit: When removing the quotation marks from $file I get the following errors:

Code:
cp: cannot access ls: No such file or directory
cp: cannot access -1t: No such file or directory
cp: cannot access |: No such file or directory
cp: cannot access head: No such file or directory
cp: cannot access -n: No such file or directory
cp: cannot access 2: No such file or directory


Last edited by mattkoz; 02-23-2015 at 03:49 PM.. Reason: Included more detail about error
# 4  
Old 02-23-2015
rbatte1 was using backticks ` whilst you replaced those with the apostrophe (or single quote) ' . The meaning of both is remarkable different - the former introduces command substitution, the latter introduces, say, "constants" (text that is not expanded by the shell).
Please be aware that backticks are deprecated, use $(...) instead.
All of above you'll find in man bash (or whatever shell you are using).
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-24-2015
Quote:
Originally Posted by mattkoz
This is exactly what I am looking for, thanks Robin. This is my first script so I have yet to get down the proper syntax for control structures and variable handling.

I am having an issue while testing the given code:
Code:
# Select two most recent files with corresponding file name format and 
# copy to destination directory
 
for file in 'ls -1t OUS_*_*_*.txt | head -n 2'
do 
    cp "$file" $testDestDir
done

I am getting this error:

Code:
cp: cannot access ls -1t OUS_*_*_*.txt | head -n 2: No such file or directory

Do you know why I am getting this error?

Edit: When removing the quotation marks from $file I get the following errors:

Code:
cp: cannot access ls: No such file or directory
cp: cannot access -1t: No such file or directory
cp: cannot access |: No such file or directory
cp: cannot access head: No such file or directory
cp: cannot access -n: No such file or directory
cp: cannot access 2: No such file or directory

Hello mattkoz,

Could you please try following command, it may help you.
Code:
ls -1t OUS_*_*_*.txt | awk 'NR==1||NR==2{print "cp " $NF " path_to_directory"}'

Above command will only print the command as follows.
Code:
cp OUS_123112343567_wertc_1234.txt path_to_directory
cp OUS_123123~4467543_wertc_1234.txt path_to_directory

If happy with results then you can use following command.
Code:
ls -1t OUS_*_*_*.txt | awk 'NR==1||NR==2{print "cp " $NF " path_to_directory"}' | sh

NOTE: I have just shown an example of files.


Thanks,
R. Singh
# 6  
Old 02-24-2015
Quote:
Originally Posted by RudiC
rbatte1 was using backticks ` whilst you replaced those with the apostrophe (or single quote) ' . The meaning of both is remarkable different - the former introduces command substitution, the latter introduces, say, "constants" (text that is not expanded by the shell).
Please be aware that backticks are deprecated, use $(...) instead.
All of above you'll find in man bash (or whatever shell you are using).
Thank you, you are exactly right. Did not use backtick. I have another somewhat related question.

I am attempting to cycle through files in a directory in order to mail them to a email one at a time. The filenames have spaces however and I get a similar error to the previous one I listed, involving the backticks. What I assume is happening is that the spaces are being interpreted as spaces between multiple file names rather than the spaces within the filename.

Code:
for file in `ls -1t OUS*.htm* | head -n 2`
do
	(echo "Body of text 4"; uuencode `$file`) | mailx -m -s "Test" $testEmail
done

Errors:
Code:
/home/koczmj/OutageReportScript.ksh[61]: OUS:  not found
/home/koczmj/OutageReportScript.ksh[61]: Complete:  not found
/home/koczmj/OutageReportScript.ksh[61]: Scheduled:  not found
/home/koczmj/OutageReportScript.ksh[61]: Job:  not found
/home/koczmj/OutageReportScript.ksh[61]: List.htm:  not found
/home/koczmj/OutageReportScript.ksh[61]: OUS:  not found
/home/koczmj/OutageReportScript.ksh[61]: Scheduled:  not found
/home/koczmj/OutageReportScript.ksh[61]: Job:  not found
/home/koczmj/OutageReportScript.ksh[61]: List:  not found
/home/koczmj/OutageReportScript.ksh[61]: Filtered:  not found
/home/koczmj/OutageReportScript.ksh[61]: by:  not found
/home/koczmj/OutageReportScript.ksh[61]: Criteria.htm:  not found

How can I properly format my code so the spaces within the filenames are read correctly?

The filenames should be:
OUS Complete Scheduled Job List.htm
OUS Scheduled Job List Filtered by Criteria

Thanks!
# 7  
Old 02-24-2015
Look carefully at rbatte1's post #2 and try
Code:
ls -1t "OUS*.htm*" | head -n 2 | while read file
do
   cp $file target_directory  ( ... or whatever you want to do with it ... )
done

And, don't use backticks around $file - they're NOT the panacea!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rsync added new folders after copy?

Hi guys, Don't really know much about unix or anything, just starting to mess around a little bit to have more understanding in general. So, I tried using rsync to copy my macbook pro backup/clone from an external drive I have to another external drive. I ended up using... "sudo rsync -a... (1 Reply)
Discussion started by: cbjeebs
1 Replies

2. Shell Programming and Scripting

How to select all files added to a directory in the past 5 mins (HP-UX)?

Hey everyone, I need to select all files that were added to a specific directory in the past 5 mins and copy them over to a different directory. I am using HP-UX OS which does not have support for amin, cmin, and mmin. B/c of this, I am creating a temp file and will use the find -newer command... (7 Replies)
Discussion started by: mattkoz
7 Replies

3. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

4. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

5. UNIX for Dummies Questions & Answers

How to archive old files from the recently added 10 files?

Hi there, I am very new to unix and having trouble with a fairly simple statement: cd /user ls -t -c1 | sed -ne '11,$p' | mv xargs archive/ What I want the code to do is sort all files in a directory by timestamp, select all of the files after the 10th file, and then move those files... (3 Replies)
Discussion started by: DSIReady
3 Replies

6. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

7. Shell Programming and Scripting

subtring and copy recently files and another folder

Hi I have the following files A320_ZONAL_v24_-_AMM_Suffix_jess_2011-05-31.xls Jun 10 17:21 A320_ZONAL_v24_-_AMM_Suffix_jess_.xls Nov 10 17:21 A320_ZONAL_v24_-_AMM_Suffix_jess_2011-03-31.xls Nov 23 20:21 And I need only the more recent one using UNIX timestamp... (2 Replies)
Discussion started by: javeiregh
2 Replies

8. Shell Programming and Scripting

rebuild/update fuppes database if files are added/removed from directory

The title says it all. I have a upnp server running fuppes that is connected to my xbox360. In order to see the files on the xbox360 i have to manually update and rebuild the database anytime i add or remove files. I have tried cron jobs to do it every 20 min which works, but if I am streaming... (0 Replies)
Discussion started by: tr6699
0 Replies

9. Shell Programming and Scripting

Find recently updated files in home directory

Is there a shell command that will allow me to list index files in the /home directory for all users on a server that have been updated within the past 24 hours? (e.g. index.htm .html .php in/home/user1/public_html /home/user2/public_html /home/user3/public_html etc ) (2 Replies)
Discussion started by: Kain
2 Replies

10. Shell Programming and Scripting

Trying to Copy Files Changed Recently

I have been toying around with a script that will copy all files altered in a development directory over to a testing directory and have been trying to construct the command to meet my needs. Basically I am using find in a directory to see what files have changed over the past 24 hours. Then if... (4 Replies)
Discussion started by: scotbuff
4 Replies
Login or Register to Ask a Question