Creating a string array from a directory listing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a string array from a directory listing
# 1  
Old 08-08-2008
Creating a string array from a directory listing

Hi all,

I'd like to create a string array from a long directory listing, extracting only files last modified on a specific date (e.g. Aug 08). I tried the following:

aug8=`ls -ltr |grep 'Aug 08'`

The result was an array (I think) but all of the output from the listing went to the first element of the array, so that:

echo ${aug8[0]}

displayed everything and:

echo ${aug8[1]}

displayed nothing.

Any suggestions as to how I can get each filename in the directory listing to be a single element in the "a8" array?

FYI, I'm in Korn Shell.

THANKS!
# 2  
Old 08-08-2008
The reason why your command failed was that the shell was not aware that "aug8" should be an array. That you found the output of "ls" in aug8[0] was just because for every variable this is the case:

Code:
willy="x"
print - $willy[0]   # will yield "x"

If you want to assign the content to an array you would have to use the "set -A" subcommand in the shell. Alas, the output of "ls -ltr" contains not only the filenames but also a lot of other information which is why it will have to be trimmed before. I have not Unix-system at hand writing this, but it would be something like the following:

set -A aug8 "$(ls -ltr | sed -n '/Aug 08/ s/ */ /gp' | cut -d' ' -f12)"

There are two problems with this approach anyways: first, while evaluating the commandline the subshell will be executed and the command be replaced by its output. UNIX-commandlines have a maximum length (4096 characters) which could be exceeded if there are enough files with long enough names.

Secondly, ksh-arrays have a maximum number of elements, which is 1024. This might be not enough if the directory contains enough matching files (although presumably the other limit would be hit first).

The first limitation could be circumvented by using a loop to assign the array elements:

Code:
(( iCnt=0 ))
ls -ltr | sed -n '/Aug 08/ s/  */ /gp' | cut -d' ' -f12 | while read filename ; do
     aug8[$iCnt]="$filename"
     (( iCnt += 1 ))
done

But the second limitation cannot be overcome: arrays cannot exceed 1024 elements. Still i cannot understand what it would be necessary to create the array in first place. If you want to process these files one after the other apply the logic from my second example and instead of assigning the content of variable $filename to an array element do some processing with it:

Code:
ls -ltr | sed -n '/Aug 08/ s/  */ /gp' | cut -d' ' -f12 | while read filename ; do
     do_something "$filename"
done

I hope this helps.

bakunin
 
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

Hi, I have a directory with a bunch of files say around 150K. I want the directory's path and the filenames printed to a text file. Example: If I am in the directory /path/test and the files in this directory are My output file should be like this Thanks in advance ----------... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

2. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

3. Shell Programming and Scripting

Directory Listing Help

i have searched through this site and have found some useful information but i'm struggling with one thing. In my script i am created a start and end file so I can get a listing of the files within those two files. However I want to exclude any sub-directories in this listing. Below are the... (8 Replies)
Discussion started by: J-DUB
8 Replies

4. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

5. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

6. UNIX for Dummies Questions & Answers

Sorting Directory Listing

If I do an ls -l on a directory I get this: -rw-r--r-- 1 root other 5248094 Jun 24 03:56 monitor.log.7 -rw-r--r-- 1 root other 5248303 Jul 11 11:19 ct.log.1 -rw-r--r-- 1 root other 5248907 Jun 29 06:01 ct_monitor.log.5 -rw-r--r-- 1 root other 5249042 Jun 19... (1 Reply)
Discussion started by: Sepia
1 Replies

7. UNIX for Dummies Questions & Answers

How can i get directory listing?

Hai friends is there any command in unix that display only directories... (I have 5 directories in my home directory, and i also have some files along with directories...But when i tried to show the directory listing using the command ls -d i wasn't presented by the directory listing...Please... (2 Replies)
Discussion started by: haisubbu
2 Replies

8. UNIX for Dummies Questions & Answers

Full Directory Listing...

Is there a way of listing everything under a directory. So for example if you wanted to know everything under the USR directory you would get all the sub directories and files in those directories as well as the file directly under the USR directory. I would imagine that you could do this... (5 Replies)
Discussion started by: B14speedfreak
5 Replies

9. UNIX for Dummies Questions & Answers

Timestamp in directory listing

Hi, I need a help. I want to see all the files in the directory with the Time Stamp. I use the following command. $ls -lt This displays the files with time stamp, but not all the files. Only last few months, the files are displayed with timestamp, the old files are only have dates. ... (2 Replies)
Discussion started by: vijashok
2 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