LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help I need help tarring multiple files in multiple directories (https://www.linuxquestions.org/questions/linux-newbie-8/help-i-need-help-tarring-multiple-files-in-multiple-directories-159583/)

VisionZ 03-18-2004 06:16 PM

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.

Tinkster 03-18-2004 06:53 PM

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

VisionZ 03-18-2004 10:24 PM

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.

VisionZ 03-18-2004 10:26 PM

how can i tell if they end is .sh???

Tinkster 03-19-2004 12:02 AM

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


VisionZ 03-20-2004 12:44 AM

patience
 
Thanks for helping me I,ll give it a shot and let ya know if it works. I appreciate the patience. :D

VisionZ 03-20-2004 06:04 AM

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.

Bebo 03-22-2004 04:25 AM

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.

VisionZ 03-22-2004 06:01 AM

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??

Bebo 03-22-2004 06:47 AM

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?

VisionZ 03-22-2004 05:18 PM

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

:D
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.

Tinkster 03-22-2004 11:14 PM

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 :)

VisionZ 03-23-2004 01:17 AM

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???

VisionZ 03-23-2004 01:22 AM

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.???

VisionZ 03-23-2004 01:33 AM

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??

Tinkster 03-23-2004 02:21 AM

What do you get in cygwin when you try
echo "Test" > test
?

Even that emulation can't be THAT bad...


Cheers,
Tink

Bebo 03-23-2004 02:25 AM

Ohnoooo...! I'm sorry VisionZ, I gave you a script which is a bit too specific in the wrong places. You see the line with the find command? It says it should find files under /usr/local/bin, which is where I have my scripts :) I did this just to test the script, but I forgot to change it when I posted it. OK, let's see now... I take som ideas from Tink's script now :)

Code:

#!/bin/sh

SCRIPTNAME=~/archiver

if [ x`echo $1` = x ] ; then
  ARCHIVEDIR=`pwd`
else
  ARCHIVEDIR="$1"
fi

ARCHIVE=${ARCHIVEDIR}/scripts.tar

find $ARCHIVEDIR -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


VisionZ 03-23-2004 04:07 AM

OK I think it may have worked, but I cant find a tar archive??? it should be in my home correct? What is its name also, scripts.tar??

except I get no output, it just sits there and waits, then goes back to the prompt. I get no echoing or any output at all.

I really do appreciate both of your guys help and paitence, hopefull I can attain your level of skill someday kudos to both of you.

Tink - to answer your question when I make a script out of

echo "Test" > test

nothing happens, when i type it in the cygwin terminal nothing happens

it just goes to the next prompt.

Bebo 03-23-2004 04:36 AM

Yes, the tar archive is called scripts.tar and should be in your home dir. This is very strange... My script might fail if the file command doesn't work as I think. Try issue file oneofyourscripts and see what the output is. What my script looks for is output of the form "Bourne-Again shell script text executable" or "Bourne shell script text executable". If, for some reason, this isn't echoed, then my script won't work.

Sorry for confusing you; maybe I'm just stupid and write bad scripts :)

VisionZ 03-23-2004 05:13 AM

No it probably is working just fine, but is an issue with my cygwin for windows or something. Let me try
file rmv~
a script i wrote to get rid of the extra ~ files emacs makes
and see what happens.

VisionZ 03-23-2004 05:14 AM

when i try this it says

rmv~: cannot open (rmv~).

thats all it says

VisionZ 03-23-2004 05:16 AM

what are you guys using as a text editor?? is it wordpad and you create txt files or are you guys using emacs or vim? Or something else I dont know about.

Bebo 03-23-2004 05:43 AM

I'm usually using emacs or pico, vi/vim when I'm forced to it :)

That file error message looks odd to me. Another possibility is to rewrite the BASHSCRIPT "boolean" to look for the string "#!/bin/" in every file:
Code:

BASHSCRIPT=`grep -c "\#\!/bin/" "$FILE"`

VisionZ 03-23-2004 06:31 AM

I,ll give it a try, and and well this is what happened



IT WORKED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
now one more SMALL thing how do I make it not archive itself, and to check and see if the archive it is making already exists, I dont want to archive over a previous archve, maybe just add a 1 and so on to the end or something if that is not to complicated

Bebo 03-23-2004 10:08 AM

To solve the problem with its archiving itself in a nice way I'd first like to know a way to expand directory names such as ./blah and ../../blah, or ~/blah to the proper full path name. This is something I haven't learned yet :)

VisionZ 03-23-2004 11:15 PM

Again Thanks a ton for your help I,m Afferoing ya

Bebo 03-24-2004 01:59 AM

Gee thanks :) But can you do that? I haven't signed up...

VisionZ 03-24-2004 02:40 AM

I dont know, I,ll figure it out though, I left feedback =)
Not only am I new to linux I,m new to linuxquestions.com
But you all have inspired me to parition some of my HD to Slackware
screw this cygwin BS!

Bebo 03-25-2004 05:25 PM

Hello again, VisionZ and spectators :) I've solved your problem now, I think:
Code:

#!/bin/sh

ARCHIVEBASENAME=scripts

if [ x`echo $1` = x ] ; then
  SEARCHDIR=.
else
  SEARCHDIR="$1"
fi
 
NUMBER=1
while [ -f "${ARCHIVEBASENAME}${NUMBER}.tar" -o -f "${ARCHIVEBASENAME}${NUMBER}.tar.gz" ] ; do
  NUMBER=$((NUMBER + 1))
done

ARCHIVE=${ARCHIVEBASENAME}${NUMBER}.tar

if [ -d "$SEARCHDIR" ] ; then
  find "$SEARCHDIR" -type f | while read FILE ; do
      ISSCRIPT=`grep -c "\#\!/bin/" "$FILE" 2> /dev/null`
     
      [ $? -gt 0 ] && ISSCRIPT=0
     
      if [ $ISSCRIPT -eq 1 ] ; then
        if [ ! "$FILE" -ef "$0" ] ; then
            echo "Archiving $FILE in $ARCHIVE.gz"
            tar rf "$ARCHIVE" "$FILE"
        fi
      fi 
  done   

  [ -f "$ARCHIVE" ] && gzip "$ARCHIVE"
fi

Well, this was fun, we have to do it again some time ;) :p


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