tags:

views:

416

answers:

7

I know you can use find command for this simple job. But I got an assignment not to use find or ls and do the job. Please help....

A: 

The du command will list subdirectories recursively.

I'm not sure if empty directories get a mention, though

pavium
A: 

Try using

tree -d
Alberto Zaccagni
Why did I get a -1? Comment please.
Alberto Zaccagni
I didn't give you the -1, but it's obvious to me why you got it. Why not give as answer `alias bla ls ; bla`? The question that was asked was to write the algorithm, not find some *command* which does the same thing as `ls` but is not called `ls`.
vladr
@Vlad Romascanu: Oh... ok, but from "I got an assignment not to use find or ls and do the job" I understood that everything was good enough, except find and ls ^^
Alberto Zaccagni
On the other hand maybe his assignment has to run on Solaris or AIX or HPUX, not Linux. :) Let me assure you there is no `tree` command available by default on non-Linuces etc.
vladr
@Vlad Romascanu: hehehe ok, I understand your point ;)
Alberto Zaccagni
+1  A: 

Like Mark Byers said you can use echo * to get a list of all files in the current directory.

The test or [] command/builtin has an option to test if a file is a directory.

Apply recursion and you're done.

schot
As an alternative to `echo`, `for` will glob, so OP can use `for file in * ; do ... ; done`.
outis
A: 

you can do it with just the shell

#!/bin/bash
recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse /path

OR if you have bash 4.0

#!/bin/bash
shopt -s globstar
for file in /path/**
do
    echo $file
done
ghostdog74
-1 for complete answer to a homework question. OTOH, it has a bug...
just somebody
why don't you downvote the one with the tree command as well? or the one with echo, those are valid answers as well.
ghostdog74
A: 
$ function f { for i in $1/*; do if [ -d $i ]; then echo $i; f $i; fi; done }
$ mkdir -p 1/2/3 2/3 3
$ f .
./1
./1/2
./1/2/3
./2
./2/3
./3
Alex Brown
-1 if you're going to provide a complete answer to a homework question, make it a correct one. what about spaces in directory entry names?
just somebody
A: 

Below is one possible implementation:

# my_ls -- recursively list given directory's contents and subdirectories
# $1=directory whose contents to list
# $2=indentation when listing
my_ls() {
  # save current directory then cd to "$1"
  pushd "$1" >/dev/null
  # for each non-hidden (i.e. not starting with .) file/directory...
  for file in * ; do
    # print file/direcotry name if it really exists...
    test -e "$file" && echo "$2$file"
    # if directory, go down and list directory contents too
    test -d "$file" && my_ls "$file" "$2  "
  done
  # restore directory
  popd >/dev/null
}

# recursively list files in current
#  directory and subdirectories
my_ls .

As an exercise you can think of how to modify the above script to print full paths to files (instead of just indented file/dirnames), possibly getting rid of pushd/popd (and of the need for the second parameter $2) in the process.

Incidentally, note the use of test XYZ && command which is fully equivalent to if test XYZ ; then command ; fi (i.e. execute command if test XYZ is successful). Also note that test XYZ is equivalent to [ XYZ ], i.e. the above is also equivalent to if [ XYZ ] ; then command ; fi. Also note that any semicolon ; can be replaced with a newline, they are equivalent.

Remove the test -e "$file" && condition (only leave the echo) and see what happens.

Remove the double-quotes around "$file" and see what happens when the directory whose contents you are listing contains filenames with spaces in them. Add set -x at the top of the script (or invoke it as sh -x scriptname.sh instead) to turn on debug output and see what's happenning in detail (to redirect debug output to a file, run sh -x scriptname.sh 2>debugoutput.txt).

To also list hidden files (e.g. .bashrc):

...
for file in * .?* ; do
  if [ "$file" != ".." ] ; then
    test -e ...
    test -d ...
  fi
done
...

Note the use of != (string comparison) instead of -ne (numeric comparison.)

Another technique would be to spawn subshells instead of using pushd/popd:

my_ls() {
  # everything in between roundbrackets runs in a separatly spawned sub-shell
  (
    # change directory in sub-shell; does not affect parent shell's cwd
    cd "$1"
    for file in ...
      ...
    done
  )
}

Note that on some shell implementations there is a hard limit (~4k) on the number of characters which can be passed as an argument to for (or to any builtin, or external command for that matter.) Since the shell expands, inline, * to a list of all matching filenames before actually performing for on it, you can run into trouble if * is expanded inside a directory with a lot of files (same trouble you'll run into when running, say ls * in the same directory, e.g. get an error like Command too long.)

Cheers, V.

vladr
you can use `shopt -s dotglob` to list hidden files.
ghostdog74
@ghostdog -- not portable. `.*` (or `.?*` to skip `.` off the bat) does the trick just fine and is portable too.
vladr
A: 
ephemient
but then, if that's the case, using Python/Ruby/PHP etc any language that can list files will be technically not using `ls` or `find`
ghostdog74
@ghostdog74 Obviously. But since all the serious answers have gotten less than amazing feedback, this is something... less serious. (I was actually going to write up a clone of `find` in C and a shell script which compiles+runs it, but decided it was too much effort for a joke.)
ephemient