WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Script to list contents of a directory (subdirectories first)  (Read 4649 times)

Offline deetee

  • Newbie
  • *
  • Posts: 34
Hi!

I try to list all files of a directory (including hidden files) but subdirectories first.

Code: (bash) [Select]
ls -lA | grep "^d" && ls -lA | grep "^-"
... works fine, but it doesn't list correctly where a directory contains links and no subdirectories.

For example in the TC-directory '/bin' - which contains no subdirectory, many links and one file (busybox) - no file is listed.

I don't really have a clue why.
Is there another solution which shows links and files in /bin and lists subdirectories first (if they exist)?

TIA
deetee

Offline caminati

  • Full Member
  • ***
  • Posts: 184
    • Homepage
Re: Script to list contents of a directory (subdirectories first)
« Reply #1 on: May 07, 2013, 07:16:11 AM »
Hi!

I try to list all files of a directory (including hidden files) but subdirectories first.

Code: (bash) [Select]
ls -lA | grep "^d" && ls -lA | grep "^-"
... works fine, but it doesn't list correctly where a directory contains links and no subdirectories.

Yes, you're instructing grep to only pick files (^-).
Try grep "^[^d]" instead.

You can also use "find -maxdepth 1 -mindepth 1 {-type d | -not -type d}" in lieu of ls+grep.

Or do something like
Code: [Select]
ls -p| sed "s_^.*/_!&_"| sort | sed "s_^!__"
to avoid parsing the filesystem twice.

Last solution is just a stub, and should be made more robust (e.g., using awk) for filenames starting with spaces, etc...
« Last Edit: May 07, 2013, 08:57:35 AM by caminati »

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: Script to list contents of a directory (subdirectories first)
« Reply #2 on: May 07, 2013, 08:29:43 AM »
Hi caminati!

Thanks for your fast reply.
Actually my first command is piped to awk for further processing.

I tried your hint with "^[^d]" - but unfortunately
Code: (bash) [Select]
ls -lA | grep "^d" && ls -lA | grep "^[^d]"doesn't list correctly in TC's /bin (empty list).
It is strange that the second ls (with "^-" or "^[^d]") doesn't list anything if the first ls (with "^d]") provides an empty list.
The second ls "alone" works (lists file busybox in /bin).

As I'm a beginner in scripting I suppose that "^[^d]" means "every line which doesn't start with a d" - the man page of grep is a little bit unclear how to formulate this.

Also i tried to check the appended / in your "ls -p solution", but i don't know how to check the last character (opposite of ^) with grep. The grep man page tells something about $, but every try failed.

I confess, that I'm a novice in sed too - but I will play with your sed-solution.

deetee


Offline caminati

  • Full Member
  • ***
  • Posts: 184
    • Homepage
Re: Script to list contents of a directory (subdirectories first)
« Reply #3 on: May 07, 2013, 08:54:08 AM »
Hi caminati!

Thanks for your fast reply.
Actually my first command is piped to awk for further processing.

I tried your hint with "^[^d]" - but unfortunately
Code: (bash) [Select]
ls -lA | grep "^d" && ls -lA | grep "^[^d]"doesn't list correctly in TC's /bin (empty list).

Change && into ; to solve that.

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: Script to list contents of a directory (subdirectories first)
« Reply #4 on: May 07, 2013, 09:30:40 AM »
Hi caminati!

I didn't see the wood for the trees.
The '&&' made the problems.
Unexpected from my side - the ';' works fine - even if I put both ls's between brackets '()' and pipe to awk, head and tail.

You are great - thanks for your help.
deetee

Offline caminati

  • Full Member
  • ***
  • Posts: 184
    • Homepage
Re: Script to list contents of a directory (subdirectories first)
« Reply #5 on: May 07, 2013, 09:38:49 AM »

Unexpected from my side - the ';' works fine - even if I put both ls's between brackets '()' and pipe to awk, head and tail.

Yes, you can () almost any subset of statements.
However, if you don't need a subprocess spawned, I suggest using { ... ; } instead of ( ... ).

Quote
You are great - thanks for your help.
deetee

You're welcome.

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: Script to list contents of a directory (subdirectories first)
« Reply #6 on: May 08, 2013, 08:09:00 AM »
Sorry to be annoying, but there is still one question open:

How can I grep to show files starting not with d?

Code: (ash) [Select]
ls -lA | grep "^d" ; ls -lA | grep "^[^d]"... didn't work.

Code: (ash) [Select]
ls -lA | grep "^d" ; ls -lA | grep -v "^d"... is close to the solution, but unfortunately between the two ls's a "total" is displayed.

Maybe there is a solution which is close to
Code: (ash) [Select]
ls -lA | grep "^d" ; ls -lA | grep "$d"or
Code: (ash) [Select]
ls -lA | grep "^d" ; ls -lA | grep "!^d"without a "total-line" between?

My recent script
Code: (ash) [Select]
ls -lA | grep "^d" ; ls -lA | grep "^-"doesn't show a "total" (I don't know why) but fails if there are other files starting with "d" or "-" (i.e. links like in /dev).

TIA
deetee

Offline deetee

  • Newbie
  • *
  • Posts: 34
Re: Script to list contents of a directory (subdirectories first)
« Reply #7 on: May 08, 2013, 09:08:00 AM »
In the meantime I found on

http://superuser.com/questions/109537/unix-ls-how-to-sort-first-directories-then-files-etc

Mark's solution, which seems to "filter" the total-line.

Code: (ash) [Select]
ls -Al | grep "^d" && ls -Al | grep "^-" && ls -Al | grep -E "^d|^-" -v | grep -v "^total"
I replaced the && with "caminati's" ";" and it seems to work so far (didn't really analize/understand the code completely).

deetee