LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script (untar *.tar files and then cd to the untared dir) (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-untar-%2A-tar-files-and-then-cd-to-the-untared-dir-129727/)

k0ljat 12-29-2003 06:36 AM

Bash script (untar *.tar files and then cd to the untared dir)
 
I wanted to do a script that would untar all the *.tar files in /home/koljat/ dir, and after untaring them would cd into the untared directory so far i have done this :

Code:

#!/bin/bash
tar -zxvf /home/koljat/*.tar
cd ?????

it untar's files with no error's but the changing directory part is the thing that bothers me..

XavierP 12-29-2003 07:47 AM

Why not create an 'untar' directory, move all the tar files into it and then run your script directing the file to cd into whatever your untar directory is.

Your script would then be:

#!/bin/bash
tar -zxvf /home/koljat/*.tar
cd untar

If you look at "man tar" there should be an instruction on how to make files untar into a different directory - similar to the way Winzip can do this.

lone_nut 12-30-2003 05:57 AM

you can't do that, because bash creates a new shell every time you type a new command. The shell script will change directory, but it will then quit leaving you with your original shell and the origial directory.
for your idea to work, you will need to run the script as
. script
(a dot a space and then the name of the script)
instead of
./script
therefore delete the line reading
Code:

#!/bin/bash
and run the script as
Code:

. script
therefore, there is no need for the script to be executable.


All times are GMT -5. The time now is 03:02 AM.