Unix/ls: Difference between revisions

Line 1,840:
 
=={{header|UNIX Shell}}==
 
ls sorts by default, if it doesn't work for you, pipe it's output to sort :
Using `ls` explicitly would be a cheat, since the goal of the task is to emulate this program.
<syntaxhighlight lang="bash">
 
Aamrun $ ls -1
A simple way to list files in the current directory without using `ls` is to
Applications
use filename expansion (a.k.a. "globbing").
Desktop
 
Documents
<syntaxhighlight lang="bash">echo *</syntaxhighlight>
Downloads
 
KeyGenerator.png
To print each filename on a separate line, use a loop:
Library
 
Movies
<syntaxhighlight lang="bash">for f in *; do echo "$f"; done</syntaxhighlight>
Music
 
My Projects
Finally, if you want this output sorted, then use `sort`:
Pictures
 
Public
<syntaxhighlight lang="bash">for f in *; do echo "$f"; done |sort</syntaxhighlight>
Aamrun $ ls -1|sort
 
Applications
Desktop
Documents
Downloads
KeyGenerator.png
Library
Movies
Music
My Projects
Pictures
Public
Aamrun $
</syntaxhighlight>
=={{header|Ursa}}==
<syntaxhighlight lang="ursa">decl file f
1,934

edits