LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2012, 03:13 AM   #1
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Rep: Reputation: 1
Lightbulb Find various files from a text file


Hey all,

I'd like to use a command which would find various files, under a specific directory, while these file names I'm looking for - are taken from a text file.

How can I do that using a simple command?


Thank you.
 
Old 03-08-2012, 05:36 AM   #2
elmo219
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: Disabled
Do your various files have anything in common like being named file.2

For instance if i wanted to find all files in home dir called .common i'd run;

Code:
find /export/home/user -name "*.common"

Last edited by elmo219; 03-08-2012 at 05:38 AM.
 
Old 03-08-2012, 05:49 AM   #3
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
Thanks for your reply.

Thing is, these file names most be taken from a text file rather than I write it.
 
Old 03-08-2012, 06:20 AM   #4
elmo219
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: Disabled
will these files still be being written to? if so can you not list them via PID?
 
Old 03-08-2012, 06:26 AM   #5
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
I cannot list those file via PID since they will be written on a text file.

let me make myself more clear

lets say I have a file called - fnames.txt. this file contains many names inside -

What I'd like to do is to search for each one of these names under a specific directory.
 
Old 03-08-2012, 06:39 AM   #6
elmo219
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: Disabled
sorry if i'm still missing the mark but then would this not work?

Code:
 grep -rn name /dir/

Last edited by elmo219; 03-08-2012 at 06:48 AM.
 
Old 03-08-2012, 07:32 AM   #7
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
Nope.

Can anyone else help here?


Thanks.
 
Old 03-08-2012, 07:54 AM   #8
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371
Try this, although there may be problems if the filenames contain spaces.
Code:
while read FILE; do find <specific directory> -name $FILE -print; done < fnames.txt
 
Old 03-08-2012, 08:22 AM   #9
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
To me your question is not clear enough - you would benefit from an example. Do you mean something like this:

namesfile contains
user.txt
bash.log

Then you want a command that will list all files whose names match "user.txt" or "bash.log"? But do you want exact filenames only, or do you want shell globbing characters like user*.txt? Or do you want full regular expression matching like tty[1..9]{1,3}.log?
 
1 members found this post helpful.
Old 03-08-2012, 09:06 AM   #10
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
Thank you very much allend. it works perfectly.

---------- Post added 03-08-12 at 09:07 AM ----------

and thanks for the others who has tried helping as well
 
1 members found this post helpful.
Old 03-09-2012, 03:21 AM   #11
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
1 more thing, how could I exclude a directory from the search?
 
Old 03-09-2012, 03:27 AM   #12
elmo219
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: Disabled
i believe
Code:
while read FILE; do find <specific directory> -type d \( ! -name tmp \) -name $FILE -print; done < fnames.txt
Should search all but /tmp

Sorry for not understanding at first..
 
Old 03-11-2012, 06:06 AM   #13
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
thanks elmo, no need to sorry - it's all ok

about your suggestion - it doesn't seem to work. I just get a blank results.

Code:
[root@n-bw ~]# while read FILE; do find ~ -type d \( ! -name tmp \) -name $FILE -print; done < fnames.txt
[root@n-bw ~]#
Code:
[root@n-bw ~]# ls -lR
.:
total 80
-rw-------. 1 root root  1557 Mar  7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root    52 Mar  8 18:02 fnames.txt
-rw-r--r--. 1 root root     0 Mar  8 18:04 hello
-rw-r--r--. 1 root root 52502 Mar  7 18:06 install.log
-rw-r--r--. 1 root root 11587 Mar  7 18:01 install.log.syslog
-rw-r--r--. 1 root root     0 Mar  8 18:04 nir
drwxr-xr-x  2 root root  4096 Mar 11 14:02 tmp

./tmp:
total 0
-rw-r--r-- 1 root root 0 Mar 11 14:05 hello
 
Old 03-11-2012, 10:16 AM   #14
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371
Code:
while read FILE; do find <specific directory> -path <exclude directory> -prune -o -name $FILE -print; done < fnames.txt
Hopefully pruning the directory to be excluded will work. I have not tested this.
 
Old 03-12-2012, 03:52 AM   #15
niiiro
Member
 
Registered: Feb 2010
Posts: 65

Original Poster
Rep: Reputation: 1
Thanks for your reply but unfortunately it didn't do the job -

[root@n-bw ~]# while read FILE; do find ~ -path ~/tmp/ -prune -o -name $FILE -print; done < fnames.txt
/root/hello
/root/tmp/hello
/root/nir
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find Text After String Search in Text File redir Linux - Newbie 12 08-02-2011 04:57 PM
[SOLVED] read a text file distrubute its contents on different text files magische_vogel Programming 13 02-26-2011 07:51 PM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-08-2007 12:24 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
linux 'find' to find text files subu_s Programming 2 12-02-2004 03:56 AM


All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration