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
    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 Aug 14 at 15:09
up vote 17 down vote accepted

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 ./*/
  • 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 Aug 14 at 17:22
  • Thanks @bytesized I have edited the answer to clarify this point – steeldriver Aug 15 at 20:14
find . -maxdepth 1 -type d ! -name "\.*"

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

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.