Solved

Finding Files using shell/perl

Posted on 2012-03-26
10
788 Views
Last Modified: 2012-11-19
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,
0
Comment
Question by:new_perl_user
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 37768266
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

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37768308
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

0
 

Author Comment

by:new_perl_user
ID: 37768357
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.
0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37768444
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.
0
 

Author Comment

by:new_perl_user
ID: 37768593
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".
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.

 
LVL 13

Accepted Solution

by:
Carl Bohman earned 500 total points
ID: 37768845
Untested, but something like this should work:
perl -MFile::Find -MFile::Copy -e 'find(sub{/^[^_]+\.txt$/ && move($File::Find::name, "/usr/HOME/DATA");}, @ARGV)' /dir1 /dir2 /sub/dir3

Open in new window

Note that you need to make sure that the directory /usr/HOME/DATA exists before running this command or you won't get the results you're looking for.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37768958
or easier to do

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

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37771768
@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.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37774145
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

0
 

Expert Comment

by:ashsysad
ID: 38614248
Good one.  I just came to know that we can use RegEx with find command.

Thanks
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

Suggested Solutions

Title # Comments Views Activity
improve Shell script 5 142
use shell script to parse json returned by a restful service 6 97
Best way to split and output to csv in bash 2 60
Linux Scripting 3 97
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 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

20 Experts available now in Live!

Get 1:1 Help Now