Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do i list all of the gz files in a directory and their sizes

Posted on 2014-01-27
8
Medium Priority
?
132 Views
Last Modified: 2014-02-01
Hi

I think the question title says it all. I have tried ls -l -h  **/*.gz to just list the gz files in a directory but i get the error message 'cannot access **/*.gz no such file or directory

Thanks a lot
0
Comment
Question by:andieje
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 39813233
What directory are the files in?
Did you try  ls -l -h  *.gz
0
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 39813322
Are you looking in a specific subdirectory? If so, include the directory name, like this:
ls -l -h  sub_dir_name/*.gz

Open in new window

Or do you want to find all *.gz that are 1 subdirectory below the current directory? If so, you need to use find, and tell it to only look 1 level deep, like this:
find -maxdepth 2 -name "*.gz"

Open in new window

Note that you need to use 2 because the current directory is considered the first, and its subdirs are 2, etc.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39813380
if you want to list all files in a particular directory, try

ls -ltr *.gz

but that won't include gz files from subdirectories
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:Surrano
ID: 39814350
To find recursively and list the size as well:
find . -name "*.gz" -ls

Open in new window

0
 

Author Comment

by:andieje
ID: 39814913
Hi
Thanks for the replies. I can see that as ever my question isn't worded as well as it could have been. I just want the file name and the size in human readable form (like MB or something). However in order to do this I seem to have to add the -l option which give me a load of other gunk. Or do i need to pipe it into cut to get teh columns I want. I dont use linux very mich. Sorry
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39814930
Assuming there are no whitespaces in your file names the following will work.

find . -name "*.gz" -ls | awk '{size=$7; name=$11; suf=""; format="8d  ";
if (size>1024) { size /= 1024.0; suf="k"; format="10.1f" }
if (size>1024) { size /= 1024.0; suf="m" }
if (size>1024) { size /= 1024.0; suf="g" }
printf "%"format"%-2s %s\n", size, suf, name; 
}'

Open in new window

0
 
LVL 8

Accepted Solution

by:
Surrano earned 2000 total points
ID: 39814942
or utilising the "-h" flag of "ls" command (which won't align the numbers decimally):

 find . -name "*.gz" | xargs ls -lh | awk '{ printf "%10s %s\n", $5,$9}'

Open in new window

0
 

Author Closing Comment

by:andieje
ID: 39826871
brilliant :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question