Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-16-2009, 03:50 PM   #1
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, 04:04 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and Welcome to LQ!

Well, you can do it either using the recursive option of ls:
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, 04:08 PM   #3
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, 04:14 PM   #4
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

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, 05:22 PM   #5
Registered: Feb 2009
Posts: 51

Original Poster
Rep: Reputation: 16

Edit: I fixed the duplicate problem.

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



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

but now if instead of doing:

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

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:

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

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 06:01 PM.


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

All times are GMT -5. The time now is 05:34 PM.

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