LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-26-2013, 11:10 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 02-26-2013, 11:49 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
Old 02-26-2013, 12:05 PM   #3
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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??!!
 
Old 02-26-2013, 12:19 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.

Last edited by shivaa; 02-26-2013 at 12:23 PM.
 
Old 02-26-2013, 12:36 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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
 
  


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] copy multiple files to multiple directories Sanderson Linux - Newbie 15 01-22-2013 11:17 AM
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 02:52 PM
[Perl] Search for multiple files in multipe directories Asy Programming 7 10-28-2009 03:20 PM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 09:16 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 06:25 PM


All times are GMT -5. The time now is 08:31 AM.

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