LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get the filename (https://www.linuxquestions.org/questions/linux-newbie-8/get-the-filename-835497/)

pginfl 09-30-2010 06:09 PM

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?

GrapefruiTgirl 09-30-2010 06:19 PM

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

Tinkster 09-30-2010 06:21 PM

Hi, welcome to LQ!

Never tried it with pure bash, but ...

basename $dirrec1



Cheers,
Tink

grail 09-30-2010 06:44 PM

With you parameter substitution you can do:
Code:

echo ${dirrec1##*/}
This of course assumes that the file name does not contain a /

pginfl 10-01-2010 07:03 AM

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.

GrapefruiTgirl 10-01-2010 07:11 AM

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!

pginfl 10-01-2010 07:41 AM

Thanks again.
 
Quote:

Originally Posted by GrapefruiTgirl (Post 4114675)
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.


All times are GMT -5. The time now is 05:03 PM.