0

I need to list some files in a directory using the ls command, but I need to also to hide the file sizes. How can I do that with the ls command?

1
  • 5
    ls command in it's own does not show file size at all. Please be more specific and explain what you want to achieve. Nov 8, 2013 at 19:53

2 Answers 2

2

You could mask it out using awk:

$ ls -l | awk '{print $1, $2, $3, $4, $6, $7, $8, $9}'

Example

$ ls -l|awk '{print $1, $2, $3, $4, $6, $7, $8, $9}' | head -5
total 172136      
drwxrwxr-x 2 saml saml Jan 16 2013 desktop-integration
-rw-r--r-- 1 saml saml Jan 16 2013 libobasis3.6-base-3.6.5.2-2.x86_64.rpm
-rw-r--r-- 1 saml saml Jan 16 2013 libobasis3.6-binfilter-3.6.5.2-2.x86_64.rpm
-rw-r--r-- 1 saml saml Jan 16 2013 libobasis3.6-calc-3.6.5.2-2.x86_64.rpm

Details

The awk command automatically will parse the data into columns, this command is telling awk to print all the columns except for the 5th, which is the column with the size information in it.

NOTE: I wouldn't recommend doing it this way however, I show you the approach only to show you the general concept of how you can parse the output from one command in Unix by piping to another tool such as awk. The next method is the preferred way of solving your particular example!

More concise awk example

Thanks to @val0x00ff in the comments there is an even more efficient method to instruct awk to print all the columns except the 5th.

Example

$ ls -l|awk '{$5=""; print}' | head -5
total 172136   
drwxrwxr-x 2 saml saml  Jan 16 2013 desktop-integration
-rw-r--r-- 1 saml saml  Jan 16 2013 libobasis3.6-base-3.6.5.2-2.x86_64.rpm
-rw-r--r-- 1 saml saml  Jan 16 2013 libobasis3.6-binfilter-3.6.5.2-2.x86_64.rpm
-rw-r--r-- 1 saml saml  Jan 16 2013 libobasis3.6-calc-3.6.5.2-2.x86_64.rpm

Details

In this method we're instructing awk to blank out the 5th column by setting it to an empty string, "". Everything else is left as is. We then tell awk to print the resulting string of what's left.

8
  • 1
    I think a more efficient way would be ls -l | awk '{ $5=""; print}'. I did not post an answer since I have no clue what the OP wants. Nov 8, 2013 at 20:06
  • 1
    @val0x00ff - post that as an answer, I believe that is what he wants. I can think of no other interpretation of his question beyond that.
    – slm
    Nov 8, 2013 at 20:07
  • I'm just saying maybe it would be more efficient. You could just edit your answer. Nov 8, 2013 at 20:08
  • 1
    I don't mind at all. I appreciate your contribution and you help a lot of folks including me with your nice and well explained answers. Cheers! Nov 8, 2013 at 20:10
  • 1
    @val0x00ff - thank you for your guidance, and kind words!
    – slm
    Nov 8, 2013 at 20:12
1

From your question I assume that your call to ls is an alias which includes the -l option. So one way would be to use ls without the -l option as indicated above, e.g. by calling:

echo "call $(which ls)"
$(which ls)

Another possibility is that you actually want to use the -l option, but don't want to have a different output every time the file-sizes change, in this case and only if you have no files above 1TB in size you could use the following command which hides the true file size by reporting 1T for all files and avoids using pipes and other commands:

ls -l --block-size=T

Of course if you don't mind using a pipe and want to use awk then please use the solution as posted by @slm thanks to @val0x00ff:

ls -l | awk '{$5=""; print}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .