Solved

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

Posted on 2014-01-27
8
117 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 84

Expert Comment

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

Expert Comment

by:ThomasMcA2
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

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

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:andieje
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
brilliant :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now