PreviousUpNext

5.5.56  Process contents of directories and directory trees

Alphabetically list all vanilla names (names not starting with a dot) in current directory:

    eval:  foreach (dir::entry_names ".") {. printf "%s\n" #filename; };
    bar
    foo
    zot

Alphabetically list all names in current directory, except for “.” and “..”:

    eval:  foreach (dir::entry_names' ".") {. printf "%s\n" #filename; };
    .bashrc
    .emacs
    bar
    foo
    src
    zot

Alphabetically list all filenames in current directory, including “.” and “..”:

    eval:  foreach (dir::entry_names'' ".") {. printf "%s\n" #filename; };
    .
    ..
    .bashrc
    .emacs
    bar
    foo
    src
    zot

Alphabetically list names of all vanilla files in current directory, ignoring directories, pipes etc:

    eval:  foreach (dir::file_names ".") {. printf "%s\n" #filename; };
    .bashrc
    .emacs
    bar
    foo
    zot

Count number of entries in current directory:

    eval:  length (dir::entry_names'' ".");
    7

Alphabetically list paths of all vanilla names in current directory:

    eval:  foreach (dir::entries ".") {. printf "%s\n" #filename; };
    /home/jcb/bar
    /home/jcb/foo
    /home/jcb/src
    /home/jcb/zot

Alphabetically list paths of all names in current directory except for “.” and “..”:

    eval:  foreach (dir::entries' ".") {. printf "%s\n" #filename; };
    /home/jcb/.bashrc
    /home/jcb/.emacs
    /home/jcb/bar
    /home/jcb/foo
    /home/jcb/src
    /home/jcb/zot

Alphabetically list paths of all names in current directory, including “.” and “..”:

    eval:  foreach (dir::entries'' ".") {. printf "%s\n" #filename; };
    /home/jcb/.
    /home/jcb/..
    /home/jcb/.bashrc
    /home/jcb/.emacs
    /home/jcb/bar
    /home/jcb/foo
    /home/jcb/src
    /home/jcb/zot

Alphabetically list paths of all vanilla files in current directory, ignoring directories, pipes etc:

    eval:  foreach (dir::files ".") {. printf "%s\n" #filename; };
    /home/jcb/.bashrc
    /home/jcb/.emacs
    /home/jcb/bar
    /home/jcb/foo
    /home/jcb/zot

Print the sizes and names of all vanilla files in current directory:

    eval:  foreach (dir::file_names ".") {. printf "%8d %s\n" (stat #filename).size #filename; };
         328 .bashrc
      240553 .emacs
       29559 bar
       24963 foo
      124518 zot

Build a list of (filename, filesize) pairs for current directory:

    eval:  pair_list = map {. (#name, (stat #name).size); } (dir::file_names ".");
    [(".bashrc", 328), 
     ("bar", 29559), ("foot", 24963), 
     ("zot", 126542)]

Build a list of (filename, filesize) pairs for current directory II:

    eval:  [ (filename, (stat filename).size) for filename in dir::file_names "." ];
    [(".bashrc", 328), 
     ("bar", 29559), ("foot", 24963), 
     ("zot", 126542)]

Sort the above list by size:

    eval:  sorted_list = sort {. #2 #a < #2 #b; } pair_list;
    [(".emacs", 240547), ("zot", 126542),
     ("bar", 29559), (".bashrc", 328)]

Print above size-sorted list:

    eval:  apply  {. printf "%8d %s\n" (#2 #a) (#1 #a); }  sorted_list;
      240547 .emacs
      126542 zot
       29559 bar
         328 .bashrc

Print names of the immediate subdirectories of the current directory:

    eval:  foreach (dir::entry_names ".") {. if (-D #name) printf "%s\n" #name; fi; };
    src

Print names of all files matching a given regular expression:

    eval:  foreach (dir::file_names ".") {. if (#name =~ ./o/) printf "%s\n" #name; fi; };
    foo
    zot

Build a map from file names to file sizes, then print it out:

    #!/usr/bin/mythryl

    include package   string_map;

    # Build a string-map (balanced binary tree)
    # where the keys are the file names and the
    # values are the file sizes:
    #
    name_to_size
        =
        for (result = empty,  input = dir::file_names ".";
             length input > 0; input = tail input;
             result
            )
            {  name    = head input;
               result $= (name, (stat name).size);
            };

    # Iterate over all keys in the map, fetching
    # corresponding sizes and printing the two out:
    #
    foreach (keys_list name_to_size) {.
        size = the (get (name_to_size, #name));
        printf "%8d %s\n" size #name;
    };

    exit 0;

List all plain files in or below current directory:

    eval:  foreach (dir_tree::files ".") {. printf "%s\n" #file; };
    /home/jcb/.bashrc
    /home/jcb/.emacs
    /home/jcb/bar
    /home/jcb/foo
    /home/jcb/src/test.c
    /home/jcb/zot

List sizes of all plain files in or below current directory:

    eval:  foreach (dir_tree::files ".") {. printf "%8d %s\n" (stat #file).size #file; };
         328 /home/jcb/.bashrc
      240553 /home/jcb/.emacs
       29559 /home/jcb/bar
       24963 /home/jcb/foo
        1978 /home/jcb/src/test.c
      124518 /home/jcb/zot

Count number of plain files in or below current directory:

    eval:  length (dir_tree::files ".");
    6

Print all .c files in or below current directory:

    eval:  foreach (dir_tree::files ".") {. if (#file =~ ./\\.c$/) printf "%s\n" #file; fi; };
    /home/jcb/src/test.c

Print all .c files in or below current directory, another way:

    eval:  foreach (filter {. #file =~ ./\\.c$/; } (dir_tree::files ".")) {. printf "%s\n" #file; };
    /home/jcb/src/test.c

Count number of .c files in or below current directory:

    eval:  length (filter {. #file =~ ./\\.c$/; } (dir_tree::files "."));
    1

Two equivalent ways to count the number of directories in or below a directory:

    eval:  length (filter {. isdir #file; } (dir_tree::entries' "."));
    1
    eval:  length (filter {. -D #file; } (dir_tree::entries' "."));

To count other things, you can replace isdir above by one of ispipe, issymlink, issocket, ischardev or isblockdev, or equivalently replace -D by one of -P, -L, -S, -C or -B.

To follow symlinks to directories, change dir_tree to link_tree in the above examples. (To avoid symlink loops, link_tree remembers which directories it has already visited, by dev-inode number, and visits each one at most once.)


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext