11

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?

1
  • 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, 2013 at 15:23

5 Answers 5

11

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

2
  • 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, 2013 at 15:35
  • Glad to help :)
    – Dr.Tower
    May 14, 2013 at 15:36
3

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
1
  • 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, 2013 at 15:37
3

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

0
--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.

0

find <path> -ls does the trick. I was looking for the same thing, but didn't have tree installed.

3
  • What is the -ls argument for? When I look at the help output for find, I don't even seen those arguments listed.
    – mmortal03
    Aug 18, 2022 at 14:42
  • See linux.die.net/man/1/find - list current file in ls -dils format on standard output
    – Daniel
    Aug 19, 2022 at 15:16
  • Thanks, it must not be a feature on all versions of 'find' that are out there. But I found the following to be a helpful explanation: askubuntu.com/questions/1169615/…
    – mmortal03
    Aug 20, 2022 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .