How to store files names from a directory to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store files names from a directory to an array
# 1  
Old 05-17-2011
How to store files names from a directory to an array

Hi
I want to store the file names into an array.
I have written like this but I am getting error.

Code:
declare -A arr_Filenames
ls -l  *.log | set -A arr_Filenames $(awk '{print $9}')
index=0
while (( $index < ${#arr_Filenames[*]})); do
          Current_Filename=${arr_Filenames[index]}
          Log "index $index curr file name " ${Current_Filename}
          
          let  index="index + 1"
done

But I am getting error
declare: log_purge.sh 29: Unknown option "-a"

Last edited by radoulov; 05-17-2011 at 06:24 PM.. Reason: Code tags.
# 2  
Old 05-17-2011
What shell are you using?

This construct is fishy:
Quote:
ls -l *.log | set -A arr_Filenames $(awk '{print $9}')
That awk statement is out of place -- did you want to estract 9th column from ls output? Why do you use -l option then at all?
Try this:
Code:
arr_Filenames=(`ls *.log`)

or, if you really want to filter the output of ls command:
Code:
arr_Filenames=(`ls -l *.log | awk 'someAwkCommandHere'`)

The var=() construct will make 'var' an array. (In Bash, at least)

You can also assign into array in a following way:
Code:
$ ls -l  *log 
-rw-rw-r-- 1 user group  23230 May 17 10:19 Changelog
-rw-rw-r-- 1 user group 145060 May 17 12:01 config.log
$ ls -l  *log | while read -a arr ; do 
  echo "perms=${arr[0]} ; size=${arr[4]}" ;
done
perms=-rw-rw-r-- ; size=23230
perms=-rw-rw-r-- ; size=145060

Which will read each line of input being piped in into an array (-a switch of built-in read).
# 3  
Old 05-17-2011
I am using MKS Toolkit to execute the shell script.

Code:
ls -l  *.log | while read -a arr ; do 
  Current_Filename=${arr[8]} ;
  Log "Current_Filename $Current_Filename"
done

Error Unknown option "-a"


Code:
arr_Filenames=(`ls *.log`)

Error 43: syntax error: got (, expecting Newline

I am trying to list all files from the directory and delete which are two months old. My file has a f1_YYYY-MM-DD.log.
Wanted to delete which are two months old.

Thanks and Regards
Digambar
# 4  
Old 05-17-2011
OK. From what I understand, MKS toolkit supports different shells. What is the first line of your script? (starting with #!)

Quote:
am trying to list all files from the directory and delete which are two months old
This should work:
Code:
find . -type f -mtime +60 -exec ls {} \;

will execute 'ls -l <filename>' on all files inside current directory and all subdirectories that were modified more than 60 days ago. Look at 'man find' to check the 'mtime' option.
If you know the name format of your log files, you should specify that also to filter out old files that you might want to keep.
The following will remove all files named f1_YYYY-MM-DD.log inside the current dir (and any level deep), that were modified at least 60 days ago. You should test this without the '-exec rm {} \;' part first, to list the files matched by find; then when you are sure, run the whole thing to delete them.
Code:
find . -name "f1_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log" -type f -mtime +60 -exec rm {} \;

# 5  
Old 05-18-2011
I haven't used the MKS toolkit, but here's an interesting test that might shed light:
Code:
#!/bin/sh
DIR=/tmp/test
rm -rf $DIR;  mkdir -p $DIR;  cd $DIR  ||  exit 1
touch "ab cd" "ef gh" "ij kl"

a=()
for f in *; do
    a[${#a[@]}]="$f"
done

for f in "${a[@]}"; do
    echo "$f"
done

These are the results I get when I run it:
Code:
ab cd
ef gh
ij kl

This User Gave Thanks to KenJackson For This Post:
# 6  
Old 05-18-2011
Yes this is working.

Here is the code what I did.
Code:
cd $LOG_DIR
for f in *
do
let cnt="cnt + 1"
filename=$f
echo "$filename"
done
echo "total count $cnt"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

Edit names of files in a directory

Hi all, I have a directory with multiple (thousnads) of files, which are named this way ABCDEF.wo.im-1 OKRAME.ire.roi IOJEAFO01.irt.gfg IMNYBL05.REG.gkf I would like to keep the part of the name (everything before the first dot in the filename). The desired output: ABCDEF... (3 Replies)
Discussion started by: Error404
3 Replies

3. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

4. Shell Programming and Scripting

What amount of store occupy only files in a directory?

Hello I'm trying to do an exercise programming in bash where I have to get only the store of files in a directory but NOT all capacity of the directory. I probe with: du -sh "$directory"*` but I get all the capacity and I probe with ls command but I couldnt. Are there any way to get only files... (2 Replies)
Discussion started by: adiegorpc
2 Replies

5. Shell Programming and Scripting

renaming files from an array of names

I haven’t used Unix in over 25 years … and so I am at a loss for something that should be very simple. I have a lot of jpeg files (i.jpg) of students in a yearbook.. I also have an array name(i) of their names. I need to rename each “i.jpg” to “name(i).jpg”. I believe the ksh script... (11 Replies)
Discussion started by: chuckmg
11 Replies

6. Shell Programming and Scripting

how can i copy a list of files with different names into others directory have the same name

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (0 Replies)
Discussion started by: t17
0 Replies

7. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

8. Shell Programming and Scripting

Find Directory from array of file names with paths

I have a script that generates a variable with the location of a file and its complete path. What i want to do is to "cd" to the directory where that file is located using the path name of the file. GIS has absolutely failed me. For example when i run my script it generates a variable called... (1 Reply)
Discussion started by: Knome
1 Replies

9. Shell Programming and Scripting

Getting all the files modified today & store the names in a variable as , separated

Hi all, I have a question. I have a folder. I want to find the list of files that are modified today and store all those file names in a variable as comma separated values. I thought of using "find . -mtime 0" command to find the list of files modified today. Also to get those values of file... (10 Replies)
Discussion started by: ananthi_ku
10 Replies
Login or Register to Ask a Question