• Stars
    star
    139
  • Rank 261,273 (Top 6 %)
  • Language
    C
  • License
    Other
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

list files, recursively

lr: list files, recursively

lr is a new tool for generating file listings, which includes the best features of ls(1), find(1), stat(1) and du(1).

lr has been tested on Linux 4.1, FreeBSD 10.2, OpenBSD 5.7, NetBSD 5.2.3, DragonFlyBSD 5.0, Mac OS X 10.10, OmniOS 5.11 and Cygwin 1.7.32. It will likely work on other Unix-like systems with C99, but you'll need to port scan_filesystems for fstype to work.

Screenshot

Screenshot of lr -AFGl -ovh

Benefits

Over find:

  • friendly and logical C-style filter syntax
  • getopt is used, can mix filters and arguments in any order
  • can sort
  • compute directory sizes
  • can strip leading ./
  • can do breadth first search

Over ls:

  • sorts over all files, not per directory
  • copy & paste file names from the output since they are relative to pwd
  • ISO dates
  • powerful filters

Rosetta stone

  • ls: lr -1 | column
  • find .: lr (or lr -U for speed.)
  • ls -l: lr -1l
  • ls -ltrc: lr -l1Aoc
  • find . -name '*.c': lr -t 'name ~~ "*.c"'
  • find . -regex '.*c': lr -t 'path =~ "c$"'
  • find -L /proc/*/fd -maxdepth 1 -type f -links 0 -printf '%b %p\n': lr -UL1 -t 'type == f && links == 0' -f '%b %p\n' /proc/*/fd
  • find "${@:-.}" -name HEAD -execdir sh -c 'git rev-parse --resolve-git-dir . >/dev/null 2>/dev/null && pwd' ';': lr -0U -t 'name == "HEAD"' "$@" | xe -0 -s 'cd ${1%/*} && git rev-parse --resolve-git-dir . >/dev/null && pwd; true' 2>/dev/null
  • Filter list of files for existence: xe lr -dQU <list
  • replacement for who(1): lr -om -t 'name =~ "[0-9][0-9]*$" && uid != 0' -f '%u\t%p\t%CY-%Cm-%Cd %CH:%CM\n' /dev/pts /dev/tty*
  • Find files with setuid or setgid: lr -t 'mode | 06000' /usr/bin or lr -t 'mode = "u+s" || mode = "g+s"' /usr/bin
  • Find files with non-umask permissions: lr -t '!(mode = "=rw,+X")' -l
  • Find broken symlinks: lr -L -t 'type = l'

Usage:

lr [-0|-F|-l [-TA|-TC|-TM]|-S|-f FMT] [-B|-D] [-H|-L] [-1AGPQXdhsx] [-U|-W|-o ORD] [-e REGEX]* [-t TEST]* PATH...

The special path argument - makes lr read file names from standard input, instead of traversing path.

  • -0: output filenames separated by NUL bytes. Likewise, read input filenames separated by NUL bytes.
  • -F: output filenames and an indicator of their file type (*/=>@|).
  • -l: long output a la ls -l (implies -Q).
  • -TA: with -l, output atime.
  • -TC: with -l, output ctime.
  • -TM: with -l, output mtime (default).
  • -S: BSD stat(1)-inspired output (implies -Q).
  • -f FMT: custom formatting, see below.
  • -B: breadth first traversal.
  • -D: depth first traversal. prune does not work, but entries and total are computed on the fly.
  • -H: only follow symlinks on command line.
  • -L: follow all symlinks.
  • -1: don't go below one level of directories.
  • -A: don't list files starting with a dot.
  • -G: colorize output to tty. Use twice to force colorize.
  • -X: print OSC 8 hyperlinks to tty. Use twice to force.
  • -P: quote file names using $'...' syntax.
  • -Q: shell quote file names (default for output to TTY).
  • -d: don't enter directories.
  • -h: print human readable size for -l (also %s).
  • -s: strip directory prefix passed on command line.
  • -x: don't enter other filesystems.
  • -U: don't sort results, print during traversal.
  • -W: sort results by name and print during traversal.
  • -o ORD: sort according to the string ORD, see below.
  • -e REGEX: only show files where basename matches REGEX.
  • -t TEST: only show files matching all TESTs, see below.

Output formatting:

  • \a, \b, \f, \n, \r, \v, \0 as in C.
  • %%: plain %.
  • %s: file size in bytes.
  • %S: file size, with human readable unit.
  • %b: file size in 512-byte blocks.
  • %k: file size in 1024-byte blocks.
  • %d: path depth.
  • %D: device number (stat.st_dev).
  • %R: device ID for special files (stat.st_rdev).
  • %i: inode number.
  • %I: one space character for every depth level.
  • %p: full path (%P if -s).
  • %P: full path without command line argument prefix.
  • %l: symlink target.
  • %n: number of hardlinks.
  • %F: file indicator type symbol (*/=>@|).
  • %f: file basename (everything after last /).
  • %A-, %C-, %T-: relative age for atime/ctime/mtime.
  • %Ax, %Cx, %Tx: result of strftime for %x on atime/ctime/mtime.
  • %m: octal file permissions.
  • %M: ls-style symbolic file permissions.
  • %y: ls-style symbolic file type (bcdfls).
  • %g: group name.
  • %G: numeric gid.
  • %u: user name.
  • %U: numeric uid.
  • %e: number of entries in directories.
  • %t: total size used by accepted files in directories (only with -D).
  • %Y: type of the filesystem the file resides on.
  • %x: Linux-only: a combination of: # for files with security capabilities, + for files with an ACL, @ for files with other extended attributes.

Sort order

Sort order is string consisting of the following letters. Uppercase letters reverse sorting. E.g. Sn sorts first by size, smallest last, and then by name (in case sizes are equal).

Default: n.

  • a: atime.
  • c: ctime.
  • d: path depth.
  • e: file extension.
  • f: file basename.
  • i: inode number.
  • m: mtime.
  • n: file name.
  • p: directory name.
  • s: file size.
  • t: file type. This sorts all directories before other files.
  • v: file name as version numbers (sorts "2" before "10").

Filter expressions

lr filters are given by the following EBNF:

<expr>     ::= <expr> || <expr>  -- disjunction
             | <expr> && <expr>  -- conjunction
             | <expr> ? <expr> : <expr>  -- ternary operator
             | ! <expr>          -- negation
             | ( <expr )
             | <timeprop> <numop> <dur>
             | <numprop> <numop> <num>
             | <strprop> <strop> <str>
             | <typetest>
             | <modetest>
             | prune             -- do not traverse into subdirectories
             | print             -- always true value
             | skip              -- always false value
             | color <num>       -- always true value, override 256-color

    <timeprop> ::= atime | ctime | mtime

<numprop>  ::= depth | dev | entries | gid | inode
             | links | mode | rdev | size | total | uid

<numop>    ::= <= | < | >= | > | == | = | !=

    <dur>      ::= "./path"          -- mtime of relative path
                 | "/path"           -- mtime of absolute path
                 | "YYYY-MM-DD HH:MM:SS"
                 | "YYYY-MM-DD"      -- at midnight
                 | "HH:MM:SS"        -- today
                 | "HH:MM"           -- today
                 | "-[0-9]+d"        -- n days ago at midnight
                 | "-[0-9]+h"        -- n hours before now
                 | "-[0-9]+m"        -- n minutes before now
                 | "-[0-9]+s"        -- n seconds before now
                 | [0-9]+            -- absolute epoch time

<num>      ::= [0-9]+ ( c        -- *1
                      | b        -- *512
                      | k        -- *1024
                      | M        -- *1024*1024
                      | G        -- *1024*1024*1024
                      | T )?     -- *1024*1024*1024*1024

<strprop>  ::= fstype | group | name | path | target | user | xattr

<strop>    ::= == | = | !=       -- string (in)equality
             | ===    | !===     -- case insensitive string (in)equality
             | ~~     | !~~      -- glob (fnmatch)
             | ~~~    | !~~~     -- case insensitive glob (fnmatch)
             | =~     | !=~ | !~ -- POSIX Extended Regular Expressions
             | =~~    | !=~~     -- case insensitive POSIX Extended Regular Expressions

<str>      ::= " ([^"] | "")+ "  -- use "" for a single " inside "
             | $[A-Za-z0-9_]     -- environment variable

<typetest> ::= type ( == | = | != ) ( b | c | d | p | f | l )

<modetest> ::= mode ( == | =     -- exact permissions
                    | &          -- check if all bits of <octal> set
                    | |          -- check if any bit of <octal> set
                    ) <octal>
             | mode = "<chmod>"  -- check if symbolic mode is satisfied

<octal> ::= [0-7]+

<chmod> ::= <clause> (, <clause>)+

<clause> ::= [guoa]* [+-=] [rwxXstugo]*  -- see chmod(1)

EWONTFIX

The following features won't be implemented:

  • -exec: use -0 and xargs (or even better xe).
  • columns: use column, git-column (supports colors), Plan 9 mc. (e.g. lr -1AGFs | git column --mode=dense --padding=2)

"Screenshots"

Default output, sorted by name:

% lr
.
.git
.git/HEAD
.git/config
[...]
Makefile
README.md
lr.c

Long output format:

% lr -l
drwxrwxr-x 3 chris users   120 2015-10-27 13:56 ./
drwxrwxr-x 7 chris users   240 2015-10-27 13:56 .git/
-rw-rw-r-- 1 chris users    23 2015-10-27 13:56 .git/HEAD
-rw-rw-r-- 1 chris users   257 2015-10-27 13:56 .git/config
[...]
-rw-rw-r-- 1 chris users   297 2015-10-27 13:56 Makefile
-rw-rw-r-- 1 chris users  5828 2015-10-27 13:56 README.md
-rw-rw-r-- 1 chris users 27589 2015-10-27 13:56 lr.c

Simple test:

% lr -F -t 'type == d'
./
.git/
.git/hooks/
.git/info/
.git/logs/
.git/logs/refs/
.git/logs/refs/heads/
.git/logs/refs/remotes/
.git/logs/refs/remotes/origin/
.git/objects/
.git/objects/info/
.git/objects/pack/
.git/refs/
.git/refs/heads/
.git/refs/remotes/
.git/refs/remotes/origin/
.git/refs/tags/

List regular files by size, largest first:

% lr -f '%S %f\n' -1 -t 'type == f' -oS
  27K lr.c
 5.7K README.md
  297 Makefile

List directory total sizes, indented:

% lr -D -t 'type == d' -f '%I%I%t %p\n'
172 .
  132 .git
    40 .git/hooks
    4 .git/info
    12 .git/logs
      8 .git/logs/refs
        4 .git/logs/refs/heads
        4 .git/logs/refs/remotes
          4 .git/logs/refs/remotes/origin
    48 .git/objects
      0 .git/objects/info
      48 .git/objects/pack
    8 .git/refs
      4 .git/refs/heads
      4 .git/refs/remotes
        4 .git/refs/remotes/origin
      0 .git/refs/tags

List all files, but print them in red if they match "havoc":

% lr -G -t 'name =~ "havoc" && color 160 || print'

Do not enter .git or .hg directories:

% lr -t 'name = ".git" || name = ".hg" ? prune : print' .

Installation

Use make all to build, make install to install relative to PREFIX (/usr/local by default). The DESTDIR convention is respected. You can also just copy the binary into your PATH.

Copyright

Copyright (C) 2015-2023 Leah Neukirchen <purl.org/net/chneukirchen>

Licensed under the terms of the MIT license, see lr.c.

More Repositories

1

nq

Unix command line queue utility
C
2,791
star
2

styleguide

443
star
3

mblaze

Unix utilities to deal with Maildir
C
441
star
4

bacon

a small RSpec clone
Ruby
426
star
5

cwm

portable version of OpenBSD's cwm(1) window manager
C
343
star
6

hrmpf

hrmpf rescue system, built on Void Linux
Shell
294
star
7

snooze

run a command at a particular time
C
191
star
8

xe

simple xargs and apply replacement
C
176
star
9

dosfetch

NeoFetch clone for DOS
Pascal
140
star
10

xtools

a few helpers for working with XBPS
Shell
123
star
11

extrace

trace exec() calls system-wide
C
116
star
12

rum

a gRand Unified Mapper for Rack
Ruby
106
star
13

redo-c

An implementation of the redo build system in portable C with zero dependencies
C
101
star
14

outils

port of some non-standard OpenBSD tools to Linux
C
76
star
15

gitsum

basic darcsum feelalike for Git
Emacs Lisp
67
star
16

ignite

OBSOLETE: use Void Linux
Shell
65
star
17

sabotage

an experimental distribution based on musl libc and busybox
Shell
63
star
18

rdumpfs

a rsync-based dump file system backup tool
Shell
59
star
19

leahutils

description of leahutils
53
star
20

obase

a port of the OpenBSD userland to Linux | unmaintained: use outils
C
51
star
21

colfm

A console, column-oriented file manager
Ruby
46
star
22

trivium

Trivium, my minimalist blogging engine
Ruby
39
star
23

fail

crash in various possible ways
C
38
star
24

rwc

report when files change
C
37
star
25

rps

Ruby Packaging Standard
Ruby
30
star
26

sq

a 7x15 pixel font inspired by Codec and Quadraat Sans Mono
Makefile
30
star
27

knock

Knock is a simplification of the Test Anything Protocol used by Perl and others.
Ruby
29
star
28

virtualrb

Virtualize Ruby installations
Ruby
25
star
29

te

tiny emacs
C
23
star
30

challis

a soft lightweight cloth
Ruby
22
star
31

nb

Nota Bene, a quick note-taking tool for Emacs
Emacs Lisp
22
star
32

tools

various simple tools, not worth a project on their own
Ruby
19
star
33

notyet

a text-based recursive task tracker
Ruby
19
star
34

5x13

a condensed pixel font built on 6x13 ("fixed")
Makefile
18
star
35

wibget

WibGet, a minimalist, but convenient Git web frontend
Ruby
17
star
36

git-merge-pr

apply GitHub pull request from command-line
Shell
17
star
37

libste

C string library based on string ends
C
17
star
38

vuxi

a minimalist static Wiki compiler
Ruby
15
star
39

rdd

random data dumper
C
15
star
40

qed-caltech

C
15
star
41

yam

Yam, a functional language running as JavaScript
JavaScript
14
star
42

reap

run process until all its spawned processes are dead
C
14
star
43

necho

minimal, sensible alternatives to echo(1)
C
14
star
44

rup

a sane and simple Ruby package manager
14
star
45

hyx

terminal hex editor
C
13
star
46

signify

portable version of OpenBSD's signify(1) signature tool
C
13
star
47

coma

a console mail user agent | obsolete: use mblaze
Ruby
13
star
48

lywsd03mmc-exporter

a Prometheus exporter for the LYWSD03MMC BLE thermometer
Go
12
star
49

coset-mirror

(experimental) Mirror of the coset darcs repository
Ruby
12
star
50

tt

a 9term-compatible terminal in Ruby/Tk
Ruby
12
star
51

hittpd

efficient, no-frills HTTP 1.1 server
C
11
star
52

daiquiri

a Rack-based resourceful web framework
11
star
53

wcal

ISO weekly calendar
C
11
star
54

px

search for processes and print top(1)-like status
C
11
star
55

rs

rs(1) — reshape a data array (from OpenBSD) | unmaintained: use outils
C
11
star
56

rack-mirror

OUTDATED mirror of Rack's darcs repository, use github.com/chneukirchen/rack
Ruby
10
star
57

amok

a compact mock library
Ruby
9
star
58

rubyports

Hookin-based RubyPorts system
Ruby
9
star
59

xlossage

display pressed keys in X11 in a readable way
C
8
star
60

sgn

pseudonymous digital signatures
Emacs Lisp
7
star
61

ji

a minimalist forum software
Ruby
7
star
62

vmenu

personal fork of vis-menu/slmenu
C
7
star
63

revisit

a TODO list for the future
Ruby
7
star
64

clitter

a ncurses twitter client
Ruby
7
star
65

schell

a lispy shell scripting language
Scheme
7
star
66

arr

(re)arrange and select fields on each line
Groff
7
star
67

rnl

remove trailing newlines
Shell
7
star
68

htping

periodically send HTTP requests
Go
7
star
69

netpbm-mirror

git-svn mirror of netpbm (updated manually)
C
6
star
70

mlog

merge log files by timestamp
C
6
star
71

listening

check if a TCP server is listening
C
5
star
72

atxec

run command expanding arguments from file or environment
Perl
5
star
73

adventofcode2019

Advent of Code (adventofcode.com) in k and Perl 5
Perl
5
star
74

musl-chris2

my sandbox for playing with musl
C
5
star
75

lrep

literate read-eval-print
Ruby
5
star
76

rexample

Ruby
4
star
77

tap3

check output/error/status of a command against a specification
Perl
4
star
78

btac

print file in reverse order
C
4
star
79

rmeta

a OMeta implementation for Ruby
4
star
80

sson

S-Expression Standard Object Notation
Ruby
4
star
81

adventofcode2015

Advent of Code (adventofcode.com) in K
Ruby
4
star
82

noir

a new programming language
JavaScript
4
star
83

adventofcode2022

Advent of Code 2022 in Racket and Zig.
Racket
3
star
84

dwm-chris2

Personal dwm fork.
C
3
star
85

holes

find runs of zero bytes
C
3
star
86

adventofcode2020

Advent of Code 2020 (adventofcode.com) in J and Clojure
Clojure
3
star
87

parseopt

a collection of command line argument parsers
Shell
3
star
88

rc

C
3
star
89

xdu

display the output of "du" in an X window
C
3
star
90

pds

parallel data substitution
Ruby
3
star
91

gleam-codespace

A codespace to work with Gleam
Dockerfile
2
star
92

actions-archive-all

generate tarballs including submodules from GitHub Actions
Shell
2
star
93

docker-lab-bgp

A small BGP lab in Docker
2
star
94

literate-janet

Makefile
2
star
95

adventofcode2021

Advent of Code 2021 (adventofcode.com) in BQN and Clojure
Clojure
2
star
96

adventofcode2017

adventofcode.com 2017
C++
2
star
97

conference-sponsors

Conference Sponsor tracking
2
star
98

systas

Systas Scheme (archival copy)
C
2
star
99

adventofcode2016

Advent of Code 2016 (adventofcode.com) in K and J
Ruby
2
star
100

metaid

metaid SVN checkout as of 2006-07-06
Ruby
2
star