Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-24-2007, 10:01 AM   #1
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82

Rep: Reputation: 15
Need to find if a matching file exist from a list of possible file names

I am sure this questions has been asked before but I can not seem to find it here. I would appreciate if someone can help or point me to a thread that has already answered this.

I am guessing this can be done with a simple bash script, but I am not a scripter so help would be appreciated. I have a list of about 6500 folder names that I need to see if they still exist on the system. The folders will all be under the same parent directory. Currently I have the list in a csv file but the format can be changed. If matches are found I need the folder name outputted to a text file so I can manually check weather they are still being used.

If anyone can help me with this or point me in the right direction I would appreciate it.

Old 10-24-2007, 07:38 PM   #2
Registered: Sep 2007
Distribution: Puppy 5.2.8
Posts: 69

Rep: Reputation: 15
Boy, I sure hope this isn't your homework. You should be able to use ls piped to grep to do this, something like >ls -d |grep whattofind,
Old 10-25-2007, 09:39 AM   #3
Registered: Oct 2007
Location: Val-des-Monts, Québec, Canada
Distribution: CentOS, RHEL, Fedora
Posts: 110

Rep: Reputation: 16
You could do it with something like this:


#Always start with a fresh report file

cat $CSV_FILE_PATH | \
   cut -d, -f$FOLDER_NAME_COLUMN | \
   while read FOLDER_NAME; do
      if [ -d "$FOLDER_NAME" ]; then
         echo "$FOLDER_NAME" >> $REPORT_FILE
Old 10-25-2007, 10:15 AM   #4
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: 15
No this is no homework assignment.

This may be something that should be obvious-- but I don't know the answer and can not find it in the ls man page. How do I get it to search for folders that match the data in a csv file (or any other file)? In other words how do I input data from a file into ls to search for?

EDIT, I had already started to reply and got distracted before I submitted. When I submitted I noticed wolfperkins had another response. I will try that.

Last edited by wit_273; 10-25-2007 at 10:17 AM.
Old 10-25-2007, 10:25 AM   #5
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: 15
Thanks wolfperkins, I tried that with a smaller sample and it worked perfectly.
Old 10-25-2007, 10:47 AM   #6
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You might want to post a couple of lines from your csv file. For example, is it of the form
item1,"item with comma, in name",item3.

You can get a list of the directories under the parent directory using the find command:
find /path/to/parent/directory -type f

Given the large number of entries, it may work best if you can process the csv file and produce a file of directories. (Let's call it csvdirs for illustration sake.)
Then you could use:
grep -f csv-dirs current-dir-list
to find matches. A better way would be to sort both lists and use the "comm" command.
comm compares to sorted lists and outputs three columns.
First column: lines unique to file1; Second column: lines unique to file2; Third column: lines common to both files. You can turn off any of the columns with the -1, -2, and -3 options.
To list common entries, use "comm -12 file1 file2".


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
find command and file names with whitespace sir_woland Linux - General 13 06-04-2018 04:53 PM
tough one: how do you find patterns/sequences in file names? BrianK Programming 5 10-18-2007 01:33 PM
Find and special file names CritterZ Linux - Newbie 2 12-23-2004 10:49 PM
SSL Install not going well. Linux server, Apache directions? File names not matching. maineweb Linux - Newbie 1 11-18-2004 10:18 AM
Couldn't find matching file system: LABEL=/user1 kausik Linux - General 4 05-20-2002 07:00 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:29 PM.

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