I have doing tons of research and can't seem to figure it out.

I am using sed and trying to process a file of paths


I need to remove everything past the last / but not the / itself. Can anyone help me with this? I can never handle sed lol

  • It looks like you want to do something that dirname command do. Can you try this one?
    – mrc02_kr
    Nov 14, 2019 at 6:44
  • Every line is different. I have a find command that searches for .rar files and another that extracts them. But I need it to show only the folder name for the command to work properly Nov 14, 2019 at 6:52
  • That's exactly what dirname do. But to be sure, could you provide examples of input and output?
    – mrc02_kr
    Nov 14, 2019 at 7:01
  • Could you say something about the underlying issue in your question, e.g. why you have a file with pathnames in it and whether this is part of solving -another task? Storing pathnames in a file is rarely necessary.
    – Kusalananda
    Nov 14, 2019 at 8:37

2 Answers 2


Use dirname:

dir="$(dirname $path)/"

Or use shell:



$ echo "$dir"

To find all "/..../file.rar" and print "/..../" in a file-of-paths you can use good old grep...

grep -Po '/\S+/(?=\S+\.rar\b)' file-of-paths

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .