LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to search multiple directories for multiple files? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-search-multiple-directories-for-multiple-files-4175451770/)

Batistuta_g_2000 02-26-2013 10:10 AM

How to search multiple directories for multiple files?
 
Ok, so am newbie obviously, have to make a script to have a user enter one or more filenames and one or more .tar filenames - I need for the user to be able to enter as many filenames and as many tar files as they want - I then need the script to display the filename and the matching .tar which contains it.

Stumped!


Came across below,
find . -name "*.tgz" -exec tar tzf {} \; > list.txt

# copy all contents of all tar files to list???
But this only give the file names not versions or associated archive_filenames.

Is it something like below of a complex AWK jobbie?
Code:

#!/bin/bash
echo "ENTER Directory or directories"
read dir
echo "ENTER FILE or files"
read file

for dir in "$@"; do
  for file in "$dir"/*; do
    echo "Doing something with '$file'"
  done
done


shivaa 02-26-2013 10:49 AM

If your purpose is to search for file, entered by user, to search in a tar file, then why don't you do like:

Code:

#!/bin/bash
echo "Enter tar filename: "; read tarfile
echo "Enter file to be searched: "; read file
tar -tvf $tarfile | grep $file


Batistuta_g_2000 02-26-2013 11:05 AM

Quote:

Originally Posted by shivaa (Post 4900167)
If your purpose is to search for file, entered by user, to search in a tar file, then why don't you do like:

Code:

#!/bin/bash
echo "Enter tar filename: "; read tarfile
echo "Enter file to be searched: "; read file
tar -tvf $tarfile | grep $file


Thanks but the main purpose is to be able to search for many different files in a many different tarfiles.
So sample out put might be:

Enter file name/s:

bill bob sam frank jim

Enter tar file name/s:

accounts_dept.tar.gz sales_dept.tar.gz marketing_dept.tar.gz

And the "find" resluts as something like:

bill sam backed up in accounts_dept.tar.gz
frank backed up in marketing_dept.tar
jim not found
bob not found

.....

no idea where to start??!!

shivaa 02-26-2013 11:19 AM

Look, putting one tar file and than one file, which is to be searched in that tar, is a complicated plus inconvenient task.

So better use arrary here.

Either insert all tar files in an arrary or insert all those files which are to be searched, in the arrary. Then use a simple for or while loop to search the same.
Code:

tarfiles=(accounts_dept.tar.gz sales_dept.tar.gz marketing_dept.tar.gz )
echo "Enter filename: "; read file
for file in ${tarfiles[@]}; do
tar -tvf tarfile[@] | grep $file
echo "$file found in tarfile[@]"
done

Also check this once.

Batistuta_g_2000 02-26-2013 11:36 AM

Quote:

Originally Posted by shivaa (Post 4900195)
Look, putting one tar file and than one file, which is to be searched in that tar, is a complicated plus inconvenient task.

So better use arrary here.

Either insert all tar files in an arrary or insert all those files which are to be searched, in the arrary. Then use a simple for or while loop to search the same.
Code:

tarfiles=(accounts_dept.tar.gz sales_dept.tar.gz marketing_dept.tar.gz )
echo "Enter filename: "; read file
for file in ${tarfiles[@]}; do
tar -tvf tarfile[@] | grep $file
echo "$file found in tarfile[@]"
done

Also check this once.


Great thanks


All times are GMT -5. The time now is 02:33 PM.