Operating System - HP-UX
1833863 Members
2013 Online
110063 Solutions
New Discussion

list file name and size only

 
SOLVED
Go to solution
Youlette Etienne_2
Regular Advisor

list file name and size only

Hello everyone,

How do I list only the file name and size within a directory? The "ll|cut fnum,num d " " does not suppress the blank spaces between the fields so I get weird results. I need to feed this information into an array in a Perl program, but Perl is getting confused somewhere around the "print" word with using "awk" in the below.

@array1=system("ll /tmp|awk '{print $5,$9}'");
If at first you don't succeed, change the rules!
16 REPLIES 16
Jeff Schussele
Honored Contributor

Re: list file name and size only

Hi,

Simply use ls -s

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

I need the actual size in kilobytes, not the block size.
If at first you don't succeed, change the rules!
Dave Hutton
Honored Contributor

Re: list file name and size only

If you just want 1 long line you can do:
ls -1s
Dave Olker
Neighborhood Moderator

Re: list file name and size only

How about this:

# ll | awk '{print $5, $NF}'

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

ls -1s gives me the block size. I need the size in kilobytes.

As I mentioned, Perl gets confused when calling awk from the "system" command. This does not work.
If at first you don't succeed, change the rules!
Dave Olker
Neighborhood Moderator

Re: list file name and size only

Can you use the du command?

# du -k /tmp

See if that gives you what you want.

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

No, that's not what I am looking for. Let me explain. We have SeeBeyond's Egate server configured. The egate user picks up files from our ftp server. Problem is that the egate user picks up the files before the ftp is complete. The only way to restrict this without involving our clients is by file permissions and to write a program to check the file sizes. That is, get a list of files and their sizes in an array, after about a minute or so, list the files in a second array, and compare the sizes for each file. If the size has not increased, then the ftp is complete, change the permissions to allow the egate user to pick up the file.

This will not work if the size is in blocks. Or any ideas on how to do this would be most appreciated also. Thanks
If at first you don't succeed, change the rules!
Dave Olker
Neighborhood Moderator

Re: list file name and size only

Why wouldn't "du -k /tmp" work? It returns the list of files and their sizes in kbytes.

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

k460: /tmp =>date>test1
k460: /tmp =>ll test1
-rw-r--r-- 1 root sys 29 Aug 12 17:13 test1
du -k * /tmp
1 test1

k460: /tmp =>date>>test1
k460: /tmp =>ll test1
-rw-r--r-- 1 root sys 58 Aug 12 17:11 test1
du -k * /tmp
1 test1

As you can see, the file size has increased but the "du" size did not. This is why it will not work
If at first you don't succeed, change the rules!
Dave Olker
Neighborhood Moderator

Re: list file name and size only

Ok, it sounds like you're really looking for the output in *bytes* not *kilobytes*. In your example the target file was less than 1 KB both before and after your commands, which is why du reported 1 for the number of kbytes.


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

so, any ideas??
If at first you don't succeed, change the rules!
Dave Olker
Neighborhood Moderator
Solution

Re: list file name and size only

Try this one:

system("ll /tmp | awk '{print \$5, \$9}'")

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

You are a genius!!! That worked! Thank you so much! 10 points coming right up!
If at first you don't succeed, change the rules!
Jeff_Traigle
Honored Contributor

Re: list file name and size only

I came home and cracked the good ole "Programming Perl" book from O'Reilly... as an alternative to making a system call that runs a couple of OS commands, is there a reason you wouldn't want to use the readdir and stat functions within perl to get this information? Obviously there's a little more logic to write that way, but I imagine it would be a bit more efficient to run.
--
Jeff Traigle
Duncan Galbraith
Frequent Advisor

Re: list file name and size only

I agree with Jeff. system calls are best avoided for efficiency and simplicity.
Perl's stat function could be used but "-s" in Perl gives you what you want anyway.

I've over complicated this slightly to populate array1 exactly as you had it (which I guess is not really what you want as I imagine you loop through all the files in it later anyway).

my @array1;
my $file;
my $i = 0;
my @files = glob ("/tmp/*");

foreach $file (@files)
{
@array1[$i++] = -s $file;
@array1[$i++] = $file;
}
Youlette Etienne_2
Regular Advisor

Re: list file name and size only

I will try this method as well to see how it works. I have time to work with this. 10 points for taking the time to research. Thanks
If at first you don't succeed, change the rules!