LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-12-2011, 03:11 PM   #1
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Rep: Reputation: 0
get last folder in path


Hi, I am trying to zip a directory but when I zip and unzip it, it contains the whole path of the variable "album". I would just like to zip the last folder in the path. Here is what I have now. I tried basename but that didn't seem to work, possible I just can't make it work right.

Code:
#!/bin/sh

album=/var/www/hfandt.com/artists/*/*
for al in $album
do 	
	if [ ! -f "$al" ]; then
	tar -zcvf "$al.tar.gz" "$al"
	rm -r "$al"
	fi
done
Thanks
 
Old 04-12-2011, 03:49 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Use the basename command.
 
Old 04-12-2011, 03:56 PM   #3
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Original Poster
Rep: Reputation: 0
I tried that but I can't get it to work
When I use
Code:
do
basename $al
done
it does exactly what I want it to and lists the last folders

My problem is that I can't implement into the tar command like
Code:
tar -zcvf "basename $al.tar.gz" "$al"
I'm guessing my syntax is wrong but I've tried it a hundred different ways with no luck
 
Old 04-12-2011, 04:12 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
tar -zcvf "$(basename $al).tar.gz" "$al"
 
Old 04-12-2011, 04:15 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by acid_kewpie View Post
tar -zcvf "$(basename $al).tar.gz" "$al"
I would quote the $al after basename, in case the path has spaces in it.
 
1 members found this post helpful.
Old 04-12-2011, 04:23 PM   #6
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks, I tried that but the spaces in the folder name caused a problem.
So, I tried
Code:
tar -zcvf "$(basename "$al").tar.gz" "$al"
and 
tar -zcvf "$("basename $al").tar.gz" "$al"
but still didn't work
I've also been trying this approach
Code:
${al##*/}
which gives me the last folder but won't work like so
Code:
tar -zcvf "${al##*/}.tar.gz" "$al"
 
Old 04-12-2011, 04:27 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by xjx424 View Post
Thanks, I tried that but the spaces in the folder name caused a problem.
So, I tried
Code:
tar -zcvf "$(basename "$al").tar.gz" "$al"
and 
tar -zcvf "$("basename $al").tar.gz" "$al"
but still didn't work
Define "didn't work".
 
Old 04-12-2011, 04:31 PM   #8
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Original Poster
Rep: Reputation: 0
it's not creating the zipped file

here is the response I get which looks as though its working
Code:
tar: Removing leading `/' from member names
/var/www/hfandt.com/artists/Al Green/Call Me/
/var/www/hfandt.com/artists/Al Green/Call Me/Have You Been Making Out O.k..mp3
/var/www/hfandt.com/artists/Al Green/Call Me/Funny How Time Slips Away.mp3
tar: Removing leading `/' from member names
/var/www/hfandt.com/artists/Al Green/Let's Stay Together/
/var/www/hfandt.com/artists/Al Green/Let's Stay Together/05 Old Time Lovin.mp3
/var/www/hfandt.com/artists/Al Green/Let's Stay Together/01 Let's Stay Together.mp3
tar: Removing leading `/' from member names
/var/www/hfandt.com/artists/Assembly of Dust/Shishkebab_ A Jam Band Compendium, Volum/
/var/www/hfandt.com/artists/Assembly of Dust/Shishkebab_ A Jam Band Compendium, Volum/03 Arc of the Sun (feat. Mike Gordon.mp3
 
Old 04-12-2011, 04:39 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
hint - don't randomly hack in miscellaneous punctuation into scripts "just in case it works". Understand what you're doing in the first place.
 
Old 04-12-2011, 04:56 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by acid_kewpie View Post
hint - don't randomly hack in miscellaneous punctuation into scripts "just in case it works". Understand what you're doing in the first place.
Like in your first attempt with basename: what makes you think that the word "basename" in quotes will act as a command, but "dgsnfgsdfig" won't?

Also, "basename $al" won't work because quotes combine everything into one string, even if there are spaces inside. So if $al contains "thing/stuff", then it would try to find the command "stuff" in the directory "basename thing".

Basically, the way bash works is that it treats the first string as the command, and following strings as arguments. That's probably the most important concept.

Last edited by MTK358; 04-12-2011 at 05:04 PM.
 
Old 04-12-2011, 05:01 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
I found another problem!

You mixed up the parameters to tar. You are not calling basename on the path to be tar'ed, which means they are absolute paths. tar does not allow this by default because if you untar'ed it, it could overwrite important files not in you current directory.

(and by the way, a tar file is not a zipped (which refers to the ZIP format) file.)

So tell me, what do you want the tar files to contain, and how do you want them named?

Last edited by MTK358; 04-12-2011 at 05:02 PM.
 
Old 04-12-2011, 05:27 PM   #12
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Original Poster
Rep: Reputation: 0
I did realize that putting basename in the quotes was wrong when it showed the file path as "basename /var/www/...".
This is my first day working with bash commands so I'm really in the dark.

I would like the tar files to contain the last folder in the path or the last "*" in
album=/var/www/hfandt.com/artists/*/*

The name worked for me, it was the last "*" or folder name.tar.gz but the tar'ed file was incorrect

I'm pretty sure my problem is what you said "not calling basename on the path to be tar'ed", the whole path is being tar'ed, not the basename
 
Old 04-12-2011, 05:35 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by xjx424 View Post
I'm pretty sure my problem is what you said "not calling basename on the path to be tar'ed", the whole path is being tar'ed, not the basename
Then you should call basename on the file passed to tar.
 
Old 04-12-2011, 05:42 PM   #14
xjx424
LQ Newbie
 
Registered: Apr 2011
Posts: 15

Original Poster
Rep: Reputation: 0
I thought that was what $(basename "$al") did
I thought it would only return the last folder or file
 
Old 04-12-2011, 07:04 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by xjx424 View Post
I thought that was what $(basename "$al") did
I thought it would only return the last folder or file
But you're putting that in the argument of tar that specifies the name of the tar file, not the contents to put in it!
 
  


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
What is the path for QCAD folder in Ubuntu? coss_cat Linux - Software 3 03-29-2011 03:22 PM
Add folder to perl path sir-lancealot Programming 1 06-08-2010 01:36 PM
Path to a share folder, how??? lefaman72 SUSE / openSUSE 4 02-26-2010 05:32 AM
How do you address a folder in another path Redbug Linux - Newbie 1 08-27-2008 12:06 PM
Cant move a folder to another path? dwarf007 Linux - Newbie 2 09-19-2004 03:28 AM


All times are GMT -5. The time now is 06:20 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