LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-30-2010, 07:09 PM   #1
pginfl
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Rep: Reputation: 0
Get the filename


#!/bin/bash

dirrec1="./yyyymmdd/steak/chicken/fish/file1.doc"
dirrec2="./yyyymmdd/apples/bananas/peaches/pears/filer2.doc"

echo $dirrec1
echo $dirrec2
echo ${dirrec1%/*.*}
echo ${dirrec2%/*.*}

This little fragment does the opposite of what I want and I am trying to figure out how to reverse it. I want to strip off the directory and echo just the filenames. I cannot predict how many levels deep the directory tree is.

Any script wizards?
 
Old 09-30-2010, 07:19 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Probably a few 'wizards' around somewhere.. Is this homework, or do you just want a simple quick way to get the filename?

Code:
shell# basename $dirrec1
Sasha
 
Old 09-30-2010, 07:21 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Never tried it with pure bash, but ...

basename $dirrec1



Cheers,
Tink
 
Old 09-30-2010, 07:44 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
With you parameter substitution you can do:
Code:
echo ${dirrec1##*/}
This of course assumes that the file name does not contain a /
 
Old 10-01-2010, 08:03 AM   #5
pginfl
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks everyone. Is there an basic tutorial on how the "string editing" functions in {} works? And thanks, again for "basename" I figured there had to be something like that but could not
find it.
 
Old 10-01-2010, 08:11 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
The bash man page is a pretty good place to see the many ways of doing expansion & substitution of variables using the shell. Read it in the section called "Parameter Expansion". Also, look up "Absolute Bash Scripting Guide", and http://mywiki.wooledge.org/BashFAQ for lots of shell goodies.

For what it's worth, here's another way to do what you wish:
Code:
root@reactor: dirrec1="./yyyymmdd/steak/chicken/fish/file1.doc"
root@reactor: echo ${dirrec1/*\//}
file1.doc
root@reactor:
Though I'll be the first to admit, I don't use the shell variable substitution for this purpose as much as other some members here (I would use `basename` for this), so precisely *why* this particular syntax works, and under which conditions it may fail, you'll have to research yourself or await a breakdown by someone else.

Good luck!

Last edited by GrapefruiTgirl; 10-01-2010 at 08:13 AM. Reason: typo - missing brace
 
Old 10-01-2010, 08:41 AM   #7
pginfl
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks again.

Quote:
Originally Posted by GrapefruiTgirl View Post
The bash man page is a pretty good place to see the many ways of doing expansion & substitution of variables using the shell. Read it in the section called "Parameter Expansion". Also, look up "Absolute Bash Scripting Guide", and http://mywiki.wooledge.org/BashFAQ for lots of shell goodies.

For what it's worth, here's another way to do what you wish:
Code:
root@reactor: dirrec1="./yyyymmdd/steak/chicken/fish/file1.doc"
root@reactor: echo ${dirrec1/*\//}
file1.doc
root@reactor:
Though I'll be the first to admit, I don't use the shell variable substitution for this purpose as much as other some members here (I would use `basename` for this), so precisely *why* this particular syntax works, and under which conditions it may fail, you'll have to research yourself or await a breakdown by someone else.

Good luck!
Thanks, in this case the basename is fine because I know the extension. But I like to learn as much as possible for future use.
 
  


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
How can we convert a dynamic library (filename.so) to a static library (filename.a). hanumaan Programming 4 11-20-2009 01:07 AM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 10:29 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 07:07 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 05:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 11:32 PM


All times are GMT -5. The time now is 08:36 PM.

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