3

I'm on a remote server which is a Ubuntu 16.04.1 LTS and I need to list only directories in this folder called "NewsData" I found that ls -d */ is a good command to list folders, however it works in some folders and not in others.

Below is the sample output

(venv_p3.5) anjali@momo:/scratche/home/anjali$ ls -d */
archive/  DownloadImages/  fixed/  getNews/  html/  log/  MonumentData/  NewsData/  Pytorch-finetuning/  src/  TestData/  TrainData/  TrainData2/  venv_p3.5/  VGG16FeatureExtraction/
(venv_p3.5) anjali@momo:/scratche/home/anjali$ cd NewsData/
(venv_p3.5) anjali@momo:/scratche/home/anjali/NewsData$ ls -d */
ls: invalid option -- '/'
Try 'ls --help' for more information.
(venv_p3.5) anjali@momo:/scratche/home/anjali/NewsData$ ls 
-  A  B  C  cleanData.py  D  E  F  G  getList.py  H  I  J  K  L  M  N  O  P  Q  R  S  sequentialNumbering.sh  T  U  V  W  Y  Z

Why does this happen? how can I fix this?

1
  • 1
    Thank you. If you are happy with --, OK, if you still have problems on different platforms, please keep in mind that not all shells limit the */ expansion to directories only. So if there still is a difference check your shell and /bin/sh.
    – schily
    Commented Aug 14, 2018 at 15:09

2 Answers 2

18

The directory contains a subdirectory named -, so that expansion of */ by the shell includes -/, which is being misinterpreted as a command option.

You can avoid this by marking the end of options explicitly using -- i.e.

ls -d -- */

or by prepending the glob with a path

ls -d ./*/
2
  • 4
    Filename expansion is done by bash before the values are passed to the program. This is why ls -d */ can expand to ls -d -/.
    – bytesized
    Commented Aug 14, 2018 at 17:22
  • Thanks @bytesized I have edited the answer to clarify this point Commented Aug 15, 2018 at 20:14
2
find . -maxdepth 1 -type d ! -name "\.*"

"-d" means just list that directory as I understand it.

2
  • It's worth noting this omits all hidden directories as well as . and ..
    – Wildcard
    Commented Aug 14, 2018 at 23:17
  • @Wildcard: which glob expansion also does, at least by default Commented Aug 15, 2018 at 6:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .