LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-01-2012, 02:50 PM   #1
meet10may
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
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
 
Old 06-01-2012, 03:46 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-01-2012, 04:41 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
 
Old 06-02-2012, 12:54 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 06-02-2012 at 12:56 PM. Reason: That darned text-duplicating problem again!
 
Old 06-04-2012, 04:29 AM   #5
meet10may
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-04-2012, 09:45 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by meet10may View Post
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 View Post
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.
 
Old 06-04-2012, 11:48 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
  


Reply

Tags
array


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy files after search in array from this directory to another directory! phanvinhgiap Programming 4 01-04-2009 06:48 PM
File server setup...what directory to store shared files glenn69 Linux - Server 1 07-08-2008 03:42 PM
Store into a file the list of executable files of a system directory jianelisj Linux - Newbie 3 03-17-2008 12:26 PM
To store N numbers in an array som_kurian Programming 10 12-16-2007 03:31 AM
How to deal with huge swp files such as .store.log.2.swp from squid directory? Niceman2005 Linux - General 2 11-01-2005 07:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration