Parse find input into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse find input into array
# 1  
Old 01-02-2013
Parse find input into array

I need help parsing the output of find into an array. I need to search 3 directories and find all files older than 31 days old. This is what I have so far.

Code:
TIME=" -maxdepth 1 -mtime +31"
DIR1="/dir1/"
DIR2="/dir2/"
DIR3="/dir3/"

FIND_DIR1=$(find ${DIR1}${TIME})
FIND_DIR3=$(find ${DIR2}${TIME})
FIND_DIR3=$(find ${DIR3}${TIME})

IFS=' ' read -a array <<< "$FIND_DIR1"
for element in "${array[@]}"
do
    echo "$element"
done

When I echo the FIND_DIR variables, it prints out..
/dir1/file1 /dir1/file2 separated by what I'm assuming is either white space or a tab.

The issue I'm having is when I try to put the output of the find into an array, array index 0 has both /dir1/file1and/dir1/file2

I need array index 0 to be file 1 and array index 1 to be file 2.

The above code will output /dir1/file1 for both array index 0 and array[@], it prints nothing when asked to print array index 1.

Thank you in advance.
# 2  
Old 01-02-2013
try in script:
Code:
array=($($FIND_DIR1))
for element in "${array[@]}"
do
    echo "$element"
done

# 3  
Old 01-02-2013
It says /dir1/file1: Permission denied, I put sudo before it as well.
# 4  
Old 01-02-2013
Another approach in BASH:
Code:
#!/bin/bash

DIR="/dir1/"
idx=0

FIND_DIR=$( find ${DIR} -type f )

for file in $FIND_DIR
do
  array[$idx]="$file"
  idx=$(( idx + 1 ))
done

end_idx="${#array[@]}"

for idx in $( seq 1 $end_idx )
do
  echo ${array[idx]}
done

# 5  
Old 01-02-2013
You could do something like this (using non-standard shell syntax):

Code:
bash-4.2$ mkdir dir{1..3}
bash-4.2$ touch -t 201211010000 dir{1..3}/file{1,2}
bash-4.2$ ls -l dir{1..3}
dir1:
total 0
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file1
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file2

dir2:
total 0
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file1
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file2

dir3:
total 0
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file1
-rw-r--r-- 1 sysadmin None 0 Nov  1 00:00 file2

Given the directory structure and the files above, the following code:

Code:
_find_opts=( 
  -maxdepth 1 
  -mtime +31
  )
_dirs=( 
  dir1 
  dir2 
  dir3 
  )

for d in "${_dirs[@]}"; do
  eval "__find_$d=( $( find "$d" "${_find_opts[@]}" ) )"
done

for fd in ${!__find_*}; do
  eval "_files=( "\${$fd[@]}" )"
  printf 'files in %s:\n' "$fd"  
  for f in "${_files[@]}"; do
    printf '%s\n' "$f"
  done
done

Produces:

Code:
files in __find_dir1:
dir1/file1
dir1/file2
files in __find_dir2:
dir2/file1
dir2/file2
files in __find_dir3:
dir3/file1
dir3/file2

This is only for illustration! Most probably the eval ... parts could be avoided ...
I don't believe you really need a separate structures/arrays. If you tell us what you actually need to do,
we could give a more appropriate solution.

Last edited by radoulov; 01-02-2013 at 06:46 PM..
# 6  
Old 01-03-2013
@rdrtx1
I changed the permissions and got it run but it produces the incorrect output, if it finds one file it prints one newline, if two files are found it prints two newlines etc. Also I fail to see where you are taking the individual files and putting them into an array.

@bipinajith
I tried something like this earlier and it did not work either. When I do this is does not split up my string either. Array index one still prints out the full string, I do not see where this splits the string up either.

@radoulov
I do not fully understand your code because I am very new to bash, I just need to know exactly how to split a string from find into an array.

Quote:
If you tell us what you actually need to do,
we could give a more appropriate solution.
What I am trying to do:
-find files older than 31 days from multiple directories
-store those multiple files into an array based on which directory they are from
-iterate through those arrays and ask the user if there are any files they want to keep
-deleted all the files that the user does not want kept

I am stuck on the 2nd step though, if my find result produces one hit, everything works fine, but if more than one item is found, then both of those items are stored as one index. I have tried many options and the closest I have come is storing one of the files as array index one, but array index two remains empty, leaving out the other files.

I do not understand how to split a string at whitespace and then get it into an array.

Is there a pipe I could use for this?
# 7  
Old 01-03-2013
Code:
I do not understand how to split a string at whitespace and then get it into an array.

With bash >= 4, if I recall correctly:
Code:
$ _string='a b c'
$ read -a _array <<< "$s"
$ declare -p _array
declare -a _array='([0]="a" [1]="b" [2]="c")'

Otherwise (zsh, bash, ksh93):
Code:
_array=( $_string )

If you need standard code, you can use positional parameters (you may need to save the existing ones first):

Code:
$ set -- $_string
$ # now you have all elements in "$@", single elements
$ # could be accessed using $1, $2 ... etc.

Quote:
What I am trying to do:
-find files older than 31 days from multiple directories
-store those multiple files into an array based on which directory they are from
-iterate through those arrays and ask the user if there are any files they want to keep
-deleted all the files that the user does not want kept
1. You can find them with a single command: find dir1 dir2 ... -mtime ....
2. Why do you think, you need to do this?
3. You could iterate through the files using a while read loop and do whatever you need to do inside the loop:

Code:
find ... | while IFS= read -r; do #filename is in $REPLY ...

This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse section of csv into array

In the awk below I am trying to parse the Sample Name below the section. The values that are extracted are read into array s(each value in a row seperated by a space) which will be used later in a bash script. The awk does execute but no values are printed. I am also not sure how to print in a row... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Parse input of two files to be the same in awk

I have two files that I am going to use diff to find the differences but need to parse them before I do that. I have include the format of each file1 and file2 with the desired output of each (the first 5 fields in each file). The first file has a "chr" before the # that needs to be removed. I... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Array from cli input

I need to create a bash array from the command line parameters. I only know how to do it when I know the number of parameters. But what do I do when I dont know the number of parameters? (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

Bash Script for parse input like option and value

I would create a bash script than parse like this: test.sh -p (protocol) -i (address) -d (directory) I need retrive the value after -p for example... understand??? I hope... thanks (6 Replies)
Discussion started by: ionral
6 Replies

5. Shell Programming and Scripting

Parse input -AWK

Input File Defined configuration: cfg: CLL_DCC_Fabric_A BTS00P21; BAU_AP00P01QC; BAU_LGSCNJP02; BAU_TS00P20; BAU_DSMSM14; BAU_HT00P02; BAU_DSMSM13; BAU_HT00P01; cfg: CX0014_list BAU_TS00P20; BAU_NYP_PRODIAD1_CJ;... (5 Replies)
Discussion started by: greycells
5 Replies

6. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies

7. Shell Programming and Scripting

parse long input parameters

anybody know a nice way to parse long input parameters such as --path /dir1/dir2/ (see below). Now I have more than 10 input parameters and it's confusing having parameters like -q something, I would prefer longer ones case $OPTKEY in --path) M_PATH=$OPTARG ;; -s) ... (3 Replies)
Discussion started by: larne
3 Replies

8. Shell Programming and Scripting

input text from file into 2d array

Hi all I have a little brainscratcher here. I want to draw a pie chart from data in a text file. The drawing of the graph works fine, if I insert the data manually into a 2d array. Now I want to pull the data from a text file (which was created using a uniq -c command) see sample below.... (2 Replies)
Discussion started by: pietie
2 Replies

9. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

10. Shell Programming and Scripting

parse a file and fill an array with it

Hi Guys, I am trying to do a file parse which is something like config file: machines= sha1 sha2 sha3 sha4 The bash script should be supporting upto 64 such machines what I want is to place the machines in an array and then use the array to ssh to each machine. The script I worte ... (11 Replies)
Discussion started by: jbmukund
11 Replies
Login or Register to Ask a Question