You are not logged in.

#1 2011-01-04 11:58:17

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved] bash: remove everything before / after last slash

Hi!


I know this is easy with sed, but I could have sworn there once was a bash tool - maybe even coreutils - that's sole purpose was making strings like
"/folder/something/file.ex"
into
"/folder/something/"
or
"file.ex"

Am I mistaken or can someone tell me the name of this tool and if it's (still) common?


Thx!

Last edited by whoops (2011-01-04 12:36:09)

Offline

#2 2011-01-04 12:13:48

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,403
Website

Re: [solved] bash: remove everything before / after last slash

basename?  That prints the folder.

You can do this using bash string substitution too.

Offline

#3 2011-01-04 12:21:45

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: [solved] bash: remove everything before / after last slash

To answer your other question, the command "dirname". Watch out for the root directory though. Both basename and dirname are owned by coreutils.

Offline

#4 2011-01-04 12:35:49

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] bash: remove everything before / after last slash

Exactly, thanks!

So simple, yet so hard to google.

Last edited by whoops (2011-01-04 12:36:53)

Offline

#5 2011-01-04 14:20:45

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: [solved] bash: remove everything before / after last slash

You can use bash with parameter expansion:

foo="/folder/something/file.ex" ; echo "${foo##*/}" ; echo "${foo%/*}"

Offline

Board footer

Powered by FluxBB