Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have done a bit of searching online, and I am trying to find a way to recursively list all files with their absolute path and with their permissions. I want to do this so that I can grep out what I want, so that when I run the command, I can get just the matching files, their permissions, and their full paths, like:

<search command> | grep file.name

Output:

/home/current/Desktop/file.name
/etc/program/src/file.name

I would prefer to use ls because it is the fastest, and I would type:

ls -alR $PWD/

But this doesn't show the file's path, so if I grep'ed the output, then I would see file permissions, but not the directory from which it originated.

I can use ls integrated with find and grep to get the output in exactly the format that I want, and I could use something like this:

ls -ault `find $PWD/ -type f` | grep file.name

But this is extremely slow, I'm guessing because two commands are actually running.

If I just use find without ls or grep, then it goes faster, but it is a bunch to type:

find $PWD/ -type f -name file.name -printf '%M %u %g %s\t%a\t%p\r\n'

This will give me a nice format (It also includes the user, group, size, and last date of access, which are helpful). However, it is a ton to type, and it is certainly not as fast as using ls with grep.

Is there a faster way to do what I am trying to do than to use find?

share|improve this question

migrated from stackoverflow.com May 15 '13 at 3:36

This question came from our site for professional and enthusiast programmers.

    
if your $PWD is huge, there is no faster way than your last find command. btw, you may want to have a -a (and) between -type and -name? or maybe single -name is enough for your find? –  Kent May 14 '13 at 15:23

4 Answers 4

up vote 5 down vote accepted

Rather than ls or find you may try tree. Specifically tree -ifpugDs $PWD should give you what you would like.

-if removes indentation lines and prints out path

-p prints permissions

-ug prints user and group

-D prints modification time

-s prints size

share|improve this answer
    
Excellent, this appears to be exactly what I am trying to do. It is just as fast as <code>ls</code>, and I can use <code>grep</code> to just show the lines that I am trying to show. Thanks! –  Michael Plautz May 14 '13 at 15:35
    
Glad to help :) –  Dr.Tower May 14 '13 at 15:36

If typing it is a problem, what about putting what you already have in a function:

myspecialfinder() {
  find $PWD/ -type f -name "$1" -printf '%M %u %g %s\t%a\t%p\r\n'
}

You would use it as

myspecialfinder file.name
share|improve this answer
    
This is good to keep in mind - I can put this in my .bashrc file so that every time I log on, I already have this function available. –  Michael Plautz May 14 '13 at 15:37

Simple answer: find -type f -print0 | xargs -0 ls -al

share|improve this answer
--tree -ifpugDs $PWD-- etc.

Very close. The problem is $PWD, which results in useless garbage if you have actual file names, not Unix-style file names, that is, with no spaces. To get it to work on any file system, EG NTFS, you need to quote the $PWD.

I use the C-shell, as I was a berkeley/Sun user at the Lab, but the same ideas apply in bash.

tree -if "$PWD"

Now if your file name is

/home/he/Documents/00 - Writing/02 The Rapture of the Maiden/0 - Text/25th/Rapture, pt 1-4, ch 01-20 old.txt, 

it doesn't crap out as soon as it hits the space.

I'm a big fan of English language file names, that is, not something like RSFunc97Stat.txt. Maybe because, unlike most programmers, I can type with ten figures at a normal writing speed -- few people need more than 40 words per minute to type as fast as they can compose. Most techies I've worked with use the 3-fingers-on-one-hand-2-on-the-other method, and they like to use short variable name, don't like to comment, and in general cannot be counted on to help with the user doc. Also, it's an old habit from the days when you only got 8 chars to say it.

Nowadays, we need to support all file names, even "O'Reilly's Army.txt". I even have a database test item in all databases I made, called the "O'Reilly" test. Many a program failed utterly when a text field had a quote mark or a leading pace, etc. The good news is that tcsh autocompletes your files, and quotes them according to the correct archanesyntax.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.