Remove the number of hard links from the output of `ls -al` command

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











up vote
1
down vote

favorite












Here is outpu of ls -al command:



total 280K
drwxrwxr-x 2 enan enan 4.0K Jun 28 09:59 ranger/
drwxrwxr-x 2 enan enan 4.0K Jun 8 23:26 scripts/
-rw-rw-r-- 1 enan enan 49 Jun 4 18:19 caps_to_ctrl.sh
-rw-rw-r-- 1 enan enan 739 Jun 4 18:19 cmus.theme
-rw-rw-r-- 1 enan enan 8.6K Jun 4 18:19 compton.conf
-rw-rw-r-- 1 enan enan 5.9K Jun 22 09:39 config
-rw-rw-r-- 1 enan enan 859 Jun 14 12:13 i3blocks.conf
-rw-rw-r-- 1 enan enan 4.7K Jun 14 12:13 init.el
-rw-rw-r-- 1 enan enan 8.7K Jun 28 11:22 init.vim
-rw-rw-r-- 1 enan enan 815 Jun 14 12:13 install.sh
-rwxrwxr-x 1 enan enan 142 Jun 4 18:19 lock.sh*
-rw-rw-r-- 1 enan enan 1.1K Jun 21 13:30 README.md
-rw-rw-r-- 1 enan enan 197K Jun 14 12:13 screenshot.png
-rw-rw-r-- 1 enan enan 1.2K Jun 20 21:51 Session.vim
-rw-rw-r-- 1 enan enan 427 Jun 9 13:46 tmux.sh


You can see there is a number between the file permissions -rw-rw-r-- and file owner enan. That number is the number of hard links of the file. Showing that in the output might be useful for some cases but right now, I don't need it and it distracts me sometimes. You can see, I had a way to remove the last modified date from the output of ls -al because I don't need it right now.



So, how can I remove the number of hard links from the output of ls -al command? And what is the importance of having the number of hard links and last modified date being shown in the output of the command? I mean, if any of you use those info, tell me why and how you do so, cause I don't wanna unintentionally shadow a behavior that I should use.







share|improve this question





















  • Please don't post screenshots of text. Copy the text here and apply code formatting.
    – muru
    Jun 28 at 4:35










  • @muru Sorry, I didn't think of that. Edited the post.
    – Enan Ajmain
    Jun 28 at 5:28














up vote
1
down vote

favorite












Here is outpu of ls -al command:



total 280K
drwxrwxr-x 2 enan enan 4.0K Jun 28 09:59 ranger/
drwxrwxr-x 2 enan enan 4.0K Jun 8 23:26 scripts/
-rw-rw-r-- 1 enan enan 49 Jun 4 18:19 caps_to_ctrl.sh
-rw-rw-r-- 1 enan enan 739 Jun 4 18:19 cmus.theme
-rw-rw-r-- 1 enan enan 8.6K Jun 4 18:19 compton.conf
-rw-rw-r-- 1 enan enan 5.9K Jun 22 09:39 config
-rw-rw-r-- 1 enan enan 859 Jun 14 12:13 i3blocks.conf
-rw-rw-r-- 1 enan enan 4.7K Jun 14 12:13 init.el
-rw-rw-r-- 1 enan enan 8.7K Jun 28 11:22 init.vim
-rw-rw-r-- 1 enan enan 815 Jun 14 12:13 install.sh
-rwxrwxr-x 1 enan enan 142 Jun 4 18:19 lock.sh*
-rw-rw-r-- 1 enan enan 1.1K Jun 21 13:30 README.md
-rw-rw-r-- 1 enan enan 197K Jun 14 12:13 screenshot.png
-rw-rw-r-- 1 enan enan 1.2K Jun 20 21:51 Session.vim
-rw-rw-r-- 1 enan enan 427 Jun 9 13:46 tmux.sh


You can see there is a number between the file permissions -rw-rw-r-- and file owner enan. That number is the number of hard links of the file. Showing that in the output might be useful for some cases but right now, I don't need it and it distracts me sometimes. You can see, I had a way to remove the last modified date from the output of ls -al because I don't need it right now.



So, how can I remove the number of hard links from the output of ls -al command? And what is the importance of having the number of hard links and last modified date being shown in the output of the command? I mean, if any of you use those info, tell me why and how you do so, cause I don't wanna unintentionally shadow a behavior that I should use.







share|improve this question





















  • Please don't post screenshots of text. Copy the text here and apply code formatting.
    – muru
    Jun 28 at 4:35










  • @muru Sorry, I didn't think of that. Edited the post.
    – Enan Ajmain
    Jun 28 at 5:28












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Here is outpu of ls -al command:



total 280K
drwxrwxr-x 2 enan enan 4.0K Jun 28 09:59 ranger/
drwxrwxr-x 2 enan enan 4.0K Jun 8 23:26 scripts/
-rw-rw-r-- 1 enan enan 49 Jun 4 18:19 caps_to_ctrl.sh
-rw-rw-r-- 1 enan enan 739 Jun 4 18:19 cmus.theme
-rw-rw-r-- 1 enan enan 8.6K Jun 4 18:19 compton.conf
-rw-rw-r-- 1 enan enan 5.9K Jun 22 09:39 config
-rw-rw-r-- 1 enan enan 859 Jun 14 12:13 i3blocks.conf
-rw-rw-r-- 1 enan enan 4.7K Jun 14 12:13 init.el
-rw-rw-r-- 1 enan enan 8.7K Jun 28 11:22 init.vim
-rw-rw-r-- 1 enan enan 815 Jun 14 12:13 install.sh
-rwxrwxr-x 1 enan enan 142 Jun 4 18:19 lock.sh*
-rw-rw-r-- 1 enan enan 1.1K Jun 21 13:30 README.md
-rw-rw-r-- 1 enan enan 197K Jun 14 12:13 screenshot.png
-rw-rw-r-- 1 enan enan 1.2K Jun 20 21:51 Session.vim
-rw-rw-r-- 1 enan enan 427 Jun 9 13:46 tmux.sh


You can see there is a number between the file permissions -rw-rw-r-- and file owner enan. That number is the number of hard links of the file. Showing that in the output might be useful for some cases but right now, I don't need it and it distracts me sometimes. You can see, I had a way to remove the last modified date from the output of ls -al because I don't need it right now.



So, how can I remove the number of hard links from the output of ls -al command? And what is the importance of having the number of hard links and last modified date being shown in the output of the command? I mean, if any of you use those info, tell me why and how you do so, cause I don't wanna unintentionally shadow a behavior that I should use.







share|improve this question













Here is outpu of ls -al command:



total 280K
drwxrwxr-x 2 enan enan 4.0K Jun 28 09:59 ranger/
drwxrwxr-x 2 enan enan 4.0K Jun 8 23:26 scripts/
-rw-rw-r-- 1 enan enan 49 Jun 4 18:19 caps_to_ctrl.sh
-rw-rw-r-- 1 enan enan 739 Jun 4 18:19 cmus.theme
-rw-rw-r-- 1 enan enan 8.6K Jun 4 18:19 compton.conf
-rw-rw-r-- 1 enan enan 5.9K Jun 22 09:39 config
-rw-rw-r-- 1 enan enan 859 Jun 14 12:13 i3blocks.conf
-rw-rw-r-- 1 enan enan 4.7K Jun 14 12:13 init.el
-rw-rw-r-- 1 enan enan 8.7K Jun 28 11:22 init.vim
-rw-rw-r-- 1 enan enan 815 Jun 14 12:13 install.sh
-rwxrwxr-x 1 enan enan 142 Jun 4 18:19 lock.sh*
-rw-rw-r-- 1 enan enan 1.1K Jun 21 13:30 README.md
-rw-rw-r-- 1 enan enan 197K Jun 14 12:13 screenshot.png
-rw-rw-r-- 1 enan enan 1.2K Jun 20 21:51 Session.vim
-rw-rw-r-- 1 enan enan 427 Jun 9 13:46 tmux.sh


You can see there is a number between the file permissions -rw-rw-r-- and file owner enan. That number is the number of hard links of the file. Showing that in the output might be useful for some cases but right now, I don't need it and it distracts me sometimes. You can see, I had a way to remove the last modified date from the output of ls -al because I don't need it right now.



So, how can I remove the number of hard links from the output of ls -al command? And what is the importance of having the number of hard links and last modified date being shown in the output of the command? I mean, if any of you use those info, tell me why and how you do so, cause I don't wanna unintentionally shadow a behavior that I should use.









share|improve this question












share|improve this question




share|improve this question








edited Jun 28 at 5:28
























asked Jun 28 at 2:31









Enan Ajmain

1599




1599











  • Please don't post screenshots of text. Copy the text here and apply code formatting.
    – muru
    Jun 28 at 4:35










  • @muru Sorry, I didn't think of that. Edited the post.
    – Enan Ajmain
    Jun 28 at 5:28
















  • Please don't post screenshots of text. Copy the text here and apply code formatting.
    – muru
    Jun 28 at 4:35










  • @muru Sorry, I didn't think of that. Edited the post.
    – Enan Ajmain
    Jun 28 at 5:28















Please don't post screenshots of text. Copy the text here and apply code formatting.
– muru
Jun 28 at 4:35




Please don't post screenshots of text. Copy the text here and apply code formatting.
– muru
Jun 28 at 4:35












@muru Sorry, I didn't think of that. Edited the post.
– Enan Ajmain
Jun 28 at 5:28




@muru Sorry, I didn't think of that. Edited the post.
– Enan Ajmain
Jun 28 at 5:28










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










If you don't want to see the links which are in the second column then you can pipe ls -al into awk to remove it:



ls -al | awk '$2=""; print $0'


That will print all but the second column using the standard delimiter in this case which is a space.



In my environment, the columns for the last modified date are 6, 7, and 8 using space as the delimiter so to get rid of them and the second column:



ls -la | awk '$2=$6=$7=$8=""; print $0'


If you don't need or want to see some of the columns then you can use the syntax above as a model to remove whichever ones you desire. It's not going to hurt anything. If you just want to see all the columns as not to miss anything then use the standard:



ls -al


If you want an alias, add the following to ~/.bashrc:



la() 
ls -la


That's a function that behaves in the same way without having to account for and escape the single and double quotes. I used la as ll gives a syntax error as it's already an alias in my environment.






share|improve this answer























  • Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
    – Enan Ajmain
    Jun 28 at 3:29










  • See my update for a function as an alias.
    – Nasir Riley
    Jun 28 at 4:02










  • I don't see anything. Where is the update?
    – Enan Ajmain
    Jun 28 at 4:05






  • 1




    @EnanAjmain There it is. la is the alias
    – Nasir Riley
    Jun 28 at 4:05











  • Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
    – Enan Ajmain
    Jun 28 at 4:09

















up vote
1
down vote













To remove the hard link count from the output of ls -l while still leaving the rest of the line intact:



ls -l | sed -E '2,$s/ +[0-9]+//'


The sed substitution will remove the first set of digits and any space before it on each line after the first line (the first line contains the total).



As a function



myls () sed -E '2,$s/ +[0-9]+//'



This function invokes ls -l with any other command line arguments given by the user to the function before sending the result through sed.



By using command ls, we ensure that, if you later decide to rename the function into ls, the function would not call itself recursively.



Example:



$ ls -la ~/.skel
total 18
drwxr-xr-x 2 kk wheel 512 May 4 16:29 .
drwxr-xr-x 3 kk wheel 512 Jun 28 08:15 ..
-rw-r--r-- 1 kk wheel 87 Nov 1 2017 .Xdefaults
-rw-r--r-- 1 kk wheel 771 Feb 9 10:18 .cshrc
-rw-r--r-- 1 kk wheel 101 Nov 1 2017 .cvsrc
-rw-r--r-- 1 kk wheel 359 Nov 1 2017 .login
-rw-r--r-- 1 kk wheel 175 Nov 1 2017 .mailrc
-rw-r--r-- 1 kk wheel 229 May 4 16:29 .profile
-rw-r--r-- 1 kk wheel 108 Apr 15 12:50 .vimrc
-rw-r--r-- 1 kk wheel 0 Apr 21 17:44 .zshrc

$ myls -a ~/.skel
total
drwxr-xr-x kk wheel 512 May 4 16:29 .
drwxr-xr-x kk wheel 512 Jun 28 08:15 ..
-rw-r--r-- kk wheel 87 Nov 1 2017 .Xdefaults
-rw-r--r-- kk wheel 771 Feb 9 10:18 .cshrc
-rw-r--r-- kk wheel 101 Nov 1 2017 .cvsrc
-rw-r--r-- kk wheel 359 Nov 1 2017 .login
-rw-r--r-- kk wheel 175 Nov 1 2017 .mailrc
-rw-r--r-- kk wheel 229 May 4 16:29 .profile
-rw-r--r-- kk wheel 108 Apr 15 12:50 .vimrc
-rw-r--r-- kk wheel 0 Apr 21 17:44 .zshrc



A variation of the function that acts exactly like ls unless you use ls -l:



ls () (
add_filter=0
while getopts ':l' opt; do
case $opt in
l) add_filter=1 ;;
1) add_filter=0
esac
done

command ls "$@" |
if [ "$add_filter" -eq 1 ]; then
sed -E '2,$s/ +[0-9]+//'
else
cat
fi
)


The function does its own command line parsing to detect whether the -l option was used (and also not later disabled through the use of -1). If it was, we set a flag. Later, if the flag was set, we filter the output of ls through sed. If the flag wasn't set, we just pass the output as-is through cat.






share|improve this answer



















  • 2




    The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
    – dave_thompson_085
    Jun 28 at 6:23











  • @dave_thompson_085 Thanks for that!
    – Kusalananda
    Jun 28 at 6:30

















up vote
0
down vote













With ast-open's ls (also the builtin ls of ksh93 if it has been included and /opt/ast/bin is ahead of /bin in $PATH), you can fully specify the output format and omit the number of links (%3(nlink)u) there:



ls -rSZ '%(mode)s %-8(uid)s %-8(gid)s %8(device:case::%(size)u:*:%(device)s)s %(atime)s %(name)s%(linkop:case:?*: %(linkop)s %(linkpath)s)s'





share|improve this answer





















    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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f452339%2fremove-the-number-of-hard-links-from-the-output-of-ls-al-command%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    If you don't want to see the links which are in the second column then you can pipe ls -al into awk to remove it:



    ls -al | awk '$2=""; print $0'


    That will print all but the second column using the standard delimiter in this case which is a space.



    In my environment, the columns for the last modified date are 6, 7, and 8 using space as the delimiter so to get rid of them and the second column:



    ls -la | awk '$2=$6=$7=$8=""; print $0'


    If you don't need or want to see some of the columns then you can use the syntax above as a model to remove whichever ones you desire. It's not going to hurt anything. If you just want to see all the columns as not to miss anything then use the standard:



    ls -al


    If you want an alias, add the following to ~/.bashrc:



    la() 
    ls -la


    That's a function that behaves in the same way without having to account for and escape the single and double quotes. I used la as ll gives a syntax error as it's already an alias in my environment.






    share|improve this answer























    • Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
      – Enan Ajmain
      Jun 28 at 3:29










    • See my update for a function as an alias.
      – Nasir Riley
      Jun 28 at 4:02










    • I don't see anything. Where is the update?
      – Enan Ajmain
      Jun 28 at 4:05






    • 1




      @EnanAjmain There it is. la is the alias
      – Nasir Riley
      Jun 28 at 4:05











    • Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
      – Enan Ajmain
      Jun 28 at 4:09














    up vote
    2
    down vote



    accepted










    If you don't want to see the links which are in the second column then you can pipe ls -al into awk to remove it:



    ls -al | awk '$2=""; print $0'


    That will print all but the second column using the standard delimiter in this case which is a space.



    In my environment, the columns for the last modified date are 6, 7, and 8 using space as the delimiter so to get rid of them and the second column:



    ls -la | awk '$2=$6=$7=$8=""; print $0'


    If you don't need or want to see some of the columns then you can use the syntax above as a model to remove whichever ones you desire. It's not going to hurt anything. If you just want to see all the columns as not to miss anything then use the standard:



    ls -al


    If you want an alias, add the following to ~/.bashrc:



    la() 
    ls -la


    That's a function that behaves in the same way without having to account for and escape the single and double quotes. I used la as ll gives a syntax error as it's already an alias in my environment.






    share|improve this answer























    • Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
      – Enan Ajmain
      Jun 28 at 3:29










    • See my update for a function as an alias.
      – Nasir Riley
      Jun 28 at 4:02










    • I don't see anything. Where is the update?
      – Enan Ajmain
      Jun 28 at 4:05






    • 1




      @EnanAjmain There it is. la is the alias
      – Nasir Riley
      Jun 28 at 4:05











    • Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
      – Enan Ajmain
      Jun 28 at 4:09












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    If you don't want to see the links which are in the second column then you can pipe ls -al into awk to remove it:



    ls -al | awk '$2=""; print $0'


    That will print all but the second column using the standard delimiter in this case which is a space.



    In my environment, the columns for the last modified date are 6, 7, and 8 using space as the delimiter so to get rid of them and the second column:



    ls -la | awk '$2=$6=$7=$8=""; print $0'


    If you don't need or want to see some of the columns then you can use the syntax above as a model to remove whichever ones you desire. It's not going to hurt anything. If you just want to see all the columns as not to miss anything then use the standard:



    ls -al


    If you want an alias, add the following to ~/.bashrc:



    la() 
    ls -la


    That's a function that behaves in the same way without having to account for and escape the single and double quotes. I used la as ll gives a syntax error as it's already an alias in my environment.






    share|improve this answer















    If you don't want to see the links which are in the second column then you can pipe ls -al into awk to remove it:



    ls -al | awk '$2=""; print $0'


    That will print all but the second column using the standard delimiter in this case which is a space.



    In my environment, the columns for the last modified date are 6, 7, and 8 using space as the delimiter so to get rid of them and the second column:



    ls -la | awk '$2=$6=$7=$8=""; print $0'


    If you don't need or want to see some of the columns then you can use the syntax above as a model to remove whichever ones you desire. It's not going to hurt anything. If you just want to see all the columns as not to miss anything then use the standard:



    ls -al


    If you want an alias, add the following to ~/.bashrc:



    la() 
    ls -la


    That's a function that behaves in the same way without having to account for and escape the single and double quotes. I used la as ll gives a syntax error as it's already an alias in my environment.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jun 28 at 4:16


























    answered Jun 28 at 3:03









    Nasir Riley

    1,494138




    1,494138











    • Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
      – Enan Ajmain
      Jun 28 at 3:29










    • See my update for a function as an alias.
      – Nasir Riley
      Jun 28 at 4:02










    • I don't see anything. Where is the update?
      – Enan Ajmain
      Jun 28 at 4:05






    • 1




      @EnanAjmain There it is. la is the alias
      – Nasir Riley
      Jun 28 at 4:05











    • Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
      – Enan Ajmain
      Jun 28 at 4:09
















    • Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
      – Enan Ajmain
      Jun 28 at 3:29










    • See my update for a function as an alias.
      – Nasir Riley
      Jun 28 at 4:02










    • I don't see anything. Where is the update?
      – Enan Ajmain
      Jun 28 at 4:05






    • 1




      @EnanAjmain There it is. la is the alias
      – Nasir Riley
      Jun 28 at 4:05











    • Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
      – Enan Ajmain
      Jun 28 at 4:09















    Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
    – Enan Ajmain
    Jun 28 at 3:29




    Can you give me an alias for ll='ls -al | awk <what goes here?>. I'm having trouble with ' quotes.
    – Enan Ajmain
    Jun 28 at 3:29












    See my update for a function as an alias.
    – Nasir Riley
    Jun 28 at 4:02




    See my update for a function as an alias.
    – Nasir Riley
    Jun 28 at 4:02












    I don't see anything. Where is the update?
    – Enan Ajmain
    Jun 28 at 4:05




    I don't see anything. Where is the update?
    – Enan Ajmain
    Jun 28 at 4:05




    1




    1




    @EnanAjmain There it is. la is the alias
    – Nasir Riley
    Jun 28 at 4:05





    @EnanAjmain There it is. la is the alias
    – Nasir Riley
    Jun 28 at 4:05













    Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
    – Enan Ajmain
    Jun 28 at 4:09




    Thanks, it works. But the columns get left aligned and the file names get zigzagged. Any suggestion?
    – Enan Ajmain
    Jun 28 at 4:09












    up vote
    1
    down vote













    To remove the hard link count from the output of ls -l while still leaving the rest of the line intact:



    ls -l | sed -E '2,$s/ +[0-9]+//'


    The sed substitution will remove the first set of digits and any space before it on each line after the first line (the first line contains the total).



    As a function



    myls () sed -E '2,$s/ +[0-9]+//'



    This function invokes ls -l with any other command line arguments given by the user to the function before sending the result through sed.



    By using command ls, we ensure that, if you later decide to rename the function into ls, the function would not call itself recursively.



    Example:



    $ ls -la ~/.skel
    total 18
    drwxr-xr-x 2 kk wheel 512 May 4 16:29 .
    drwxr-xr-x 3 kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- 1 kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- 1 kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- 1 kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- 1 kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- 1 kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- 1 kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- 1 kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- 1 kk wheel 0 Apr 21 17:44 .zshrc

    $ myls -a ~/.skel
    total
    drwxr-xr-x kk wheel 512 May 4 16:29 .
    drwxr-xr-x kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- kk wheel 0 Apr 21 17:44 .zshrc



    A variation of the function that acts exactly like ls unless you use ls -l:



    ls () (
    add_filter=0
    while getopts ':l' opt; do
    case $opt in
    l) add_filter=1 ;;
    1) add_filter=0
    esac
    done

    command ls "$@" |
    if [ "$add_filter" -eq 1 ]; then
    sed -E '2,$s/ +[0-9]+//'
    else
    cat
    fi
    )


    The function does its own command line parsing to detect whether the -l option was used (and also not later disabled through the use of -1). If it was, we set a flag. Later, if the flag was set, we filter the output of ls through sed. If the flag wasn't set, we just pass the output as-is through cat.






    share|improve this answer



















    • 2




      The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
      – dave_thompson_085
      Jun 28 at 6:23











    • @dave_thompson_085 Thanks for that!
      – Kusalananda
      Jun 28 at 6:30














    up vote
    1
    down vote













    To remove the hard link count from the output of ls -l while still leaving the rest of the line intact:



    ls -l | sed -E '2,$s/ +[0-9]+//'


    The sed substitution will remove the first set of digits and any space before it on each line after the first line (the first line contains the total).



    As a function



    myls () sed -E '2,$s/ +[0-9]+//'



    This function invokes ls -l with any other command line arguments given by the user to the function before sending the result through sed.



    By using command ls, we ensure that, if you later decide to rename the function into ls, the function would not call itself recursively.



    Example:



    $ ls -la ~/.skel
    total 18
    drwxr-xr-x 2 kk wheel 512 May 4 16:29 .
    drwxr-xr-x 3 kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- 1 kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- 1 kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- 1 kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- 1 kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- 1 kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- 1 kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- 1 kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- 1 kk wheel 0 Apr 21 17:44 .zshrc

    $ myls -a ~/.skel
    total
    drwxr-xr-x kk wheel 512 May 4 16:29 .
    drwxr-xr-x kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- kk wheel 0 Apr 21 17:44 .zshrc



    A variation of the function that acts exactly like ls unless you use ls -l:



    ls () (
    add_filter=0
    while getopts ':l' opt; do
    case $opt in
    l) add_filter=1 ;;
    1) add_filter=0
    esac
    done

    command ls "$@" |
    if [ "$add_filter" -eq 1 ]; then
    sed -E '2,$s/ +[0-9]+//'
    else
    cat
    fi
    )


    The function does its own command line parsing to detect whether the -l option was used (and also not later disabled through the use of -1). If it was, we set a flag. Later, if the flag was set, we filter the output of ls through sed. If the flag wasn't set, we just pass the output as-is through cat.






    share|improve this answer



















    • 2




      The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
      – dave_thompson_085
      Jun 28 at 6:23











    • @dave_thompson_085 Thanks for that!
      – Kusalananda
      Jun 28 at 6:30












    up vote
    1
    down vote










    up vote
    1
    down vote









    To remove the hard link count from the output of ls -l while still leaving the rest of the line intact:



    ls -l | sed -E '2,$s/ +[0-9]+//'


    The sed substitution will remove the first set of digits and any space before it on each line after the first line (the first line contains the total).



    As a function



    myls () sed -E '2,$s/ +[0-9]+//'



    This function invokes ls -l with any other command line arguments given by the user to the function before sending the result through sed.



    By using command ls, we ensure that, if you later decide to rename the function into ls, the function would not call itself recursively.



    Example:



    $ ls -la ~/.skel
    total 18
    drwxr-xr-x 2 kk wheel 512 May 4 16:29 .
    drwxr-xr-x 3 kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- 1 kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- 1 kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- 1 kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- 1 kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- 1 kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- 1 kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- 1 kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- 1 kk wheel 0 Apr 21 17:44 .zshrc

    $ myls -a ~/.skel
    total
    drwxr-xr-x kk wheel 512 May 4 16:29 .
    drwxr-xr-x kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- kk wheel 0 Apr 21 17:44 .zshrc



    A variation of the function that acts exactly like ls unless you use ls -l:



    ls () (
    add_filter=0
    while getopts ':l' opt; do
    case $opt in
    l) add_filter=1 ;;
    1) add_filter=0
    esac
    done

    command ls "$@" |
    if [ "$add_filter" -eq 1 ]; then
    sed -E '2,$s/ +[0-9]+//'
    else
    cat
    fi
    )


    The function does its own command line parsing to detect whether the -l option was used (and also not later disabled through the use of -1). If it was, we set a flag. Later, if the flag was set, we filter the output of ls through sed. If the flag wasn't set, we just pass the output as-is through cat.






    share|improve this answer















    To remove the hard link count from the output of ls -l while still leaving the rest of the line intact:



    ls -l | sed -E '2,$s/ +[0-9]+//'


    The sed substitution will remove the first set of digits and any space before it on each line after the first line (the first line contains the total).



    As a function



    myls () sed -E '2,$s/ +[0-9]+//'



    This function invokes ls -l with any other command line arguments given by the user to the function before sending the result through sed.



    By using command ls, we ensure that, if you later decide to rename the function into ls, the function would not call itself recursively.



    Example:



    $ ls -la ~/.skel
    total 18
    drwxr-xr-x 2 kk wheel 512 May 4 16:29 .
    drwxr-xr-x 3 kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- 1 kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- 1 kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- 1 kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- 1 kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- 1 kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- 1 kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- 1 kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- 1 kk wheel 0 Apr 21 17:44 .zshrc

    $ myls -a ~/.skel
    total
    drwxr-xr-x kk wheel 512 May 4 16:29 .
    drwxr-xr-x kk wheel 512 Jun 28 08:15 ..
    -rw-r--r-- kk wheel 87 Nov 1 2017 .Xdefaults
    -rw-r--r-- kk wheel 771 Feb 9 10:18 .cshrc
    -rw-r--r-- kk wheel 101 Nov 1 2017 .cvsrc
    -rw-r--r-- kk wheel 359 Nov 1 2017 .login
    -rw-r--r-- kk wheel 175 Nov 1 2017 .mailrc
    -rw-r--r-- kk wheel 229 May 4 16:29 .profile
    -rw-r--r-- kk wheel 108 Apr 15 12:50 .vimrc
    -rw-r--r-- kk wheel 0 Apr 21 17:44 .zshrc



    A variation of the function that acts exactly like ls unless you use ls -l:



    ls () (
    add_filter=0
    while getopts ':l' opt; do
    case $opt in
    l) add_filter=1 ;;
    1) add_filter=0
    esac
    done

    command ls "$@" |
    if [ "$add_filter" -eq 1 ]; then
    sed -E '2,$s/ +[0-9]+//'
    else
    cat
    fi
    )


    The function does its own command line parsing to detect whether the -l option was used (and also not later disabled through the use of -1). If it was, we set a flag. Later, if the flag was set, we filter the output of ls through sed. If the flag wasn't set, we just pass the output as-is through cat.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jun 28 at 6:31


























    answered Jun 28 at 6:16









    Kusalananda

    101k13199312




    101k13199312







    • 2




      The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
      – dave_thompson_085
      Jun 28 at 6:23











    • @dave_thompson_085 Thanks for that!
      – Kusalananda
      Jun 28 at 6:30












    • 2




      The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
      – dave_thompson_085
      Jun 28 at 6:23











    • @dave_thompson_085 Thanks for that!
      – Kusalananda
      Jun 28 at 6:30







    2




    2




    The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
    – dave_thompson_085
    Jun 28 at 6:23





    The linkcount can be sometimes more than one digit, so you need s/ +[0-9]+// plus this clobbers the number in the total line so better 2,$s...
    – dave_thompson_085
    Jun 28 at 6:23













    @dave_thompson_085 Thanks for that!
    – Kusalananda
    Jun 28 at 6:30




    @dave_thompson_085 Thanks for that!
    – Kusalananda
    Jun 28 at 6:30










    up vote
    0
    down vote













    With ast-open's ls (also the builtin ls of ksh93 if it has been included and /opt/ast/bin is ahead of /bin in $PATH), you can fully specify the output format and omit the number of links (%3(nlink)u) there:



    ls -rSZ '%(mode)s %-8(uid)s %-8(gid)s %8(device:case::%(size)u:*:%(device)s)s %(atime)s %(name)s%(linkop:case:?*: %(linkop)s %(linkpath)s)s'





    share|improve this answer

























      up vote
      0
      down vote













      With ast-open's ls (also the builtin ls of ksh93 if it has been included and /opt/ast/bin is ahead of /bin in $PATH), you can fully specify the output format and omit the number of links (%3(nlink)u) there:



      ls -rSZ '%(mode)s %-8(uid)s %-8(gid)s %8(device:case::%(size)u:*:%(device)s)s %(atime)s %(name)s%(linkop:case:?*: %(linkop)s %(linkpath)s)s'





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        With ast-open's ls (also the builtin ls of ksh93 if it has been included and /opt/ast/bin is ahead of /bin in $PATH), you can fully specify the output format and omit the number of links (%3(nlink)u) there:



        ls -rSZ '%(mode)s %-8(uid)s %-8(gid)s %8(device:case::%(size)u:*:%(device)s)s %(atime)s %(name)s%(linkop:case:?*: %(linkop)s %(linkpath)s)s'





        share|improve this answer













        With ast-open's ls (also the builtin ls of ksh93 if it has been included and /opt/ast/bin is ahead of /bin in $PATH), you can fully specify the output format and omit the number of links (%3(nlink)u) there:



        ls -rSZ '%(mode)s %-8(uid)s %-8(gid)s %8(device:case::%(size)u:*:%(device)s)s %(atime)s %(name)s%(linkop:case:?*: %(linkop)s %(linkpath)s)s'






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 28 at 6:34









        Stéphane Chazelas

        278k52513844




        278k52513844






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452339%2fremove-the-number-of-hard-links-from-the-output-of-ls-al-command%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?