Link to home
Start Free TrialLog in
Avatar of new_perl_user
new_perl_user

asked on

Finding Files using shell/perl

Hi,

 can some one please let me know how to find .text files by recursively reading folders and sub folders from a path.

Issue here is , in that folders I have two type of txt files. For example (`7249184.txt',  '7419841_0001.txt'), but I need only to find .txt  

Thanks,
Avatar of Tintin
Tintin

Issue here is , in that folders I have two type of txt files. For example (`7249184.txt',  '7419841_0001.txt'), but I need only to find .txt  

That sentence doesn't make sense.  Both your examples are .txt files.

To find .txt files, do

find /some/path -type f -name "*.txt"

Open in new window

Find all .txt files in the current directory:
perl -MFile::Find -e 'find(sub{/\.txt$/ && print $_,"\n";}, @ARGV)' .

Open in new window

Modifying the regex allows you to search for any other files by name.  In your case, I think this regex may do what you need:
perl -MFile::Find -e 'find(sub{/^[^_]+\.txt$/ && print $_,"\n";}, @ARGV)' .

Open in new window

Avatar of new_perl_user

ASKER

Hi Bounsy,

I tried to run the perl -MFile::Find -e 'find(sub{/^[^_]+\.txt$/ && print $_,"\n";}, @ARGV)'  from command line and it throwed out an error.


invalid top directory at /usr/lib/perl5/5.8.8/File/Find.pm line 592.
The command included a period at the end.  This was to tell it to search starting from the current directory.  If you want to be more explicit, just list the directories you want to search.
perl -MFile::Find -e 'find(sub{/^[^_]+\.txt$/ && print "$File::Find::name\n";}, @ARGV)' /dir1 /dir2 /sub/dir3

Open in new window

Also note that I fixed the print statement in the above command, since the original version didn't show the path to the file, just the file name.
Hi,

Thank you so much it worked. If possible can you please help me to extend the above command.

After finding the file  can we move that file to a location "/usr/HOME/DATA".
ASKER CERTIFIED SOLUTION
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or easier to do

find .  -type f -name "*.txt" | xargs -i mv {} /usr/HOME/DATA

Open in new window

@Tintin: That does work for the simple case of all .txt files, but not for the case that new_perl_user asked for which is for only some .txt files.  You would need to make the -name option more complicated or add an addiitonal grep command (likely using a regex) in order to only get the files that new_perl_user was interested in.  My solution is obviously more complicated (not necessarily a good thing), but has the advantage of being able to accept any arbitrarily-complex regex for the file name.  In general, I definitely agree that simple is better and prefer simple solutions when they are capable of handling the requirements.
Ah, so you successfully managed to interpret that when new_perl_user said

For example (`7249184.txt',  '7419841_0001.txt'), but I need only to find .txt  

they really meant:

I want to match numeric .txt files only, ie: no underscores.

In that case, a regex is the way to go.

With GNU find, you can do:

find . -type f -regex ".*/[0-9]+.txt"

Open in new window

Good one.  I just came to know that we can use RegEx with find command.

Thanks