0

Could you please explain what each option on this ls command does: ls -td -- */? The result of such command would look like below:

$ ls
$ ls -al
total 4
drwxr-xr-x   5 root root   68 Jun  4 09:58 .
drwxrwxrwt. 13 root root 4096 Jun  4 10:05 ..
drwxr-xr-x   5 root root   36 May 31 15:48 05-31-2018
drwxr-xr-x   5 root root   36 Jun  4 09:45 06-04-2018
drwxr-xr-x   2 root root    6 Jun  4 09:56 06-05-2018
-rw-r--r--   1 root root    0 Jun  4 09:58 test

$  ls -td -- */ 
06-05-2018/  06-04-2018/  05-31-2018/

# To get latest folder created:
$ ls -td -- */ | head -n 1
06-05-2018/

I have no ideas what each option would do with ls command.

migrated from serverfault.com Jun 7 '18 at 11:07

This question came from our site for system and network administrators.

  • 2
    -td is two options: -t and -d. Check your ls man page for their meanings. -- indicates the end of the options. This is useful to prevent any filenames that happen to begin with a hyphen from affecting ls – glenn jackman Jun 4 '18 at 14:47
1

-td is the two options -t and -d written together. -t tells ls to sort the output based on time, and -d asks to show directories named on the command line as themselves, instead of their contents.

The -- option is as far as I know not explicitly documented for many commands that do support it and it has become a slightly obscure syntax.
It finds it's origins in the getopt function and is use to delimit the end of the options and the start of the parameters.

You would mainly use that -- syntax to use parameters that would otherwise look like options.

A good illustration is trying to manipulate files that start their names with a hyphen such as a file called "-rm -rf"

Create it with touch -- '-rm -rf'

ls -la
total 0
-rw-r--r--   1 herman  wheel    0 Jun  4 16:46 -rm -rf

ls -la *
ls: illegal option --
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

ls -la -- * 
total 0
-rw-r--r--   1 herman  wheel    0 Jun  4 16:46 -rm -rf

and

rm -i *
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

versus

rm -i -- *

For the meaning of command line options in general, this very basic nugget:

Nearly all Linux commands come with an online manual explaining their usage and various options that modify their behaviour. Than manual can be accessed with the man command i.e.

man ls

Try man man for an explanation of the manual.

  • 2
    You also use -- when you don't know whether the names may start with - as in ls -d -- * or ls -d -- "$1". A more striking example is with a file called -r with rm. Where a rm * can become a rm -r * and end up deleting a lot more than intended. – Stéphane Chazelas Jun 7 '18 at 11:56
  • I'm a bit tempted to change the example to something other than -rf (though the preceding space should cause an error in any case.) – ilkkachu Jun 7 '18 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy