Using ls command to display files for range of values only


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using ls command to display files for range of values only
# 8  
Old 06-10-2013
Quote:
Originally Posted by bakunin
I don't know bash, but i wonder: does this work like a glob or like parameter expansion? In case of the latter there should be an error message if one of the files is missing, yes?

bakunin
No on both counts. With default settings a glob expression that doesn't match any files will be left unexpanded and if ls doesn't find a file with that name, it will produce a diagnostic message.

In the Korn shell {x..y} is called a sequence expression. Depending on what comes before or after the sequence expression, each element of the expanded sequence could then be treated as a globing (filename pattern matching) expression. If the filenames you're trying to match have leading zeros in a numeric sequence, your can also specifiy a printf-like format to control display of the elements in the sequence. And if you don't want each element you can also specify an increment as in the following example:
Code:
printf "%s\n"  PROFILE_TXN_617{7..12..2%02d}.dat

produces:
Code:
PROFILE_TXN_61707.dat
PROFILE_TXN_61709.dat
PROFILE_TXN_61711.dat

If you try:
Code:
ls -d PROFILE_TXN_{61649..61979}.dat

and some of the files don't exist, ls will write a list of files that do exist to standard output and write a diagnostic to standard error for each pattern that is not an existing file. The diagnostic messages can be discarded using:
Code:
ls -d PROFILE_TXN_{61649..61979}.dat 2>/dev/null

Of course, if the expansions exceed ARG_MAX, you'll have the same problem you'd have with the expansion of a globing expression that matches too many files...
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 06-10-2013
Quote:
Originally Posted by shamrock
In that case try this...
Code:
ls -latr | grep -E "PROFILE_TXN_61(649|[6-9][5-7][0-9])"

The OP wanted a simple list of files, not the long listing format of ls output. I didn't see any indication that the OP wanted the files sorted in reverse order by timestamp instead of by filename. And, since the desired filenames all start with "P", there's no need for ls's -a option.

The OP wanted filenames containing the numbers 61649 through 61979 inclusive, the pattern in the grep above will exclude several desired files from the output including (but not limited to) 61900-61949 and 61680-61699.

If the OP's shell doesn't support sequence expansions like {61649..61979}, or if the expansion of PROFILE_TXN_{61649..61979}.dat on the ls command causes its invocation to fail because ARG_MAX was exceeded, I believe the following ls and grep combination will work:
Code:
ls | grep -E "PROFILE_TXN_61(649|6[5-9][0-9]|[78][0-9][0-9]|9[0-7][0-9]).dat"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display files within a range

My script reads all the log files in a directory, the user would enter a range and the script would only display the files within those range. Here's my code: #!/bin/bash LOG_FILES=("Sandbox/logs/*") for file in ${LOG_FILES}; do echo $file; done ------------- for i in $(seq $1 $2); do... (1 Reply)
Discussion started by: Loc
1 Replies

2. Shell Programming and Scripting

Create range of values and count values.

Gents, It is possible to generate a range of values according to column 1 and count the total of rows in the range. example input 15.3 15.5 15.8 15.9 16.0 16.1 16.8 17.0 17.5 18.0 output desired 15.0 - 15.9 = 4 (10 Replies)
Discussion started by: jiam912
10 Replies

3. Shell Programming and Scripting

Convert Column Values to a Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example: "Column A" 1 2 3 4 10 12 14 15 16 17 18 "Column B" 1 4 5 6 (4 Replies)
Discussion started by: newbio
4 Replies

4. UNIX for Dummies Questions & Answers

How to display lines by range specified?

My input file is N3*123 ABC FLATS~ REF*D9*10000000001~ N3*223 ABC FLATS~ REF*D9*10000000002~ N3*323 ABC FLATS~ REF*D9*10000000003~ N3*423 ABC FLATS~ REF*D9*10000000004~ N3*523 ABC FLATS~ REF*D9*10000000005~ N3*623 ABC FLATS~ REF*D9*10000000006~ N3*723 ABC FLATS~ REF*D9*10000000007~ N3*823... (2 Replies)
Discussion started by: nsuresh316
2 Replies

5. Shell Programming and Scripting

Display data from a range of dates

I have a data in a file called SCHED which has 5 columns: sched no, date, time, place and remarks. The image is shown below. http://dl.dropbox.com/u/54949888/Screenshot%20from%202013-01-02%2002%3A42%3A25.png Now, I want to display only the schedules which fall under a certain date range which... (2 Replies)
Discussion started by: angilulu
2 Replies

6. UNIX for Dummies Questions & Answers

Display dates within a given date range

Hi All, I have a requirement to display all the dates within the provided (through user input) date range. For eg: If I enter 28012009 (as From date in the format 'DDMMYYYY') and 02022009(as To date in the format 'DDMMYYYY'), the output should be all dates occuring between the from and to... (11 Replies)
Discussion started by: sunpraveen
11 Replies

7. Shell Programming and Scripting

display the file with in the date range

Hi All, I want a shell script which can display the file with in the date range. For Example I have 15 files with the following format abc_01-01-2009.txt to abc_15-01-2009.txt. Now I want to have the files between 4th of jan to 12th files. How can I acheive this. Advance... (1 Reply)
Discussion started by: fareed_do
1 Replies

8. Solaris

What command can display files in a tree?

Is there a command that displays a certain path of files in a tree just like the dos command 'tree'? (17 Replies)
Discussion started by: Bradj47
17 Replies

9. UNIX for Dummies Questions & Answers

How to display files that have been modifed between a given date range

Hi, I am new to Unix and was trying different ways of how to display the list of file names modified between a given date range in sorting order.I will get the fromdate and Todate from the browser, I need to display the list of all the file names that are modified between the given date... (1 Reply)
Discussion started by: prathima
1 Replies

10. UNIX for Dummies Questions & Answers

How to Display a range of values in the output of cat

When I use this command I get an output of some numbers cat ac.20070511 | cut -d" " -f19 Is there any way for me to display only the numbers that are greater than 1000 but not all the numbers in the ouput. Can any one help me with this. :) (8 Replies)
Discussion started by: venu_nbk
8 Replies
Login or Register to Ask a Question