A perl script to list files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A perl script to list files
# 1  
Old 03-05-2011
A perl script to list files

Hi guys,

I'm learning grep and map functions in perl and ran into a problem.
I have a little script below:

PHP Code:
#!/usr/bin/perl -w

die "Usage: $0 dir\n" if @ARGV != 1;
opendir (D$ARGV[0]) or die $!;

@
text_files grep { !-readdir D;
rewinddir D;
@
sizes sort {$b <=> $amap { -"$ARGV[0]/$_grep { !-readdir D
What I want is to print out in all non-directory files and their sizes in descending order of sizes. Although I got the files and their sizes above, I'm wondering if there's any other way of doing it. Also, I can't figure out yet how to print it out in nicely aligned format, like this:

a.pl :209
b.pl :345

Any suggestions are greatly appreciated,

new bie,

Last edited by new bie; 03-06-2011 at 09:09 PM..
# 2  
Old 03-06-2011
This will skip hidden files:

Code:
#!/usr/bin/perl 

use warnings;
use strict;

die "Usage $0 <dirname>\n" 
  unless @ARGV;
  
my @files;  

-d or push @files, [ -s, $_ ]  
  for glob "$ARGV[0]/*";
  
print map 
  "$_->[1] $_->[0]\n", 
     sort { 
      $b->[0] <=> $a->[0] 
      } @files;

This User Gave Thanks to radoulov For This Post:
# 3  
Old 03-06-2011
Thanks for replying radoudov. I typed in your code but i got this error:
"Substitution replacement not terminated at grep.pl line 24" which is the print map line.

However, I was able to do it like this:
Code:
@text_files = map {sprintf("%-10s :%d", $_, -s "$ARGV[0]/$_")} grep {!-d} readdir D;

The above will print all text files and their sizes, but in random order. Is there any ways that I can sort the sizes?

Thanks in advance,

new bie,

Last edited by new bie; 03-06-2011 at 09:27 PM..
# 4  
Old 03-07-2011
Quote:
Originally Posted by new bie
Thanks for replying radoudov. I typed in your code but i got this error:
"Substitution replacement not terminated at grep.pl line 24" which is the print map line.[,
Radoudov's code works for me as is.
# 5  
Old 03-07-2011
Quote:
Originally Posted by new bie
Thanks for replying radoudov. I typed in your code but i got this error:
"Substitution replacement not terminated at grep.pl line 24" which is the print map line.
[...]
Could you post the exact code you're executing? The entire script.
# 6  
Old 03-07-2011
Hi radoulov,

Here's what I typed in:

Code:
#!/usr/bin/perl -w

die "Usage $0 directory\n" if @ARGV != 1;
opendir (D,$ARGV[0]) or die $!;
  
my @files;  
-d or push @files, [ -s, $_ ]  
  for glob "$ARGV[0]/*";
  
print map 
  "$_->[1] $_->[0]\n", 
     sort { 
      $b->[0] <=> $a->[0] 
      } @files;

And I got the message "Substitution replacement ....".

I was able to figure out my original question by doing this:
Code:
#!/usr/bin/perl -w
die "Usage: $0 directory\n" if @ARGV != 1;
opendir (D,$ARGV[0]) or die $!;

%text = map {$_, -s "$ARGV[0]/$_"} grep {!-d} readdir D;
printf "%-15s :%d\n", $_, $text{$_} foreach (sort{$text{$b} <=> $text{$a}} keys %text);

And this works. Maybe I made typo errors when type in your code. I'll check it out again.

Thank you,

new bie,

Last edited by new bie; 03-08-2011 at 02:28 PM..
# 7  
Old 03-08-2011
The only output I get on stderr with the code you posted is:

Code:
Name "main::D" used only once: possible typo at s line 4.

And it's because you tried to merge my script with yours (my code was sufficient).

But given that you already have a working version, we could consider the problem is solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List Files script

Hello everyone - I have the task to create a file list script that will list files in directory based on the parameters passed to the program. It has to be a C Shell - I mentioned that before but I got closed :) - the company only allows this shell for security purposes I guess. Anyway, here is... (4 Replies)
Discussion started by: adrianvas12
4 Replies

2. Shell Programming and Scripting

Using Shell Script in place of Perl script to Unzip the zip files.

Hi Expert, We have some shell scripts which Internally uses Perl Script to Unzip the source zip files which comes to inbound directory. So now our requirement is to avoid the dependency on Perl Script and us Shell Script to unzip the files. I have the Perl script with me attached can some one... (3 Replies)
Discussion started by: naveen.dasu
3 Replies

3. Shell Programming and Scripting

Perl script to verify that a value is present in an array (list)

I have 2 files , i need compare both files field by field, and in the fourth field some value will be interchaged and some value will be **. ex: file1 john|0.0|4|**:25;JP:50;UY:25 file2 john|0.0|4|JP:50;**:25;UY:25 (4 Replies)
Discussion started by: veeruasu
4 Replies

4. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

5. Shell Programming and Scripting

How to get list of user into an array in perl script

Hi, cut -d: -f1,3 /etc/group >rpt.out I have a doubt in perl. right i am getting list of group user id into rpt.out file. instead i need to store it as an array in perl script. could you please tell me how can i get list of user into an array in perl script.. thanks in advance. (1 Reply)
Discussion started by: solo123
1 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

perl script for listing files and mailing the all files

Hi, I am new to perl: I need to write perl script to list all the files present in directory and mail should be come to my inbox with all the files present in that directory. advanced thanks for valuable inputs. Thanks Prakash GR (1 Reply)
Discussion started by: prakash.gr
1 Replies

8. Shell Programming and Scripting

I need a script to find socials in files and output a list of those files

I am trying to find socail security numbers in files in (and under) a specific directory and output a list of the files where they are found... the format would be with no dashes just 9 numeric characters in a row. I have tried this: find /DirToLookIn -exec grep '\{9\}' /dev/null {} \; >>... (1 Reply)
Discussion started by: NewSolarisAdmin
1 Replies

9. Shell Programming and Scripting

Perl script to move files not in use

I need to write a script to move files only when they are not in use. I have a rudementry bash script for Linux but i need a perl script so it will work on Linux and hpux. Oracle writes files to a directory called /data and the files there are moved every 5 minutes to a new home. But i need to... (1 Reply)
Discussion started by: insania
1 Replies

10. Shell Programming and Scripting

perl script to list filenames that do not contain given string

Hi, I need to write a perl script that should do a search recursively in all the '*.txt' files for the string "ALL -Tcb" and should print only the file names that do not contain this string. (21 Replies)
Discussion started by: royalibrahim
21 Replies
Login or Register to Ask a Question