views:

9233

answers:

7

This is similar to this question, but I want to include the path relative to the current directory in unix. If can do the following:

ls -LR | grep .txt

But it doesn't include the full paths. For example, I have the follow dir structure:

test1/file.txt
test2/file1.txt
test2/file2.txt

The code above will return:

file.txt
file1.txt
file2.txt

How can I get it to include the paths relative to the current directory using standard nix commands?

+4  A: 

Try find. You can look it up exactly in the man page, but it's sorta like this:

find [start directory] -name [what to find]

so for your example

find . -name "*.txt"

should give you what you want.

Sam Hoice
+10  A: 

Use find:

find . -name \*.txt -print

On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.

Glomek
...and for that matter you can leave out the '.'
Adam Mitz
+2  A: 

You could use find instead:

find . -name '*.txt'
Sherm Pendley
A: 

unfortunately, 'find' puts a tremendous load on the system compared to 'ls'. you could always write a perl script which formats the output of 'ls -R' to show path info with every file name.

tom arnall
I'm guessing this depends on the size of the directory. For small directories I think the load would be minimal.
Darryl Hein
+1  A: 
DIR=your_path
find $DIR | sed 's:""$DIR""::'

'sed' will erase 'your_path' from all 'find' results. And you recieve relative to 'DIR' path.

h-dima
A: 

here is the perl script:

sub format_lines($)
{
    my $refonlines = shift;
    my @lines = @{$refonlines};
    my $tmppath = "-";

    foreach (@lines)
    {
     next if ($_ =~ /^\s+/);
     if ($_ =~ /(^\w+(\/\w*)*):/)
     {
      $tmppath = $1 if defined $1; 
      next;
     }
     print "$tmppath/$_";
    }
}

sub main()
{
        my @lines = ();

    while (<>) 
    {
        push (@lines, $_);
    }
    format_lines(\@lines);
}

main();

usage: ls -LR | perl format_ls-LR.pl

Eric Keller
+2  A: 

Use tree, with -f (full path) and -i (no indentation lines)

tree -if .
tree -if directory/

You can then use grep to filter out the ones you want.

Stephen Irons
+1 just what i was looking for
Tom E