Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


LinkBack Search this Thread
Old 08-20-2005, 10:50 AM   #1
Registered: Jul 2004
Posts: 45

Rep: Reputation: 15
bash script help needed

Hi all,

I'll just quickly explain the script I'm trying to right.

I've got lots of directories containing .rar files that go like this...


I want to run through each directory running the unzip command on file.rar. I think the command has to be run from the directory that contains the .rar because it has to be able to see the sequential files also.

In dodgy code:

for filename in /startdir/
     tar --uncompress $filename
I think the problem I'm having at the moment is there are spaces in the file/dir names. So $filename contains non-escaped spaces messing everything up.

Does anyone have a standard script for this kind of thing? I find myself wanting this kind of thing all the time... with programs other than tar too.

Old 08-20-2005, 10:59 AM   #2
Registered: Jun 2005
Posts: 96

Rep: Reputation: 15
have you tried encompassing $filename in quotes
tar --uncompress "$filename"
i had a problem with a script that would work when i typed the commands manually but when i ran them from the script it would break because of spacing (or other non-escaped characters). After wrapping the $var in quotes all my problems cleared.

hope this helps
Old 08-20-2005, 04:40 PM   #3
Registered: Jul 2004
Posts: 45

Original Poster
Rep: Reputation: 15

Working now
Old 08-21-2005, 03:01 AM   #4
Senior Member
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 46
you could also do something like this

find /startdir/ -iname \*.rar -exec tar --uncompress "{}" \;
it means that it finds any file that ends with .rar in the /startdir/ and when it does it will execute the tar --uncompress command on these files

Last edited by heema; 08-21-2005 at 03:06 AM.
Old 08-21-2005, 09:54 AM   #5
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You can also change your IFS variable to use, say, newline as the field separator in the script. Better to not have spaces in filenames, though, I think that's Bill's attempt to scupper Linux/UNIX.


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script. help needed tazthecat Linux - General 2 09-30-2005 01:54 AM
Wrapper needed for BASH Script outspoken Programming 0 01-12-2005 09:04 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration