9

My shell is bash. How can I get the output of ls to show directories with a trailing forward-slash? When I do ls in tcsh it gives the desired output. How can I get this to occur in bash without using any arguments?

eg.

bin/
lib/
src/
file1.txt
file2.txt
  • 7
    Your shell isn't relevant here... man ls then scroll down to -p – don_crissti Feb 23 '16 at 0:00
  • is it the -F flag? I never use it, so I can't remember... – Jeff Schaller Feb 23 '16 at 0:05
  • 1
    With a trailing backslash as per your title, or with a trailing slash (which makes more sense) as per your example? – Gilles Feb 23 '16 at 0:21
  • @don_crissti See my edit to the question. – CJ7 Feb 23 '16 at 0:23
  • @CJ7 - what is type ls output ? Maybe an alias ? – don_crissti Feb 23 '16 at 0:42
9

The simplest solution (as given already by @don_crissti in the comments) is:

ls -p

You can get a similar effect with:

ls -F

But that will add some other indicators as well:

Append a character to each file name indicating the file type. Also, for regular files that are executable, append *. The file type indicators are / for directories, @ for symbolic links, | for FIFOs, = for sockets, > for doors, and nothing for regular files.


Of course, you can make the string ls execute ls -p on the command line with an alias:

alias ls='ls -p'

That is temporal and could be erased with unalias ls.
Probably your tcsh has an active alias in place.

Which you can do by placing the command in ~/.bashrc or ~/.bash_aliases.

  • That last bit caught my attention. I've never seen or heard of a ~/.bash_aliases before; only ~/.bash_profile and/or ~.bashrc. Is it correct? – tjt263 Feb 29 '16 at 8:58
  • 1
    @tjt263 It is a common practice to place all aliases in a special file sourced from bashrc. The contents may vary depending on your needs. – user79743 Feb 29 '16 at 14:46
  • To keep colors: alias ls='ls --color=auto -p' – mkczyk Mar 15 at 18:16
1

You can use -F, e.g.

$ /bin/ls -F
-/                                 diesel_folks                 grub                     presidents             user_mailer/
0/                                 dmStaffing/                  HJJJH/                   prime_scenarios   
...
# I'm using /bin/ls to make sure I run my system ls 
# (I actually have it aliased to add options automatically normally)
#
# Just read about your no argument requirement
# So you too could alias it, i.e.

alias ls='ls -F'

# Put it in your `.bashrc` or `.bash_aliases` file
  • Do you know why tcsh is giving me desired output and bash isn't? – CJ7 Feb 23 '16 at 0:34
  • 1
    @CJ7 Because tcsh and bash use different configuration files (with different syntax). You presumably already have a similar alias in your .cshrc. – Gilles Feb 23 '16 at 0:41
  • I tried making a .bashrc file in my home directory but it didn't work – CJ7 Feb 23 '16 at 2:37
  • @CJ7, did you restart your shell after making that change? – Jochem Schulenklopper Sep 26 '17 at 6:43
0

Either

   ls --classify

Or:

   ls --indicator-style=slash

Explanation

   -F, --classify
          append indicator (one of */=>@|) to entries

   -p, --indicator-style=slash
          append / indicator to directories

The other answers more or less cover this, but I prefer GNU-style long options over BSD-style short options in Stack Overflow answers since they are more self-explanatory and learnable / "memorizable" / "internalizeable".

Your Answer

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

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