Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: deetee on May 07, 2013, 05:39:32 AM
-
Hi!
I try to list all files of a directory (including hidden files) but subdirectories first.
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
-
Hi!
I try to list all files of a directory (including hidden files) but subdirectories first.
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
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...
-
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
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
-
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
ls -lA | grep "^d" && ls -lA | grep "^[^d]"
doesn't list correctly in TC's /bin (empty list).
Change && into ; to solve that.
-
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
-
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 ( ... ).
You are great - thanks for your help.
deetee
You're welcome.
-
Sorry to be annoying, but there is still one question open:
How can I grep to show files starting not with d?
ls -lA | grep "^d" ; ls -lA | grep "^[^d]"
... didn't work.
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
ls -lA | grep "^d" ; ls -lA | grep "$d"
or
ls -lA | grep "^d" ; ls -lA | grep "!^d"
without a "total-line" between?
My recent script
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
-
In the meantime I found on
http://superuser.com/questions/109537/unix-ls-how-to-sort-first-directories-then-files-etc (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.
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