1

Can anyone tell me what is the difference between the ls and ls -p commands in Linux?

  • 2
    Did you try it? did you read the manual page (man ls)? – steeldriver Jul 21 '17 at 15:41
6

The -p option adds a / at the end of directories, it helps you easily detect which one of the outputs is a directory and which one is a file.

A similar option to -p is -F which uses these characters to indicate:

  • / --> directories
  • @ --> symbolic links
  • | --> fifo files (named pipes).

On the other hand, ls does not do any of these.

These options are useful when you don't use an option like --color or your terminal does not support colorizing the output.


$ ls
block   dm-1    hwrng

It's hard to tell which one is a directory and which one is a file, right? So:

$ ls -p
block/   dm-1    hwrng

Now I can tell the block is a directory.

| improve this answer | |
  • I am using cygwin64 on windows and it shows both commands as same. – gurupal singh Jul 21 '17 at 16:19
  • @gurupalsingh what is the output of alias ls command? – Ravexina Jul 21 '17 at 16:26
  • You can also run ""ls or command ls to make sure ls is being run without any other options, I don't know if it's going to work on cygwin64 or not. but give it a shot. – Ravexina Jul 21 '17 at 16:28
  • @gurupalsingh If you are using cygwin, you shouldn't be asking questions on Ubuntu site. We only deal with Ubuntu tools and any difference in behavior of those tools isn't really relevant to this site. There is unix.stackexchange.com where cygwin is welcome. – Sergiy Kolodyazhnyy Jul 21 '17 at 16:42
  • but it comes with linux commands (in-built).... right ? – gurupal singh Jul 21 '17 at 16:47
4

ls -p adds a / after folder names, ls doesn't.

| improve this answer | |
  • sorry but i can see that both commands does the same, i mean both commands adds the slash after folder names. I am using cygwin64 in windows. – gurupal singh Jul 21 '17 at 15:55
  • @gurupalsingh - The output may look the same on cygwin, but that is the difference. You can also run bash on windows - insights.ubuntu.com/2016/03/30/… – Panther Jul 21 '17 at 16:20
  • but why not on cygwin , this is my concern, nothing else. Now don't ask me to ask the developer who develop cygwin64 #lol – gurupal singh Jul 21 '17 at 16:35
1

From man ls (Which has documentation of all Linux commands)

ls

   ls - list directory contents

ls -p

   -p, --indicator-style=slash
          append / indicator to directories
| improve this answer | |
0

If you don't notice the effect of -p, you should check what ls is aliased to.

As others have said, ls -p causes directory names to be shown with a trailing /. However, sometimes people ask about what flags like -p and -F do because they may appear to have no effect.

Typically the effect is clearly apparent:

ek@Io:~/tmp$ ls
directory  regular-file
ek@Io:~/tmp$ ls -p
directory/  regular-file

However, it is common for users to have a shell alias ls that adds options. If one of those options is -p or another option that adds the trailing slash to directory names, then adding -p yourself won't make any difference.

In Ubuntu, type ls usually shows ls is aliased to `ls --color=auto'. (This alias is provided by alias ls='ls --color=auto' in the user's .bashrc file, which is copied from /etc/skel when the user account is created.) When a user runs ls on an interactive shell prompt, it is replaced by ls --color=auto.

If -p appears to have no effect when you pass it to ls, try running type ls or alias ls. (Since it is possible, though rare, to have a shell function for ls, type is more reliable than alias at figuring out what is going on here.) It may reveal that you have an ls alias that includes -p, --indicator-style=slash, -F, or --classify.

There are a few ways to bypass alias expansion:

command ls
\ls
/bin/ls

That third command is only equivalent if the ls executable is /bin/ls. That is almost always the case; it would only be different if a second ls executable were added elsewhere. Run type -a ls to see.

| improve this answer | |

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