LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-16-2009, 02:50 PM   #1
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Rep: Reputation: 16
Question Browsing a directory recursively


Hi guys, I recently started getting a little bit more into bash programming and I've played a bit with it. Right now Im trying to create a code to list the content of a directory but if one of the files in this directory happens to be other directory then I want it to list the content into this directory as well recursively. I thought about recursion cause its the first thing that comes to my mind when I think of this specific output I want to obtain.

My question is, can I achieve this without recursion by using the command find which I think is recursive itself, Im not quite sure though, probably Im not even making sense, Im just reading and learning and I really want to get into it.

thanks in advance
 
Old 02-16-2009, 03:04 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
Hi and Welcome to LQ!

Well, you can do it either using the recursive option of ls:
Code:
ls -R
or the find command. Using find you can also specify the recursion level, that is how deep you want to descend into a directory tree. The options -maxdepth and -mindepth can help you to achieve this task. See man find for further details.
 
Old 02-16-2009, 03:08 PM   #3
bach-fiend
LQ Newbie
 
Registered: Feb 2009
Location: St Paul, Minnesota, USA
Distribution: Redhat Fedora 10; someday planet CCRMA
Posts: 11

Rep: Reputation: 0
You can use ls -lR for example. A number of unix commands can optionally recurse subdirectories. Some are cp chown chgrp chmod and ls.
This removes the need for programming. So would your suggestion of using the find command, but you might want to keep man or info open to review the many options find has.

The Free Software Foundation just LOVES options. Besides just printing out the names (or executing some command) for each file it finds satisfying your criteria, find also allows you to easily customize what information it gives for each thing found and how to format it.

You might want to glance at the xargs command too. Have fun!
 
Old 02-16-2009, 03:14 PM   #4
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
find A Versatile Tool

The short answer to your question is you can use find.

You can limit how far down find goes with -maxdepth n; you can ask find to find either files or directories and more with -type; and how you program depends on what you are find-ing and what you want to do with the results of find doing its thing.

If you are programming (shell scripts I am assuming), you have to have some place to put what find lists out. Using for and in is a way to do this. Here is one of many, many links you can find on bash

http://tldp.org/LDP/abs/html/loops1.html#EX22

It seems like a positive sign that you are experimenting with this stuff. You'll just have to trust me, otherwise I'll start telling one of those old, boring "Back at the Battle of Spitbrook ... " stories, which no one wants to hear. :-)

Good luck.
 
Old 02-16-2009, 04:22 PM   #5
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Original Poster
Rep: Reputation: 16
Unhappy

Edit: I fixed the duplicate problem.

Thanks a lot for the hints guys. I kinda got what I wanned:

Code:
#!/bin/bash

FILEPATH="."

function display()
{
	
	ls $FILEPATH | while read FILE;
	do
		if [ -d $FILE ]; then
			printf "\n"
			echo "$FILE"
                        #notice Im not typing $FILEPATH but FILEPATH
			FILEPATH="$FILE"
			echo "{"
			display
			echo "}"
			printf "\n"
		else
			echo "$FILE"
		fi
			
	done
}
this works perfectly, or at least how I wanned it to work for a min level.

but now if instead of doing:

Code:
echo "$FILE"
I want to get info out of this file(ionode, size in bytes, file type and name) using the command stat

Code:
echo " `stat --format='%n,%f,%i,%s' $FILE` "
then it works only for the files where Im running the script from but not for subsequent directories. It says it cant find the file. So I did a:

Code:
cd "$FILE"
after my if statement right after I echo the directory so I change my current working directory to this new one but it doesnt seem to work.

I also tried concatenating the FILEPATH

Code:
echo " `stat --format='%n,%f,%i,%s' $FILEPATH/$FILE` "
Any advise about better practices when it comes to bash programming Ill really appreciate it.

thanks in advance

Last edited by charlitos; 02-16-2009 at 05:01 PM.
 
  


Reply



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
[SOLVED] set ACL recursively on a directory ?? vikas027 Other *NIX 4 08-30-2008 05:41 AM
Search a string recursively under one directory say_hi_ravi Programming 6 07-09-2008 04:47 AM
how to find files in current directory only (not recursively) babu198649 Linux - Newbie 2 12-27-2007 01:30 AM
Using cp to copy directory permissions recursively? SirTristan Linux - Newbie 3 11-25-2007 08:36 AM
Changing owner of a directory recursively? fturcic Linux - General 2 02-01-2005 07:13 AM

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

All times are GMT -5. The time now is 02:57 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