Regular expression to match only last folder

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











up vote
0
down vote

favorite












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?









share





















  • 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
    2 mins ago











  • Possible duplicate of How to get last part of http link in Bash?
    – don_crissti
    2 mins ago














up vote
0
down vote

favorite












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?









share





















  • 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
    2 mins ago











  • Possible duplicate of How to get last part of http link in Bash?
    – don_crissti
    2 mins ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?









share













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?







linux bash text-processing sed regular-expression





share












share










share



share










asked 6 mins ago









novice

205




205











  • 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
    2 mins ago











  • Possible duplicate of How to get last part of http link in Bash?
    – don_crissti
    2 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
    2 mins ago











  • Possible duplicate of How to get last part of http link in Bash?
    – don_crissti
    2 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
2 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
2 mins ago













Possible duplicate of How to get last part of http link in Bash?
– don_crissti
2 mins ago




Possible duplicate of How to get last part of http link in Bash?
– don_crissti
2 mins ago















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475464%2fregular-expression-to-match-only-last-folder%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475464%2fregular-expression-to-match-only-last-folder%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

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

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?