Solved

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

Posted on 2014-01-27
8
118 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Utilizing an array to gracefully append to a list of EmailAddresses
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

21 Experts available now in Live!

Get 1:1 Help Now