LinuxQuestions.org
Visit the LQ Articles and Editorials section
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-18-2004, 07:16 PM   #1
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Rep: Reputation: 15
Exclamation Help I need help tarring multiple files in multiple directories


I cant seem to figure out how to write a script which will tar all my bash files, in all my directories in and under HOME, but not the directories themselves and not this particluar script. How can I accomplish this??? Any help would be greatly appreciated.
 
Old 03-18-2004, 07:53 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Something like
Code:
#!/bin/sh
find ~ -iname "*.sh" -exec ls -1 {} \; > ~/back_us_up.1
grep -v <backupscriptsfilenam> ~/back_us_up.1 > ~/back_us_up.txt
tar -F ~/back_us_up.txt xvzf my_scripts.tar.gz
should work ...

[edit]
If your scripts don't end in .sh it'd be a bit more
complicated ... you'd have to skip the iname bit
in find, and -exec file on all files like:
find ~ -exec file {} \; | grep -i shell | cut -d : -f 1 > ~/back_us_up.1
[/edit]

Cheers,
Tink

Last edited by Tinkster; 03-18-2004 at 07:59 PM.
 
Old 03-18-2004, 11:24 PM   #3
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
I was thinking

I am new to programing but I thought they all had to start like
#!/bin/bash

I figured the tarring would go something like

for files in $HOME | ls -l
-tar zxf backups.tar.z
then listing all the subfolders by full path name? Maybe I,m simplifying this to much. I,m just struggling on how to do this and how to TAR everything but this script. this may be a dumb question but what it the bin/sh bash line??? I use the cygwin term for windows, This is my first programming class. Any further help for this newb would be greatly appreciated.
 
Old 03-18-2004, 11:26 PM   #4
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
how can i tell if they end is .sh???
 
Old 03-19-2004, 01:02 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Re: I was thinking

Quote:
Originally posted by VisionZ
I am new to programing but I thought they all had to start like
#!/bin/bash
On most systems sh will be a symlink to
bash, but sh is "more portable" ... and if
you keep it simple like in my example it
will work with old bourne (and most likely
korn) shell as well ...

Quote:
I figured the tarring would go something like

for files in $HOME | ls -l
-tar zxf backups.tar.z
then listing all the subfolders by full path name?
You could do that, but I think my approach
is more elegant, and probably faster ;)


Cheers,
Tink


Last edited by Tinkster; 03-19-2004 at 01:04 AM.
 
Old 03-20-2004, 01:44 AM   #6
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
patience

Thanks for helping me I,ll give it a shot and let ya know if it works. I appreciate the patience.
 
Old 03-20-2004, 07:04 AM   #7
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
Post script didnt work

When i tried it, I get thte follwing errors
(archiver = my script name)

./archiver: cannot create ~/backup.1: directory nonexistent
repeats it twice
tar: you must specify on of the '-Acdtrux' options
Try 'tar --help' for more info

I,m using the bash shell, and like I said I am new to programming, some of your code made sense to me, some of the stuff I was totally lost looking at =), I am assuming the in your code where you put grep -v <myscriptname> this is where I put my script which is called archiver so it will not get tarred??? I truly do appreciate the help as this particular problem is giving me difficulty trying to figure out. Also trying to write a script which will delete my temp internet files but thats a different story.
 
Old 03-22-2004, 05:25 AM   #8
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Maybe something along these lines - although it keeps the directory structure:
Code:
#!/bin/sh

SCRIPTNAME=$HOME/archiver
[ x`echo $1` = x ] && ARCHIVE=$HOME/scripts.tar

find /usr/local/bin -type f | while read FILE ; do
   BASHSCRIPT=`file "$FILE" | grep Bourne | grep -c "shell script text executable"`

   if [ $BASHSCRIPT -eq 1 -a "$FILE" != "$SCRIPTNAME" ] ; then
      echo "Archiving $FILE in $ARCHIVE"
      tar rf $ARCHIVE $FILE
   fi
done
This uses the file command to find the (ba)sh scripts.
 
Old 03-22-2004, 07:01 AM   #9
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
After trying your script, it ran, but nothing happened, or I didnt see it. All my scripts which I wrote are still their, and I dont see a tar archive anywhere? I am running cygwin for windows, would this cause me any problems??
 
Old 03-22-2004, 07:47 AM   #10
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Well, your scripts should be left where they are, as tar only makes a tar archive and doesn't remove anything. However, I find it strange that you can't find the ~/scripts.tar archive... I don't know what cygwin does, so I can't help you with that... Can you post the output here? And your command?
 
Old 03-22-2004, 06:18 PM   #11
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
I used the exact code that you had given me above and after I ran the script, it just jumps to the next prompt in my terminal. No output in the terminal at all no echoing

Code:
echo "archiving $file in $archive"

The way my directories are set up is cygwin in under my C: drive. then it goes something like this.

cygwin/home/owner(which is my ~ directory) /emacs-21.3/bin(where my scripts are saved before I move them into owner)

I tried changing the structure in the script to something like /~/home/owner/
but that didnt work either

I appreciate the patience, if I,m not making any sense just let me know, like i said before i,m still learning.
 
Old 03-23-2004, 12:14 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
My original bits and pieces (mind you, I never tested
them ;}) had two flaws :)
Try this one:
back_scripts
Code:
#!/bin/sh
if [ "$1" != "" ]; then
    directory="$1"
else
    directory=`pwd`
fi
echo $directory
find "$directory" -exec file {} \; | grep -i shell | cut -d : -f 1 > ~/back_us_up.1
grep -v back_scripts ~/back_us_up.1 > ~/back_us_up.txt
tar czf ~/my_scripts.tar.gz -T ~/back_us_up.txt
If you don't like the intermediate files:
Code:
#!/bin/sh
if [ "$1" != "" ]; then
    directory="$1"
else
    directory=`pwd`
fi
echo $directory
find "$directory" -exec file {} \; | grep -i shell | cut -d : -f 1 | grep -v back_scripts | tar czf ~/my_scripts.tar.gz -T -

Cheers,
Tink :)
 
Old 03-23-2004, 02:17 AM   #13
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
HOLY CRAP, I dont know what happened but heres the error message I received;

cur: not found
tar (child): ~/myscripts.tar.gz: Canot open: No such file or directory
tar (child): Error is not recoverable: exiting now
find: file terminated by signal 13 (this never stops and keeps going and going and going)

until I hit Ctrl-C

everything is spot on am I doing something wrong?? Could Cygwin for windows be causing this???
 
Old 03-23-2004, 02:22 AM   #14
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
woops spelling error the first time Tink, now I have the exact same message, but no signal 13 error instead, I get the same message and then

tar: Removing leading `/' from member names

everything is perfect this time though.???
 
Old 03-23-2004, 02:33 AM   #15
VisionZ
Member
 
Registered: Mar 2004
Posts: 58

Original Poster
Rep: Reputation: 15
when I try the first one the error says

grep: ~/back_us_up.1: no such file or directory
tar: ~/back_us_up.txt: cannot open: no such file or directory
tar: error is not recoverable: exiting now

If I may ask what is this txt file??? I use emacs as a text edit not word pad.
also what is back_us_up.1 i was wondering what the .1 stood for??
 
  


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
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 08:56 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
find and copy files into multiple directories avargas22 Linux - Newbie 2 04-01-2004 12:11 PM
un-tarring multiple files phil1076 Linux - General 19 12-18-2003 03:03 PM
tar multiple directories computera Linux - General 2 11-17-2001 08:27 AM


All times are GMT -5. The time now is 01:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration