Regular expression to match only last folder [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • How do $0##*/ and $0%/* work?

    1 answer



  • How to get last part of http link in Bash?

    5 answers



  • Extracting filename from path string

    1 answer



I would like to get the name of the last folder in a path for use in bash script.
For example: I have a path /media/devicename/folder1/ I would like to get a variable called foldername that has in it the word folder1. If I do sed 's//.*/$//' /media/devicename/folder1/ that matches the entire line. How can I limit the regular expression to only the first occurrence of the backslash?



This question is a little different than How to get last part of http link in Bash? and Extracting filename from path string because I am trying to get the last folder not file or http link which have a different pattern (For those sed 's//.*// !' should work too.) Basename is what I was looking for.










share|improve this question















marked as duplicate by don_crissti, Goro, Romeo Ninov, G-Man, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
49 secs ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
    – Goro
    27 mins ago







  • 2




    or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
    – don_crissti
    21 mins ago















up vote
0
down vote

favorite













This question already has an answer here:



  • How do $0##*/ and $0%/* work?

    1 answer



  • How to get last part of http link in Bash?

    5 answers



  • Extracting filename from path string

    1 answer



I would like to get the name of the last folder in a path for use in bash script.
For example: I have a path /media/devicename/folder1/ I would like to get a variable called foldername that has in it the word folder1. If I do sed 's//.*/$//' /media/devicename/folder1/ that matches the entire line. How can I limit the regular expression to only the first occurrence of the backslash?



This question is a little different than How to get last part of http link in Bash? and Extracting filename from path string because I am trying to get the last folder not file or http link which have a different pattern (For those sed 's//.*// !' should work too.) Basename is what I was looking for.










share|improve this question















marked as duplicate by don_crissti, Goro, Romeo Ninov, G-Man, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
49 secs ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
    – Goro
    27 mins ago







  • 2




    or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
    – don_crissti
    21 mins ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • How do $0##*/ and $0%/* work?

    1 answer



  • How to get last part of http link in Bash?

    5 answers



  • Extracting filename from path string

    1 answer



I would like to get the name of the last folder in a path for use in bash script.
For example: I have a path /media/devicename/folder1/ I would like to get a variable called foldername that has in it the word folder1. If I do sed 's//.*/$//' /media/devicename/folder1/ that matches the entire line. How can I limit the regular expression to only the first occurrence of the backslash?



This question is a little different than How to get last part of http link in Bash? and Extracting filename from path string because I am trying to get the last folder not file or http link which have a different pattern (For those sed 's//.*// !' should work too.) Basename is what I was looking for.










share|improve this question
















This question already has an answer here:



  • How do $0##*/ and $0%/* work?

    1 answer



  • How to get last part of http link in Bash?

    5 answers



  • Extracting filename from path string

    1 answer



I would like to get the name of the last folder in a path for use in bash script.
For example: I have a path /media/devicename/folder1/ I would like to get a variable called foldername that has in it the word folder1. If I do sed 's//.*/$//' /media/devicename/folder1/ that matches the entire line. How can I limit the regular expression to only the first occurrence of the backslash?



This question is a little different than How to get last part of http link in Bash? and Extracting filename from path string because I am trying to get the last folder not file or http link which have a different pattern (For those sed 's//.*// !' should work too.) Basename is what I was looking for.





This question already has an answer here:



  • How do $0##*/ and $0%/* work?

    1 answer



  • How to get last part of http link in Bash?

    5 answers



  • Extracting filename from path string

    1 answer







linux bash text-processing sed regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 min ago









Kusalananda

109k14213336




109k14213336










asked 30 mins ago









novice

205




205




marked as duplicate by don_crissti, Goro, Romeo Ninov, G-Man, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
49 secs ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by don_crissti, Goro, Romeo Ninov, G-Man, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
49 secs ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2




    Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
    – Goro
    27 mins ago







  • 2




    or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
    – don_crissti
    21 mins ago













  • 2




    Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
    – Goro
    27 mins ago







  • 2




    or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
    – don_crissti
    21 mins ago








2




2




Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
– Goro
27 mins ago





Hello @novice if you want the last folder name you can use basename, you don't need regular expression for that? I mean basename /media/devicename/folder1/ ==> folder1
– Goro
27 mins ago





2




2




or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
– don_crissti
21 mins ago





or Extracting filename from path string etc... there are many similar or related questions (like dozens...)
– don_crissti
21 mins ago
















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay