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? |
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 |
Hi, welcome to LQ!
Never tried it with pure bash, but ... basename $dirrec1 Cheers, Tink |
With you parameter substitution you can do:
Code:
echo ${dirrec1##*/} |
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. |
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" Good luck! |
Thanks again.
Quote:
|
All times are GMT -5. The time now is 05:03 PM. |