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:-
for file in `ls -1t | head -n 2`
do
cp $file target_direcotry
done
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:-
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:-
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:
# 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:
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:
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
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.
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:
/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
I apologize for all the questions, but this is still giving me trouble. I removed the quotation marks, but it is still not handling the spaces within the file name correctly.
ls -1t OUS*.htm* | head -n 2 | while read file
do
uuencode $file $file | mailx -m -s "Working" $testEmail
done
Errors:
OUS: No such file or directory
Null message body; hope that's ok
OUS: No such file or directory
Null message body; hope that's ok
Different attempt:
IFSSAVE=$IFS
IFS=$'\n'
for file in `ls -1t OUS*.htm* | head -n 2`
do
uuencode $file $file | mailx -m -s "New" $testEmail
done
IFS=$IFSSAVE
Errors:
OUS Complete Scheduled Job List.htm
OUS Scheduled Job List Filtered by Criteria.htm: No such file or directory
Null message body; hope that's ok