4

How can I tweak the output of ls in Linux so that it looks like the output of the command dir /b /s in Windows?

dir /b /s output as follows:

C:\MinGW>dir /s /b 
C:\MinGW\COPYING 
C:\MinGW\COPYING.LIB
C:\MinGW\doc 
C:\MinGW\include
0

5 Answers 5

7
find . -print

This should produce the same output as dir /s/b

4
  • Not quite. The paths are displayed relative to ., not /. find BACKTICKpwdBACKTICK -print would work. But that's a bit more complicated than just using ls and I don't think exact conformance to dir's output format is a strict requirement.
    – sepp2k
    Jul 4, 2010 at 20:33
  • @sepp2k: Given your last sentence, I'm confused why you point out the first. Find is a generally much more useful tool than ls for cases similar to this.
    – Roger Pate
    Jul 4, 2010 at 23:29
  • @jrtokarz: You don't need the dot or -print, they're the default, so just "find".
    – Roger Pate
    Jul 4, 2010 at 23:30
  • @Roger: I believe the search path is required in most find versions (except GNU).
    – user1686
    Jul 5, 2010 at 10:19
2

ls -R lists all files and subdirectories recursively. With ls -R1 you get the same, but only one file per line.

Neither makes the output look exactly like that of dir /b /s, but it should be close enough.

2
ls -1

or

ls -1d "$PWD"/*
2
  • /S is the dir equivalent of -R even if the OP's example output does not make that clear.
    – sepp2k
    Jul 4, 2010 at 20:27
  • ls -1 does not work recursively (as required). ls -1R would work recursively, but its output will look quite different to dir /s /b (which shows the absolute paths for each file). ls -1d "${PWD}/* also misses two things: the recursion and matching normal files (your -d limits output to directories only). – Jul 5, 2010 at 13:22
1

It depends, how hard the requirement is to get "equivalence" to dir /b /s... These ones come pretty close (they'll have no backslashes as directory separator though, and the output order will be a bit different [dir /b /s outputs 'sibling' directories first, before diving into each one to show their content]). The second one is just a fallback in case your version find doesn't default to -print action (most do though):

find $(pwd)
find $(pwd) -print

UPDATE: I had a typo in above commands originally. I typed curly brackets "{}" instead of round ones "()" how they ought to be. Thanks to grawity for spotting this.

4
  • 1
    It's $PWD, uppercase.
    – user1686
    Jul 5, 2010 at 10:20
  • @grawity: you're right and wrong at the same time. $PWD or ${PWD} would return the content of an environment variable (the current directory). This is what you have in mind. pwd is a little commandline utility ('print working directory') which also outputs the current directory path. Using it as ${pwd} or as `pwd` would return the result of that command, which in this case has the same effect as using $PWD. So my version will definitely work, and your comment was not appropriate in this context. Jul 5, 2010 at 10:33
  • 2
    Correction: $(pwd) returns output of pwd, and ${pwd} returns the value of $pwd. Notice the difference in brackets.
    – user1686
    Jul 5, 2010 at 10:43
  • @grawity: D'oh! You're right of course. (I do it right whenever I need to do it in a shell. Heaven knows why I did it wrong when typing it into the text edit field....) I'll correct my comments so it will not confuse other readers. Jul 5, 2010 at 13:13
1

this seems to work, from the command line:

find|awk "/^\.\//{print\"$PWD\"substr(\$0,2)}"
2
  • Why would you not just use -printf instead? Oct 30, 2010 at 10:13
  • Because I'm new and do things the hard way. I've added this line to my .bashrc which seems to do the trick: alias lsb='find . -printf "$PWD/%P\n"' Is this what you mean? Thanks for the tip!
    – erik
    Oct 30, 2010 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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