Unix/ls: Difference between revisions

11,848 bytes added ,  3 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 5 users not shown)
Line 850:
for non-privilegized use: 13951995904 ( 11% )
All available free space: 20411224064 ( 16% )
 
</pre>
 
It is not visible on the output above, but the digits in the filesize are colored, each 3 digits has different color.
 
=={{header|Peri}}==
The following program lists just the subdirectories, regular files and symlinks, because I do no need the other gizmos generally, but based on these abovementioned file types it is easy to expand with them, if it is necessary.
 
The output is indeed sorted, but not all filetypes alltogether: the sorting algorythm is invoked for the different filetypes separately, because it is the way I like it!
 
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude args.uh
###sysinclude io.uh
###sysinclude str.uh
 
###define COLORS
// delete the COLORS directive above if you do not want colored output!
#g argc 3 < { "." }{ 2 argv } sto mypath
@mypath 'd inv istrue { ."The given directory doesn't exist! Exited.\n" end }
@mypath getdir { ."Cannot load the dir! Aborted.\n" end } sto mydir
'- 90 *... sto vonal
'. 60 *... sto points
 
@mydir ~d {
."Directories:\n"
@mydir ~d {{ #s
@mydir 'd {{}} octalrights dup print inv mem SPACE
@mydir 'd {{}} getfilename dup 67 mc print SPACE drop
@mydir 'd {{}} groupname ': !+
@mydir 'd {{}} ownername dup sto temp + dup 10 mc print inv mem @temp inv mem
@mydir 'd {{}} mtime dup print inv mem
NL }}
@points sprintnl
@mydir ~d { ."Total: " @mydir ~d #g print ." subdirectories.\n" }
@vonal sprintnl
}
 
@mydir ~r {
."Regular files:\n"
@mydir ~r {{ #s
@mydir 'r {{}} octalrights dup print inv mem SPACE
@mydir 'r {{}} getfilesize sbr §ifcolored
@mydir 'r {{}} executable { ." >" }{ ." " } SPACE
@mydir 'r {{}} getfilename dup 67 mc print SPACE drop
@mydir 'r {{}} groupname ': !+
@mydir 'r {{}} ownername dup sto temp + dup 10 mc print inv mem @temp inv mem
@mydir 'r {{}} mtime dup print inv mem
NL }}
@points sprintnl
@mydir ~r { ."Total: " @mydir ~r #g print ." regular files. "
."TotalSize = " @mydir 'r totalsize sbr §ifcolored NL
}
@vonal sprintnl
}
@mydir ~L {
."Symlinks:\n"
@mydir ~L {{ #s
@mydir 'L {{}} octalrights dup print inv mem SPACE
@mydir 'L {{}} executable { .">" }{ SPACE } SPACE
@mydir 'L {{}} getfilename dup 67 mc print SPACE drop
@mydir 'L {{}} broken { ."--->" }{ ."===>" } SPACE
@mydir 'L {{}} destination dup 30 mc print drop
NL }}
@points sprintnl
@mydir ~L { ."Total: " @mydir ~L #g print ." symlinks.\n"
}
}
@vonal sprintnl
."Size, alltogether = " @mydir alltotal sbr §ifcolored NL
@vonal sprintnl
 
 
@mydir inv mem
 
."free spaces: /* Total size of the filesystem is : " @mypath filesystemsize dup sto filsize sbr §ifcolored ." */\n"
." for non-privilegized use: " @mypath freenonpriv dup sbr §ifcolored
#g 100 * @filsize / ." ( " print ."% ) " NL
." All available free space: " @mypath totalfree dup sbr §ifcolored
#g 100 * @filsize / ." ( " print ."% ) " NL
@vonal inv mem
end
 
ifcolored:
###ifdef COLORS
coloredsize
###endif
###ifndef COLORS
#g !(#s) 21 >|
###endif
dup sprint inv mem
rts
 
{ „filsize” }
{ „mydir” }
{ „mypath” }
{ „temp” }
{ „vonal” }
{ „points” }
 
 
</syntaxhighlight>
 
{{out}}
<pre>
Directories:
0775 arrays vz:vz 2020.11.15 22:44:49 V
0775 examples vz:vz 2021.02.05 22:58:48 P
0775 headers vz:vz 2021.01.19 20:34:07 K
0775 inaneheaders vz:vz 2021.02.21 18:59:46 V
0775 keyboardlayout_files vz:vz 2021.09.10 21:51:11 P
0775 libraries vz:vz 2021.09.10 22:20:04 P
0775 Peri_newlinkerhez root:root 2022.06.19 18:53:37 V
0775 patterns vz:vz 2021.02.21 17:24:35 V
0775 perifonts vz:vz 2020.05.09 10:46:19 Szo
0775 tests vz:vz 2020.11.08 14:44:31 V
0775 useful vz:vz 2022.01.27 12:16:31 Cs
............................................................
Total: 11 subdirectories.
------------------------------------------------------------------------------------------
Regular files:
0664 3379 __peri.c vz:vz 2020.11.08 14:35:28 V
0664 938037 A_Peri_programozasi_nyelv.odt vz:vz 2022.02.16 16:10:56 Sze
0664 9185 castingoperators.c vz:vz 2020.12.30 20:19:48 Sze
0664 27148 inanerun.c vz:vz 2021.01.26 12:52:53 K
0664 14998 jumpingtable.c vz:vz 2021.01.26 12:52:19 K
0664 15862 keywords.c vz:vz 2021.01.26 12:46:02 K
0664 3110 libraries.sh vz:vz 2020.12.04 02:46:13 P
0664 1114 Makefile vz:vz 2020.11.08 14:12:28 V
0664 257 mybmi.upu vz:vz 2022.01.27 12:15:03 Cs
0775 10279 > peri vz:vz 2021.09.10 22:20:04 P
0664 34225 postfixoperators.c vz:vz 2021.01.01 00:16:45 P
0644 563 preinitpostmortem.c vz:vz 2020.10.25 15:48:55 V
0664 119 printitself.upu vz:vz 2023.03.21 22:23:24 K
0664 929840 The_Peri_programming_language.odt vz:vz 2021.09.10 22:23:22 P
0644 8357 UPU.syntax vz:vz 2021.01.06 18:00:58 Sze
0664 2199 verify.upu vz:vz 2020.11.29 23:07:42 V
............................................................
Total: 16 regular files. TotalSize = 1998672
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
Size, alltogether = 2043728
------------------------------------------------------------------------------------------
free spaces: /* Total size of the filesystem is : 126693117952 */
for non-privilegized use: 35734585344 ( 28% )
All available free space: 42193813504 ( 33% )
 
</pre>
Line 917 ⟶ 1,063:
 
=={{header|Java}}==
This is a generic implementation using the basic ''File'' methods.
<syntaxhighlight lang="java">
import java.io.File;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
 
public class Ls {
public static void main(String[] args) throws Exception {
Ls ls = new Ls("/");
System.out.println(ls);
}
 
private final File directory;
private final List<File> list;
 
public Ls(String path) throws Exception {
directory = new File(path);
if (!directory.exists())
throw new Exception("Path not found '%s'".formatted(directory));
if (!directory.isDirectory())
throw new Exception("Not a directory '%s'".formatted(directory));
list = new ArrayList<>(List.of(directory.listFiles()));
/* place the directories first */
list.sort((fileA, fileB) -> {
if (fileA.isDirectory() && fileB.isFile()) {
return -1;
} else if (fileA.isFile() && fileB.isDirectory()) {
return 1;
}
return 0;
});
}
 
private String size(long bytes) {
if (bytes > 1E9) {
return "%.1fG".formatted(bytes / 1E9d);
} else if (bytes > 1E6) {
return "%.1fM".formatted(bytes / 1E6d);
} else if (bytes > 1E3) {
return "%.1fK".formatted(bytes / 1E3d);
} else {
return "%d".formatted(bytes);
}
}
 
@Override
public String toString() {
StringBuilder string = new StringBuilder();
Formatter formatter = new Formatter(string);
/* add parent and current directory listings */
list.add(0, directory.getParentFile());
list.add(0, directory);
/* generate total used space value */
long total = 0;
for (File file : list) {
if (file == null) continue;
total += file.length();
}
formatter.format("total %s%n", size(total));
/* generate output for each entry */
int index = 0;
for (File file : list) {
if (file == null) continue;
/* generate permission columns */
formatter.format(file.isDirectory() ? "d" : "-");
formatter.format(file.canRead() ? "r" : "-");
formatter.format(file.canWrite() ? "w" : "-");
formatter.format(file.canExecute() ? "x" : "-");
/* include size */
formatter.format("%7s ", size(file.length()));
/* modification timestamp */
formatter.format("%tb %1$td %1$tR ", file.lastModified());
/* file or directory name */
switch (index) {
case 0 -> formatter.format(".");
case 1 -> formatter.format("..");
default -> formatter.format("%s", file.getName());
}
if (file.isDirectory())
formatter.format(File.separator);
formatter.format("%n");
index++;
}
formatter.flush();
return string.toString();
}
}
</syntaxhighlight>
<pre>
total 22.3K
dr-x 154 Feb 13 02:48 ./
dr-x 10.9K Apr 14 20:17 ../
dr-x 0 Dec 09 14:15 boot/
dr-x 660 May 12 20:48 dev/
dr-x 2.3K May 12 20:48 etc/
dr-x 12 Apr 14 20:12 home/
dr-x 614 Apr 15 15:00 lib/
dr-x 1.1K Apr 15 15:00 lib32/
dr-x 46 Feb 13 02:48 lib64/
dr-x 1.1K Apr 15 15:00 libx32/
dr-x 0 Feb 13 02:47 media/
dr-x 32 Apr 14 20:12 mnt/
dr-x 40 Apr 14 20:18 opt/
dr-x 0 May 12 20:48 proc/
d--- 30 Feb 13 02:47 root/
dr-x 400 May 12 20:48 run/
dr-x 4.2K Feb 13 02:48 sbin/
dr-x 0 Feb 13 02:47 srv/
dr-x 0 May 12 20:48 sys/
drwx 566 May 13 00:41 tmp/
dr-x 116 Feb 13 02:47 usr/
dr-x 90 Feb 13 02:47 var/
</pre>
<br />
An alternate demonstration
{{Works with|Java|11}}
 
Line 1,336 ⟶ 1,598:
4
</pre>
 
=={{header|RPL}}==
<code>VARS</code> returns all the files and folders in the current directory, but not sorted.
A few additional words are then needed to turn names into strings, sort the strings and come back to names.
≪ VARS LIST→ → len
≪ 1 len '''FOR''' n →STR len ROLL '''NEXT'''
len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' DUP2 > '''THEN''' SWAP '''END''' n ROLLD '''NEXT'''
n ROLLD -1 STEP
1 len '''FOR''' n STR→ len ROLL '''NEXT'''
len →LIST
≫ ≫ ‘'''LS'''’ STO
 
=={{header|Ruby}}==
Line 1,565 ⟶ 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
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
An other possibility is to use GNU coreutils' `stat`:
Desktop
 
Documents
<syntaxhighlight lang="bash">stat -c %n * |sort</syntaxhighlight>
Downloads
 
KeyGenerator.png
Library
Movies
Music
My Projects
Pictures
Public
Aamrun $
</syntaxhighlight>
=={{header|Ursa}}==
<syntaxhighlight lang="ursa">decl file f
Line 1,602 ⟶ 1,869:
end for</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import os
 
fn main() {
Line 1,611 ⟶ 1,878:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Directory
 
var path = "./" // or whatever
9,485

edits