Is there a simple way to group soft links to directories with directories when using --group-directories-first?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
2












I like the --group-directories-first flag for ls. My .zshrc has this line in it:



alias ls="ls -h --color='auto' --group-directories-first"



The only thing I don't like is that symbolic links to directories don't get grouped with the directories.



Are there any switches I can add to the ls command that will cause symbolic-links-to-directories to group with directories? I'm disinclined to build a compound command, because I like being able to add additional switches to ls on the fly.










share|improve this question

























    up vote
    0
    down vote

    favorite
    2












    I like the --group-directories-first flag for ls. My .zshrc has this line in it:



    alias ls="ls -h --color='auto' --group-directories-first"



    The only thing I don't like is that symbolic links to directories don't get grouped with the directories.



    Are there any switches I can add to the ls command that will cause symbolic-links-to-directories to group with directories? I'm disinclined to build a compound command, because I like being able to add additional switches to ls on the fly.










    share|improve this question























      up vote
      0
      down vote

      favorite
      2









      up vote
      0
      down vote

      favorite
      2






      2





      I like the --group-directories-first flag for ls. My .zshrc has this line in it:



      alias ls="ls -h --color='auto' --group-directories-first"



      The only thing I don't like is that symbolic links to directories don't get grouped with the directories.



      Are there any switches I can add to the ls command that will cause symbolic-links-to-directories to group with directories? I'm disinclined to build a compound command, because I like being able to add additional switches to ls on the fly.










      share|improve this question













      I like the --group-directories-first flag for ls. My .zshrc has this line in it:



      alias ls="ls -h --color='auto' --group-directories-first"



      The only thing I don't like is that symbolic links to directories don't get grouped with the directories.



      Are there any switches I can add to the ls command that will cause symbolic-links-to-directories to group with directories? I'm disinclined to build a compound command, because I like being able to add additional switches to ls on the fly.







      ls






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 30 at 6:43









      JoshuaD

      26929




      26929




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          One option is to use the -L flag:




          -L, --dereference



          when showing file information for a symbolic link, show information for the file the link references rather than for the link itself




          For example, assume I have the files and directories below:



          $ ls -l --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2
          lrwxrwxrwx. 1 user group 5 Nov 30 08:23 symlink -> zdir2


          The symlink gets sorted with the directories when -L is used:



          $ ls -lL --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 symlink
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2



          As you noticed, ls no longer indicates whether the files are symlinks are not. I'm not familiar with a way in ls to show a symlink once it's been dereferenced, but I wrote a hacky bash function to do it for you:



          lsd() while read line; do
          file=$(echo "$line"


          Essentially, the script will determine if the file output from ls -lL --group-directories-first is a symlink or not. If it is, the first character is changed to an l, and what the symlink points to is appended to the end of the line.



          $ lsd
          total 0
          lrwxr-xr-x. 2 user group 6 Nov 30 13:49 symlink -> zdir2
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file1
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file2





          share|improve this answer






















          • What if the symbolic link is pointing to file? OP only wants links pointing to directories.
            – Debian_yadav
            Nov 30 at 15:56






          • 1




            @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
            – Peschke
            Nov 30 at 16:03











          • That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
            – JoshuaD
            Nov 30 at 20:48










          • Related question: unix.stackexchange.com/questions/232394/…
            – JoshuaD
            Nov 30 at 20:49






          • 1




            @JoshuaD: See my latest edit.
            – Peschke
            Nov 30 at 21:33










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485080%2fis-there-a-simple-way-to-group-soft-links-to-directories-with-directories-when-u%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          One option is to use the -L flag:




          -L, --dereference



          when showing file information for a symbolic link, show information for the file the link references rather than for the link itself




          For example, assume I have the files and directories below:



          $ ls -l --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2
          lrwxrwxrwx. 1 user group 5 Nov 30 08:23 symlink -> zdir2


          The symlink gets sorted with the directories when -L is used:



          $ ls -lL --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 symlink
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2



          As you noticed, ls no longer indicates whether the files are symlinks are not. I'm not familiar with a way in ls to show a symlink once it's been dereferenced, but I wrote a hacky bash function to do it for you:



          lsd() while read line; do
          file=$(echo "$line"


          Essentially, the script will determine if the file output from ls -lL --group-directories-first is a symlink or not. If it is, the first character is changed to an l, and what the symlink points to is appended to the end of the line.



          $ lsd
          total 0
          lrwxr-xr-x. 2 user group 6 Nov 30 13:49 symlink -> zdir2
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file1
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file2





          share|improve this answer






















          • What if the symbolic link is pointing to file? OP only wants links pointing to directories.
            – Debian_yadav
            Nov 30 at 15:56






          • 1




            @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
            – Peschke
            Nov 30 at 16:03











          • That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
            – JoshuaD
            Nov 30 at 20:48










          • Related question: unix.stackexchange.com/questions/232394/…
            – JoshuaD
            Nov 30 at 20:49






          • 1




            @JoshuaD: See my latest edit.
            – Peschke
            Nov 30 at 21:33














          up vote
          2
          down vote













          One option is to use the -L flag:




          -L, --dereference



          when showing file information for a symbolic link, show information for the file the link references rather than for the link itself




          For example, assume I have the files and directories below:



          $ ls -l --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2
          lrwxrwxrwx. 1 user group 5 Nov 30 08:23 symlink -> zdir2


          The symlink gets sorted with the directories when -L is used:



          $ ls -lL --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 symlink
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2



          As you noticed, ls no longer indicates whether the files are symlinks are not. I'm not familiar with a way in ls to show a symlink once it's been dereferenced, but I wrote a hacky bash function to do it for you:



          lsd() while read line; do
          file=$(echo "$line"


          Essentially, the script will determine if the file output from ls -lL --group-directories-first is a symlink or not. If it is, the first character is changed to an l, and what the symlink points to is appended to the end of the line.



          $ lsd
          total 0
          lrwxr-xr-x. 2 user group 6 Nov 30 13:49 symlink -> zdir2
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file1
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file2





          share|improve this answer






















          • What if the symbolic link is pointing to file? OP only wants links pointing to directories.
            – Debian_yadav
            Nov 30 at 15:56






          • 1




            @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
            – Peschke
            Nov 30 at 16:03











          • That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
            – JoshuaD
            Nov 30 at 20:48










          • Related question: unix.stackexchange.com/questions/232394/…
            – JoshuaD
            Nov 30 at 20:49






          • 1




            @JoshuaD: See my latest edit.
            – Peschke
            Nov 30 at 21:33












          up vote
          2
          down vote










          up vote
          2
          down vote









          One option is to use the -L flag:




          -L, --dereference



          when showing file information for a symbolic link, show information for the file the link references rather than for the link itself




          For example, assume I have the files and directories below:



          $ ls -l --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2
          lrwxrwxrwx. 1 user group 5 Nov 30 08:23 symlink -> zdir2


          The symlink gets sorted with the directories when -L is used:



          $ ls -lL --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 symlink
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2



          As you noticed, ls no longer indicates whether the files are symlinks are not. I'm not familiar with a way in ls to show a symlink once it's been dereferenced, but I wrote a hacky bash function to do it for you:



          lsd() while read line; do
          file=$(echo "$line"


          Essentially, the script will determine if the file output from ls -lL --group-directories-first is a symlink or not. If it is, the first character is changed to an l, and what the symlink points to is appended to the end of the line.



          $ lsd
          total 0
          lrwxr-xr-x. 2 user group 6 Nov 30 13:49 symlink -> zdir2
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file1
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file2





          share|improve this answer














          One option is to use the -L flag:




          -L, --dereference



          when showing file information for a symbolic link, show information for the file the link references rather than for the link itself




          For example, assume I have the files and directories below:



          $ ls -l --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2
          lrwxrwxrwx. 1 user group 5 Nov 30 08:23 symlink -> zdir2


          The symlink gets sorted with the directories when -L is used:



          $ ls -lL --group-directories-first
          total 0
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 symlink
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 08:19 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file1
          -rw-r--r--. 1 user group 0 Nov 30 08:20 file2



          As you noticed, ls no longer indicates whether the files are symlinks are not. I'm not familiar with a way in ls to show a symlink once it's been dereferenced, but I wrote a hacky bash function to do it for you:



          lsd() while read line; do
          file=$(echo "$line"


          Essentially, the script will determine if the file output from ls -lL --group-directories-first is a symlink or not. If it is, the first character is changed to an l, and what the symlink points to is appended to the end of the line.



          $ lsd
          total 0
          lrwxr-xr-x. 2 user group 6 Nov 30 13:49 symlink -> zdir2
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir1
          drwxr-xr-x. 2 user group 6 Nov 30 13:49 zdir2
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file1
          -rw-r--r--. 1 user group 0 Nov 30 13:49 file2






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 30 at 21:32

























          answered Nov 30 at 15:31









          Peschke

          2,400924




          2,400924











          • What if the symbolic link is pointing to file? OP only wants links pointing to directories.
            – Debian_yadav
            Nov 30 at 15:56






          • 1




            @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
            – Peschke
            Nov 30 at 16:03











          • That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
            – JoshuaD
            Nov 30 at 20:48










          • Related question: unix.stackexchange.com/questions/232394/…
            – JoshuaD
            Nov 30 at 20:49






          • 1




            @JoshuaD: See my latest edit.
            – Peschke
            Nov 30 at 21:33
















          • What if the symbolic link is pointing to file? OP only wants links pointing to directories.
            – Debian_yadav
            Nov 30 at 15:56






          • 1




            @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
            – Peschke
            Nov 30 at 16:03











          • That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
            – JoshuaD
            Nov 30 at 20:48










          • Related question: unix.stackexchange.com/questions/232394/…
            – JoshuaD
            Nov 30 at 20:49






          • 1




            @JoshuaD: See my latest edit.
            – Peschke
            Nov 30 at 21:33















          What if the symbolic link is pointing to file? OP only wants links pointing to directories.
          – Debian_yadav
          Nov 30 at 15:56




          What if the symbolic link is pointing to file? OP only wants links pointing to directories.
          – Debian_yadav
          Nov 30 at 15:56




          1




          1




          @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
          – Peschke
          Nov 30 at 16:03





          @Debian_yadav: The -L flag will dereference the symlink and see that it points to a file, and won't place it with the directories at the top.
          – Peschke
          Nov 30 at 16:03













          That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
          – JoshuaD
          Nov 30 at 20:48




          That's pretty close to great. Do you know if there's a way to get it to keep the symlink -> target notation as well?
          – JoshuaD
          Nov 30 at 20:48












          Related question: unix.stackexchange.com/questions/232394/…
          – JoshuaD
          Nov 30 at 20:49




          Related question: unix.stackexchange.com/questions/232394/…
          – JoshuaD
          Nov 30 at 20:49




          1




          1




          @JoshuaD: See my latest edit.
          – Peschke
          Nov 30 at 21:33




          @JoshuaD: See my latest edit.
          – Peschke
          Nov 30 at 21:33

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485080%2fis-there-a-simple-way-to-group-soft-links-to-directories-with-directories-when-u%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay