Ls

Lists contents of a backup.

$ hb ls [-c backupdir] [-l] [-v] [-r version] [-1]
        [-a] [-d] [-h] [-i] [-x] [pattern]

Without options, lists the most recent version of all files, not including deleted files.

Options

The -l (el) option shows more details similar to the Unix ls -l command, including the file modification time, backup time, and version number.

The -v option must be used with -l, and shows even more details.

The -r option selects a specific version to display and lists files that are backed up at this version or an earlier version. With -r1, some of the files may be from version 1 and some may be from version zero.

The -1 (one) option shows only the files backed up in the -r version, without showing previously saved files. If -r is omitted, -1 shows files saved in the latest backup.

The -a option displays all versions of matching files, including deleted files. For example, the file /etc/hosts might be backed up in versions 2 and 3. Without -a, only the most recent version is listed. With -a, all versions are listed.

The -d option shows all versions of directory pathnames. These are normally not shown, even with -a. -d automatically enables -a.

The -i option makes pathname searches case-insensitive, so pattern ABC matches filenames abc, aBc, AbC, etc. This is the default on Apple systems since their filesystems are normally case-insensitive.

The -h option shows file sizes in KB, MB, and GB rather than in bytes, making them easier to read. 1234567 is shown as 1.2MB with -h.

The -x option shows all pathnames in the backup, including deleted paths. If a pattern is used with -x, it is a regular expression pattern rather than a glob pattern and only pathnames matching the regular expression are shown. All options except -i are ignored with -x.

Search Patterns

There are two ways to search for pathnames matching a pattern: glob searches, without -x, and regular expression searches with -x. These searches have different patterns, matching criteria, and performance. It’s easy to get the two confused, so the details are important to understand how to find pathnames when the full pathname is not known.

Glob Patterns

Without -x, the search pattern is a glob search pattern. These are similar to the Unix command line, and may contain wildcards or not. Matching defaults to case-sensitive on Linux and case-insensitive on Apple. Use the -i opton on Linux to enable case-insensitive searches.

There are two wildcard characters with glob patterns:

  1. * matches zero or more characters, not crossing slashes

  2. ? matches any character, other than slash

The simplest glob pattern does not have wildcards or slashes, and matches any pathname component. In the pathname /my/backup/path, there are 3 components: my, backup, and path. Glob patterns try to match entire components. So using a simple pattern of backup would match the pathname /my/backup/path. A simple pattern of back would not match this pathname since it does not match any component exactly.

Adjacent components can be matched using slashes. The pattern backup/path matches the pathname /my/backup/path and /your/backup/path/test, but not /backup/to/my/path. Using a complete pathname like /my/backup/path as a pattern matches that pathname.

Glob patterns match whole pathname components, for example:

Pattern

Matches

Doesn’t Match

abcd

/abcd
/home/abcd
/home/abcd/file1

/home/abcd.txt
/abcdefg

test/file7

/test/file7
/etc/test/file7
/home/test/file7/x

/mytest/file7
/test/file70
/tester/file7

If the pattern begins with a slash, ls shows only the matches beginning at the root. If you’re not sure where a pathname is located, omit the leading slash. If the pattern begins with ./ (dot slash), ls adds the current directory to the beginning of the pattern.

Glob Wildcards

For more flexible matching, a wildcard pattern without -x uses Unix "glob" rules:

  • * matches zero or more characters

  • ? matches any single character

Glob searches are much faster than -x regular expression searches and are usually powerful enough to find most files. For example, a glob search for '*jim*' (jim anywhere in the pathname) takes less than 3 seconds in a backup of 10M files, whereas a regular expression search -x jim takes 12x longer.

Keep in mind that glob patterns must match entire pathname components, so to search for "jim" anywhere in a pathname component, leading and trailing star wildcards are required. Without the wildcards, the glob pattern "jim" will only match a file or directory named "jim", exactly.

Glob Examples

Pattern

Matches

Doesn’t Match

abcd*

/abcd
/abcdefg
/home/abcd
/home/abcdefg/xyz

/home/myabcd
/Users/yourabcdefg

*abcd*

/abcd
/abcdefg
/home/abcd
/home/abcdefg/xyz
/home/myabcd
/home/myabcdefg

/home/myabcs

/Users/*/.*

/Users/jim/.profile
/Users/jane/.emacs

/Users/.emacs
/home/jim/.profile
/Users/jim/test/.profile

Always quote patterns with single quotes.

Directories are only listed 1 level deep by default. To list all files under a directory, similar to the Unix find command, add /* to the end of the pattern. Remember to single quote all patterns!

Regular Expressions

The -x option shows all pathnames in the backup, including deleted files still in the backup, without details such as permissions, size, etc. It is intended to help find files when the pathname, or maybe even the filename, is not known.

There are big differences between glob pattern matching and regex pattern matching:

  1. regex patterns have many wildcards; glob patterns only have * and ?

  2. in regex patterns, glob wildcard ? is . and glob wildcard * is .*

  3. regex patterns match anywhere in the pathname. So a pattern 'jim' matches the pathname '/Users/mrjimmy/test' for example, whereas glob patterns require leading and trailing stars to match partial pathname components.

  4. regex pattern wildcards match across slashes, unlike glob patterns. Pattern 'j.k' (dot is the single character wildcard with regular expressions) matches pathname '/Users/raj/kim', whereas the glob pattern '*j?k*' cannot match slashes.

If a pattern follows -x, it is a regular expression pattern. These are much more powerful than glob patterns, and allow searching across multiple pathname components when a file’s position in the filesystem hierarchy may not be known. See below for a list of all regex wildcards. Regular expressions are slower than glob patterns, sometimes much slower. If possible, try a glob pattern first.

Instead of using HashBackup’s built-in regular expression engine, pathnames can be "piped" into an external filter program. For example, to use grep to filter pathnames:

$ hb ls -c backupdir -x | grep -i -v 'abc.*def'
(grep -v shows all pathnames that do not match the pattern)

Pathnames can be sent to a file for futher processing using a standard redirect. This can be more efficient for multiple searches since it avoids redundant database operations.

$ hb ls -c backupdir -x >allpaths.txt
$ grep -i 'abc.*def' allpaths.txt
$ grep -i 'ghi.*jkl' allpaths.txt
$ egrep -i -e 'abc.*def' -e 'ghi.*jkl' allpaths.txt
(searches for 2 patterns in 1 pass)
Always quote patterns with single quotes.

Regular Expression Wildcards

  • . matches 1 character

  • * matches 0 or more of the previous character or group

  • + matches 1 or more of the previous character or group

  • ? matches 0 or 1 of the previous character or group

  • {n} matches the previous character or group n times

  • {m,n} matches the previous character or group m to n times

  • ( starts a group; end with )

  • ^ matches the beginning of the pathname

  • $ matches the end of the pathname

  • | is or, so 'abc|def' matches either abc or def

  • [abc] matches 1 character, either a, b, or c

  • [^ab] matches 1 character except a or b

  • [a-z] matches one lowercase letter

  • [a-zA-Z] matches one lowercase or one uppercase letter

  • [^0-9] matches one non-digit character

  • \ escapes a wildcard character, so \* matches an asterisk

  • Note: wildcards (except \ and leading ^) have no meaning inside square brackets

For more regular expression info, check out this RegexOne online tutorial.

Regex Examples

#1. Find all .pdf files, similar to the glob pattern '*.pdf' without -x

$ hb ls -c hb -x '\.pdf$'
/Users/jim/Documents/document.pdf
/Users/jim/Downloads/2015-Lenovo-Limited-Warranty.pdf
/Users/jim/SheetMusic/Kawai-RX-Series-Voicing-Manual.pdf
/Users/jim/piano/Marche Militaire.pdf

Explanation:
  \.  matches a period (normally . means any character)
  pdf matches those 3 letters
  $   matches only at the end of the pathname

#2. Show files up to 3 levels deep

$ hb ls -c hb -x '^(/[^/]*){1,3}$'
/
/Users
/Users/jim
/Users/jim/.CFUserTextEncoding
/Users/jim/.DS_Store
/Users/jim/.Trash  (but no .Trash entries)
/Users/jim/.bash_sessions

Explanation:
  ^      matches the beginning of the pathname
  (      starts a group
  /[^/]* a slash followed by non-slashes
  )      ends the group
  {1,3}  matches 1 to 3 groups
  $      matches the end of the pathname

Ls Output

$ hb ls -c hb -l
HashBackup build #1666 Copyright 2009-2016 HashBackup, LLC
Backup directory: /hb
Most recent backup version: 0
Showing most recent version
   0 drwxrwxr-t   44    0   80     1564 2016-10-12 00:30:04 2016-10-12 13:27:46 /  (parent, partial)
(Scroll this area left or right to see all fields)

The fields are:

  • version number

  • Unix permissions

  • number of links

  • user id

  • group id

  • file size

  • date modified (mtime)

  • date backed up

  • path name

  • HashBackup notes

Adding -v gives more details:

$ hb ls -c hb -lv
HashBackup build #1666 Copyright 2009-2016 HashBackup, LLC
Backup directory: /hb
Most recent backup version: 0
Showing most recent version
   0 drwxrwxr-t   44    0   80     1564 2016-10-12 00:30:04 2016-10-12 13:27:46 /  (parent, partial)
            2 2016-10-12 00:30:04          1
   0 -rw-r--r--    1  501   20      511 2016-10-12 13:27:39 2016-10-12 13:27:46 /hb/inex.conf
      5700934 2016-10-12 13:27:39          6                              [dc4094a430afa32b82247a118b23e56c64ee095a]
(Scroll this area left or right to see all fields)

The fields are the same for the first line, then on the second line:

  • inode number

  • inode change time (ctime)

  • HashBackup file id

  • file hash (SHA1 for non-sparse files)