Listing files in numerical order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing files in numerical order
# 1  
Old 02-22-2005
Listing files in numerical order

Hi, I'm trying to write a ksh script to copy a specified number of files from one directory to another.

The files are named in the convention <switchname>_log.<num> and the numbers are sequential single digit onwards. I figured I could find some parameter for ls which would list the files in correct order but having searched high and low i cannot figure this one out.

I consistently get -

<switchname>_log.1
<switchname>_log.10
<switchname>_log.11
<switchname>_log.12
<switchname>_log.13
<switchname>_log.14
<switchname>_log.15
<switchname>_log.16
<switchname>_log.17
<switchname>_log.18
<switchname>_log.19
<switchname>_log.2
<switchname>_log.20

etc.

I want to do a couple of things - firstly I want to write a script which moves the first numerically sequenced file from the directory, I will then put this in some sort of loop so it does it every 5 minutes for each of 12 directories.

Secondly I want to to be able to do this as a one off taking the first X number of each switch's files to copy elsewhere.

HELP PLEASE!
# 2  
Old 02-22-2005
something to start with:
Code:
#!/bin/ksh

ls *_log.*([0-9]*) | nawk -F'.' '{print $NF, $0}' | sort -n | cut -d ' ' -f2-

# 3  
Old 02-22-2005
Thanks, that gives me the listing I want.

How do I now grab the first list element (or the first X list elements) and cp it/them out?
# 4  
Old 02-22-2005
Quote:
Originally Posted by Steve_H
Thanks, that gives me the listing I want.

How do I now grab the first list element (or the first X list elements) and cp it/them out?
and here's something to start with:
Code:
#!/bin/ksh
for iter in $(ls *_log.*([0-9]*) | sed -e 's#^\([^_][^_]*\).*#\1#g'|sort -u)
do
   echo ${iter}
done

you can experiment with this and with the previous posting.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory listing in the order of size

I want to display directory listing in the order of size. I do not have -S option in my version of UNIX. So I wrote a simple script. But it takes "| sort -n -k5,5" part as file names. Any suggestion? #!/bin/ksh cmd='ls -l *.TXT | sort -n -k 5,5' set -x $cmd return 0 (7 Replies)
Discussion started by: Soham
7 Replies

2. Shell Programming and Scripting

File listing in Ascending order

I have a multiple file with the following name like. file_0.csv file_1.csv file_2.csv file_3.csv file_4.csv file_5.csv file_6.csv file_7.csv file_7.csv file_8.csv file_9.csv file_10.csv file_11.csv file_12.csv file_13.csv file_14.csv (2 Replies)
Discussion started by: rakesh_arxmind
2 Replies

3. Shell Programming and Scripting

how to extract data from numbered files using linux in the numerical order-

Hi experts, I have a list of files containing forces as the only number as follows. Force1.txt Force2.txt Force3.txt Force4.txt Force5.txt . . . . . . . . . Force100.txt I want to put all the data(only a number ) in these forces files in the file with the same order like 1,2,3 ..100 .... (2 Replies)
Discussion started by: hamnsan
2 Replies

4. Red Hat

Listing all child pid and deleting it in reverse order

Hi , My problem is that I am not able to list all process id of any process. If you see pstree command it shows many process id under https. But if I run ps command its not listing all the process id for httpd. It is just listing the PPID and immediate child process id only. I... (4 Replies)
Discussion started by: pratapsingh
4 Replies

5. UNIX for Dummies Questions & Answers

How to sort a column based on numerical ascending order if it includes e-10?

I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05 (6 Replies)
Discussion started by: evelibertine
6 Replies

6. Programming

Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this: 101,99,22HD,432,300HD I have tried using the sort routine like this: sort(mychans.begin(),mychans.end()); For some reason my channels are not being sorted at all. I was hoping someone might have some input that might... (2 Replies)
Discussion started by: sepoto
2 Replies

7. UNIX for Dummies Questions & Answers

moving/copying files in a numerical order

Hi I am newbie to unix scripting, but i have enough knowledge to understand. I have a specific questions like, I use to collect like 3500 files per experiment, each one named like data_001.img.. data_002.img data_003.img .... data_3500.img I would like to move every 12 files in the 3500... (3 Replies)
Discussion started by: wpat
3 Replies

8. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

9. Shell Programming and Scripting

Listing of all the files in the order of last update (including those in the subdiret

I want to list all the files in the current directory including those in the subdirectories as well as a single lot in the order of last updated. (Not as separate list given by ls -lRt). Any suggestion? Thanks (4 Replies)
Discussion started by: Soham
4 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question