Unix/ls: Difference between revisions
GP |
No edit summary |
||
Line 1: | Line 1: | ||
{{ |
{{task}} |
||
Write a program that will list everything in the current folder, similar to the Unix utility “<tt>ls</tt>” [http://man7.org/linux/man-pages/man1/ls.1.html] (or the Windows terminal command “<tt>DIR</tt>”). The output must be sorted, but printing extended details and producing multi-column output is not required. |
Write a program that will list everything in the current folder, similar to the Unix utility “<tt>ls</tt>” [http://man7.org/linux/man-pages/man1/ls.1.html] (or the Windows terminal command “<tt>DIR</tt>”). The output must be sorted, but printing extended details and producing multi-column output is not required. |
||
Line 108: | Line 108: | ||
'DIR /b /oN' /*use Windows DIR: sorts & lists.*/ |
'DIR /b /oN' /*use Windows DIR: sorts & lists.*/ |
||
/*stick a fork in it, we're done.*/</lang> |
/*stick a fork in it, we're done.*/</lang> |
||
=={{header|Ruby}}== |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
Revision as of 11:15, 1 September 2014
You are encouraged to solve this task according to the task description, using any language you may know.
Write a program that will list everything in the current folder, similar to the Unix utility “ls” [1] (or the Windows terminal command “DIR”). The output must be sorted, but printing extended details and producing multi-column output is not required.
- Example output
For the list of paths:
/foo/bar /foo/bar/1 /foo/bar/2 /foo/bar/a /foo/bar/b
When the program is executed in `/foo`, it should print:
bar
and when the program is executed in `/foo/bar`, it should print:
1 2 a b
D
<lang d>void main() {
import std.stdio, std.file, std.path;
foreach (const string path; dirEntries(getcwd, SpanMode.shallow)) path.baseName.writeln;
}</lang>
Go
<lang Go>package main
import ( "fmt" "io/ioutil" "sort" )
func main() { info, err := ioutil.ReadDir(".") if err != nil { panic(err) }
files := []string{} for i := 0; i < len(info); i++ { files = append(files, info[i].Name()) } sort.Strings(files)
for i := 0; i < len(files); i++ { fmt.Printf("%s\n", files[i]) } }</lang>
J
<lang J> >{."#.1!:0'*'</lang>
Almost half of that is removing extra detail not relevant to this task.
OCaml
<lang ocaml>let () =
Array.iter print_endline ( Sys.readdir Sys.argv.(1) )</lang>
- Output:
$ cd /foo/bar $ ocaml ls.ml 1 2 a b
PARI/GP
GP doesn't have this capability so we can either use the shell or PARI. For the latter see C; for the former: <lang parigp>system("dir/b/on")</lang> in DOS/Windows or <lang parigp>system("ls")</lang> in *nix.
Perl 6
There is a dir builtin command which returns a list of IO::Path objects. We stringify them all with a hyperoperator before sorting the strings.
<lang perl6>.say for sort ~«dir</lang>
Python
<lang python>>>> import os >>> print('\n'.join(sorted(os.listdir('.')))) DLLs Doc LICENSE.txt Lib NEWS.txt README.txt Scripts Tools include libs python.exe pythonw.exe tcl >>> </lang>
REXX
The following program works under Windows and used the Windows DIR command to list a bare-bones sorted list. <lang rexx>/*REXX program lists contents of current folder (ala mode UNIX's LS). */ 'DIR /b /oN' /*use Windows DIR: sorts & lists.*/
/*stick a fork in it, we're done.*/</lang>
Ruby
Rust
<lang rust>use std::os; use std::io::fs;
fn main() { let cwd = os::getcwd(); let info = fs::readdir(&cwd).unwrap();
let mut filenames = Vec::new(); for entry in info.iter() { filenames.push(entry.filename_str().unwrap()); }
filenames.sort(); for filename in filenames.iter() { println!("{}", filename); } }</lang>
Tcl
<lang tcl>puts [join [lsort [glob -nocomplain *]] "\n"]</lang>
zkl
<lang zkl>File.glob("*").println()</lang> Lists all files and directories in the current directory. If you only want a list of files: <lang zkl> File.glob("*",0x8)</lang> Using Unix shell wild cards.
The globular method recurses down through the directories.