LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need to find if a matching file exist from a list of possible file names (http://www.linuxquestions.org/questions/linux-general-1/need-to-find-if-a-matching-file-exist-from-a-list-of-possible-file-names-594210/)

wit_273 10-24-2007 09:01 AM

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.

George

larkl 10-24-2007 06:38 PM

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,

wolfperkins 10-25-2007 08:39 AM

You could do it with something like this:

Code:

PARENT_FOLDER=/somewhere
CSV_FILE_PATH=/find/csv/file/here
FOLDER_NAME_COLUMN=1
REPORT_FILE=/path/to/your/report/file

#Always start with a fresh report file
[ -f $REPORT_FILE ] && mv -f $REPORT_FILE $REPORT_FILE.old

cd $PARENT_FOLDER
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
      fi
done


wit_273 10-25-2007 09:15 AM

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.

wit_273 10-25-2007 09:25 AM

Thanks wolfperkins, I tried that with a smaller sample and it worked perfectly.

jschiwal 10-25-2007 09:47 AM

You might want to post a couple of lines from your csv file. For example, is it of the form
"item1","item2","item3"
or
item1,item2,item3
or
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".


All times are GMT -5. The time now is 05:15 PM.