14

What is the meaning of following ls command?

ls -l *\:2,*T
  • ls = list
  • -l = long (list)
  • *T probably means T at the end

But what about that *\:2 and the comma?

Arjan
  • 30,974
  • 14
  • 75
  • 112
Jürgen Gmach
  • 259
  • 2
  • 5

3 Answers3

25

*\:2,*T is the glob pattern of files to list. To understand it, we need to remember a few things:

  • : has to be escaped in the shell, becoming \:
  • File names can easily contain commas

So *\:2,*T would e.g. match a file called TranscationNumber:2,EventType:XYT

EDIT

From the comments, the necessity (or not) of escaping the : needs a few words: It is not strictly necessary to escape the : sign, but bash itself suggests it when autocompleting.

Eugen Rieck
  • 19,950
  • 5
  • 51
  • 46
  • 3
    Thank you, both Eugen and HBruijn. I suspected some complicated globbing pattern, but actually it should just match the colon and the comma, which I did not think of when working with files. Now, the command totally makes sense: list all email files in the Maildir directories, which are marked as "Deleted" (T). Btw. The number 2 means Maildir in version 2 (cf https://wiki2.dovecot.org/MailboxFormat/Maildir). – Jürgen Gmach Aug 18 '17 at 11:20
  • 11
    Since when does `:` have to be escaped? – Barmar Aug 18 '17 at 17:48
  • 7
    `:` doesn't need to be escaped. It is only special in specific circumstances (when used as a command), not in glob patterns. The `\:` is equivalent to `:`, escaping makes no difference. – terdon Aug 18 '17 at 18:11
  • @terdon I was wondering in which case it would be interesting to have `\:` as a command, and found out that `bash` still seems to parse `\:` as the NOP command. See [this ideone](http://ideone.com/U4GWei). I guess it was different in older versions of bash or other shells? – Aaron Aug 19 '17 at 00:59
  • @Barmar Edited my answer, thanks for pointing this out! – Eugen Rieck Aug 19 '17 at 01:33
  • 1
    @Aaron, it's not *exactly* no-op: the shell will still process variable substitutions, particularly the assignment variety (i.e. to set default values): `unset var; : ${var:=x}; echo $var` – glenn jackman Aug 19 '17 at 13:23
  • @Aaron I wouldn't expect `\:` to protect if from being recognized as a command since that is another common construct used, for example, to [ignore aliases](https://askubuntu.com/a/525242/85695). On the other hand, tab completion on a file whose name has a `:` results in `\:`. I don't know why that would happen but I would guess that it is historical and that older shells would choke on it. I tested on bash, dash, tcsh and ksh and all of them had no issue dealing with `:`. Presumably, some older shells did. – terdon Aug 22 '17 at 15:15
  • @terdon thanks, I didn't know about that use of backslash, I would have used `command` or `builtin` to achieve the same effect. There's definitely something weird with the auto-completion : I've tested with bash on both a Cygwin and an RHEL and got some weird behaviors, including the one you mention, the inability to complete just after the `:` or even worse, completion proposals based only on the part following the `:` (i.e. completion after `test:te` proposing every file starting with `te`). Looks like escaping them can be a good idea. – Aaron Aug 22 '17 at 16:42
  • 3
    @terdon & @Aaron The colon `:` is part of the default values for the `COMP_WORDBREAKS` environment variable. In Bash autocompletion those characters are used as word separators by the read line library and a file-name containing a colon should of course be a single word. Hence the reason that bash autocompletion requires the colon to be escaped, although otherwise it is "*mostly*" not a special character... – HBruijn Aug 23 '17 at 07:49
14

List all files that match the wildcard pattern *:2,*T

There the wildcard * matches anything (any number of all possible characters)
:2, are characters that need to be present in the file/directory names.
The colon : is a special character that needs to be escaped, hence the form of \:2,.
The file/directory names need to end with a T.

File names that would match would be

:2,T
a:2,T
a:2,bT
abbY-$fafaf:2,<hskjhsgdfhjk>T
shA.t
  • 488
  • 4
  • 18
HBruijn
  • 1,286
  • 1
  • 7
  • 12
  • 1
    There's nothing special about :, it doesn't need to be escaped. – Kevin Aug 18 '17 at 19:05
  • @Kevin bash auto-completion automatically escapes the colon in file/directory name patterns, because is included in the list of `COMP_WORDBREAK` environment values that get set by default. - So it is indeed not an actual special character, but when you use autocompletion on the commandline and want/need to match filenames with a colon , it does need to be escaped. – HBruijn Aug 23 '17 at 07:39
9

As others have noted, this will list in long format, files containing :2, and ending in T

This looks like a search in a Maildir folder for files that were deleted (trashed). However, for robustness it should have had another * at the end, though. New flags with a later alphabetical position could be added, and Dovecot for instance adds another field with the file size at the end.

Ángel
  • 1,218
  • 1
  • 8
  • 11