Perl Directory List Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Directory List Help
# 1  
Old 03-28-2012
Question Perl Directory List Help

I am currently trying to find all files with extensions .gif .jpg .png .wav and .au in a current directory and count them.
I am trying to count them based on there extension only.

Code:
#!/usr/bin/perl
$gif =`ls *.gif | uniq -cf 1`;
$jpg =`ls *.jpg | uniq -cf 1`;
$png =`ls *.png | uniq -cf 1`;
$wav =`ls *.wav | uniq -cf 1`;
$au  =`ls *.au  | uniq -cf 1`;
print "$gif $jpg $png $wav $au";

This is what I currently got and it is not doing what I want it to at all. It locates all the image files but doesnt print just there extension. I was thinking I needed to use AWK but my AWK skills suck. Can anyone help me or guide me in the right direction? I'd really appreciate it.

Last edited by Franklin52; 03-29-2012 at 07:03 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-28-2012
Quote:
Originally Posted by Temphas
I am currently trying to find all files with extensions .gif .jpg .png .wav and .au in a current directory and count them.
I am trying to count them based on there extension only.
#!/usr/bin/perl
$gif =`ls *.gif | uniq -cf 1`;
$jpg =`ls *.jpg | uniq -cf 1`;
$png =`ls *.png | uniq -cf 1`;
$wav =`ls *.wav | uniq -cf 1`;
$au =`ls *.au | uniq -cf 1`;
print "$gif $jpg $png $wav $au";

This is what I currently got and it is not doing what I want it to at all. It locates all the image files but doesnt print just there extension....
Code:
$
$ ls -1 *.gif *.jpg *.png *.wav *.au
file1.au
file1.gif
file1.jpg
file1.png
file1.wav
file2.au
file2.gif
file2.jpg
file2.png
file3.gif
file3.png
file4.png
$
$
$ perl -le 'for (glob "*.gif *.jpg *.png *.wav *.au"){s/^.*\.//; $x{$_}++} END {while (($k, $v) = each %x){print "$k => $v"}}'
au => 2
png => 4
jpg => 2
gif => 3
wav => 1
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 03-29-2012
Thanks Tyler this is exactly what I wanted I didn't even need to use AWK.
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 with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

How to go to up directory from a given directory in Perl?

I have a Perl script that has a variable for a directory path and I want to define another variable that will navigate to up directory from the given directory path. This is the given directory path in Perl and this is the way the directory path is derived use FindBin; $apphome =... (0 Replies)
Discussion started by: cumeh1624
0 Replies

3. Programming

Perl - How to empty a directory?

Hi Guys, i'm writing a perl script which whenever runs, should empty 3 pre-decided directories as first step and then the script has the logic to parse some other directories and copy the files inside those directories into these 3 directories. I've the logic already developed, just need to... (2 Replies)
Discussion started by: jhamaks
2 Replies

4. Shell Programming and Scripting

Copy list of files from a keyword list to another directory

Hello, I have a folder with a massive amount of files, and I want to copy out a specific subset of the files to a new directory. I would like to use a text file with the filenames listed, but can't get it to work. The thing I'm hung up on is that the folder names in the path can and do have... (5 Replies)
Discussion started by: twjolson
5 Replies

5. Shell Programming and Scripting

Directory Search Perl

@scriptfiles=glob('*.txt'); foreach $file (glob('*.txt')) { open(my $fh, $file) or die("Unable to open '$file': $!"); while (my $line = <$fh>) { if ($line =~ m/(Apple|Orange|Guava)/i) { $Sheet->Cells($row,$col-1)->{'Value'} = $file; ... (1 Reply)
Discussion started by: rajkrishna89
1 Replies

6. Shell Programming and Scripting

FTP from one directory to another using perl

Hi All I am stuck with a problem and i want your help, I have two directories dir1 and dir2 The files present in dir1 is a1,a2 a3 a4 What i want to is to FTP the files present in the dir1 to dir2 (with .txt extension at the end.) with the help of the Perl. The output expected is The... (12 Replies)
Discussion started by: parthmittal2007
12 Replies

7. Shell Programming and Scripting

Change Directory in Perl

Hi Can any one please support: From Windows, I am running perl script located in C:/scripts directory and need to run a command "ls | sort | uniq" on the files in D:/temp directory. #!c:\perl\bin\perl.exe $file_dir="D:\\temp"; $command1="cd"; $command2="ls | sort | uniq";... (2 Replies)
Discussion started by: sureshcisco
2 Replies

8. Shell Programming and Scripting

Convert perl qw list to text file list?

Does anyone have a good example? I am having trouble looping.. Thanks (1 Reply)
Discussion started by: mrlayance
1 Replies

9. Shell Programming and Scripting

find list of files from a list and copy to a directory

I will be very grateful if someone can help me with bash shell script that does the following: I have a list of filenames: A01_155716 A05_155780 A07_155812 A09_155844 A11_155876 that are kept in different sub directories within my current directory. I want to find these files and copy... (3 Replies)
Discussion started by: manishabh
3 Replies

10. UNIX for Dummies Questions & Answers

Directory list inside a directory

Hi Bosses! I have a directory name sih. that directory contains some more directories and some files. i just want to list (ls) the directories under this directory. What will be the command.I am using debian linux. Thanks bosses.will appreciate your help. sih (6 Replies)
Discussion started by: little_jhon
6 Replies
Login or Register to Ask a Question