LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   store files in a directory in an array (https://www.linuxquestions.org/questions/linux-newbie-8/store-files-in-a-directory-in-an-array-948033/)

meet10may 06-01-2012 02:50 PM

store files in a directory in an array
 
Hi all,
I am a newbie to shell scripting.
I want to store the files of the directory in an array and then access these files from the array using for loop.

I want to try the readdir command and i have written a bit of code.

#!/bin/bash
cd /home/tanmay/Desktop/BET/T1
dir=opendir
files=readdir dir;

Currently the output of this code is that,it successfully displays all the files on the terminal.But i am not able to store it in an array.Can you please help me in storing the files(the files variable) in an array?

Also can you suggest some online material/tutorials where i can read about this shell scripting.

Thanks

colucix 06-01-2012 03:46 PM

Sincerely, I've never heard about the opendir and readdir commands, but provided they are available and work as expected, your script should be:
Code:

#!/bin/bash
cd /home/tanmay/Desktop/BET/T1
dir="$(opendir)"
files=( $(readdir "$dir") )

The second line of code assigns the variable dir using command substitution. The third lines uses command substitution as well, but inside the array assignment syntax. Here are two guides you might find useful (other member could suggest a lot more):
http://tldp.org/LDP/Bash-Beginners-Guide/html/
http://linuxcommand.org/tlcl.php (see the online version)
Hope this helps.

suicidaleggroll 06-01-2012 04:41 PM

Do you actually need them in an array, or do you just need to loop over the files?

The simplest way (in my opinion) to loop over all of the files in a directory is:
Code:

for i in *; do
  # Do something with file, reference it with "$i", such as 'echo "$i"'
done


David the H. 06-02-2012 12:54 PM

Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

I have never seen opendir/readdir either, and they certainly don't exist in bash. There are C library functions with those names, but I can't find them as stand-alone commands anywhere on my system or in the apt repositories. Could you explain what they are and where you learned about them?

In any case, the OP code does not actually work at all. The only reason you're getting the output you do is due to this line:

Code:

files=readdir dir;
This line first sets the variable "files" to the literal text string "readdir"; and then (since it prefixes it directly) passes that value to the environment of the dir command before executing it, instead of setting it globally. dir is simply an alias for ls, so all your script is doing is listing the contents of the current directory.

As for colucix's suggestion here:

Code:

files=( $(readdir "$dir") )
Command substitution suffers the same issue of word-splitting that variable substitution does, so files with whitespace in them will not be stored properly in the array.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


The proper way to access files is generally with simple globbing. globbing filename expansion happens at the end of the parsing order, after word-splitting, and so isn't subject to that weakness. You can use it to populate an array, or directly in a loop, as in suicidaleggroll's example.

Code:

cd directory

files=( * )

for file in "${files[@]}"; do
        echo "$file"
done


meet10may 06-04-2012 04:29 AM

Thanks for the help..For the opendir() command i found it on http://linux.about.com/library/cmd/blcmdl3_opendir.htm
and also i can see its manual in my bash when i write man opendir in my bash.

Also, David's code give me the list of all the files. How can i access each and every files in the directory? Something like if i store all the filenames in an array named file and then access each file as file(1), file(2) etc.

suicidaleggroll 06-04-2012 09:45 AM

Quote:

Originally Posted by meet10may (Post 4694979)
Thanks for the help..For the opendir() command i found it on http://linux.about.com/library/cmd/blcmdl3_opendir.htm
and also i can see its manual in my bash when i write man opendir in my bash.

Notice the #include statements in both that link and the man page? That's a dead giveaway that it's a C program, not a bash script.

Quote:

Originally Posted by meet10may (Post 4694979)
Also, David's code give me the list of all the files. How can i access each and every files in the directory? Something like if i store all the filenames in an array named file and then access each file as file(1), file(2) etc.

Are you talking about the last code in his post? That works the same as the code I posted above his as well. The reason it's only printing the names is because the code only prints the name inside the loop. It's up to you to do something else inside the loop, since you haven't told us what you're trying to do, we can't write it for you, the most we can do is set up the loop for you and do something basic inside, like print the name of the file, which is what he's done.

David the H. 06-04-2012 11:48 AM

If you want to learn how to script, don't just flail around trying stuff at random and asking us how-to questions, find a real tutorial and start studying for yourself.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

I recommend starting with the BashGuide, or perhaps linuxcommand. They'll walk you through the basic concepts.


All times are GMT -5. The time now is 05:54 AM.