Read filenames in sorted order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read filenames in sorted order
# 1  
Old 02-01-2012
Read filenames in sorted order

Hi ,
My requirement is to scan a directory for file names with LTR.PDF*
and send those files via ftp to another server one by one.

Now the the problem is file names are like LTR.PDF ,LTR.PDF1 ,LTR.PDF2.....LTR.PDF10..upto 99
and these needs to be sent in sorted order.
is there a way to get the files in sorted order based on file names in for loop

Code:
for file in $BASEDIR/output/$LTRNAMELTR.PDF*
do
# The following check is necessary; if no files end with ltr*.afp
# then $file will be assigned "ltr*.afp"
if [ ! -f $file ]
then
echo " No print LTR PDF File created"
break
fi
FILENAME=${file%%.afp}
BASEFILENAME=$( basename $FILENAME )
ftp.......
done

quick reponse wil be much appreciated..thanks

Moderator's Comments:
Mod Comment Use code tags, please!

Last edited by radoulov; 02-01-2012 at 07:32 AM..
# 2  
Old 02-01-2012
i guess, -1 (numeric one) in ls command will give sorted order

Code:
ls -1 *.afp

# 3  
Old 02-01-2012
thanks for the quick reply..am pretty new to unix scripts...
how to redirect the output of ls -1 into for loop or to any othe looping construct
# 4  
Old 02-01-2012
Quote:
Originally Posted by itkamaraj
i guess, -1 (numeric one) in ls command will give sorted order

Code:
ls -1 *.afp

Are you sure? This indeed will give sorted order but on Strings.
Hence,

eon.1, eon.2, eon.3, eon.10, eon.11, eon.99 etc, when passed thru ls -1, the output would be:

Code:
 
eon.1 
eon.10
eon.11
eon.2
eon.3
eon.99


So the above won't work in the current query.
# 5  
Old 02-01-2012
Code:
 
$ ls -1 | sort -tF -k2 -n
LTR.PDF
LTR.PDF1
LTR.PDF2
LTR.PDF10
LTR.PDF11
LTR.PDF20


Code:
 
ls -1 | sort -tF -k2 -n | while read file
do
# your if condition
# your ftp code
done

These 2 Users Gave Thanks to itkamaraj For This Post:
# 6  
Old 02-01-2012
Quote:
Originally Posted by nishantrk
thanks for the quick reply..am pretty new to unix scripts...
how to redirect the output of ls -1 into for loop or to any othe looping construct

Try this:

[[ Assumption : basename of the file will remain the same. Only the sequence number will change ]]

Code:
 
for i in ' ' $(seq 1 99); do                        # Please include ' ' as this will give you .pdf without any numbering in them which I guess you want to include as well
   MYFILE=$LTRNAMELTR.PDF${i}
   if [[ -f ${MYFILE} ]]; then
      <<Do Your Stuff Here with this file>>
   else
       continue 
   fi
done

---------- Post updated at 04:06 PM ---------- Previous update was at 04:03 PM ----------

Quote:
Originally Posted by itkamaraj
Code:
 
$ ls -1 | sort -tF -k2 -n
LTR.PDF
LTR.PDF1
LTR.PDF2
LTR.PDF10
LTR.PDF11
LTR.PDF20


Code:
 
ls -1 | sort -tF -k2 -n | while read file
do
# your if condition
# your ftp code
done


I thought of this one, but this will fail if file path or name contains more than 1 dot '.' Smilie
This User Gave Thanks to knight_eon For This Post:
# 7  
Old 02-01-2012
yes.. but seems his requirement is

My requirement is to scan a directory for file names with LTR.PDF*
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. Shell Programming and Scripting

Uniq code in sorted order

Hi All, A small question: I have a file check.txt which looks like below: KRPROD2,2012-11-20 08:46:50:408,ODK3325688102 KRPROD2,2012-11-20 08:47:35:289,ODK3325688102 KRPROD2,2012-11-20 08:47:35:446,ODK3325688102 KRPROD2,2012-11-20 08:48:32:973,ODK3325689120 KRPROD2,2012-11-20... (8 Replies)
Discussion started by: irfanmemon
8 Replies

3. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

4. Shell Programming and Scripting

How to read the filenames with space character in it

Hi, My Requirement is to read the filenames which possess space charatcer in it and pass the same file name to the other shell script as input parameter. Please provide your valuable suggestion. (5 Replies)
Discussion started by: cnraja
5 Replies

5. UNIX for Dummies Questions & Answers

numerically sorted filenames

How do you sort filenames: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 as: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: kenneth.mcbride
6 Replies

6. Shell Programming and Scripting

read line in reverse order from file

dear all i want to read 5th no of line from last line of file. kindly suggest me possible ways. rgds jaydeep (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

7. Shell Programming and Scripting

executing code on files in the sorted order -help!

Say i have 2 files in the giving format: file1 1 2 3 4 1 2 3 4 1 2 3 4 file2 1 2 3 4 1 2 3 4 1 2 3 4 I have a PERL code (loaned by one of u -i forgot who - thanks!) that extracts the 2nd column from each file and append horizontally to a new file: perl -ane 'push @{$L->}, $F; close... (1 Reply)
Discussion started by: epi8
1 Replies

8. Shell Programming and Scripting

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. Shell Programming and Scripting

How to read filenames with space in between

Hi All, I need to read filenames with space in between in a for loop like Integration of utility projects Integration of hdf projects I copied these files from a windows system and as you know windows filename has spaces in between them. But while unix is reading the filename in a for... (12 Replies)
Discussion started by: dayanandra
12 Replies

10. UNIX for Advanced & Expert Users

Sorting filenames by order in another file

I would like to arrange /sort filenames ending with suffix like ".00XXXX". where X is a digit. However the order of arrangement is in a text file and is 'harpharzard'. e.g the text file may be like 002345 009807 001145 I wanted to avoid doing this using sql and exporting the text file back to... (4 Replies)
Discussion started by: samudimu
4 Replies
Login or Register to Ask a Question