Delete everything before last / in a file path
Asked Answered
C

4

20

I have many file paths in a file that look like so:

/home/rtz11/files/testfiles/547/prob547455_01

I want to use a bash script that will print all the filenames to the screen, basically whatever comes after the last /. I don't want to assume that it would always be the same length because it might not be.

Would there be a way to delete everything before the last /? Maybe a sed command?

Conglobate answered 21/4, 2015 at 1:18 Comment(1)
Do you also want to delete the last /, or only content prior to it?Bullfrog
A
20
awk '{print $NF}' FS=/ input-file

The 'print $NF' directs awk to print the last field of each line, and assigning FS=/ makes forward slash the field delimeter. In sed, you could do:

sed 's@.*/@@' input-file

which simply deletes everything up to and including the last /.

Aplasia answered 21/4, 2015 at 1:36 Comment(1)
Note that the at sign @ is merely used here as a delimiter, replacing the usual forward slash /. The latter cannot be readily employed because it constitutes the target. Equivalent commands would be sed s|.*/||' or sed 's:.*/::'.Legerdemain
B
43

Using sed for this is vast overkill -- bash has extensive string manipulation built in, and using this built-in support is far more efficient when operating on only a single line.

s=/home/rtz11/files/testfiles/547/prob547455_01
basename="${s##*/}"
echo "$basename"

This will remove everything from the beginning of the string greedily matching */. See the bash-hackers wiki entry for parameter expansion.


If you only want to remove everything prior to the last /, but not including it (a literal reading of your question, but also a generally less useful operation), you might instead want if [[ $s = */* ]]; then echo "/${s##*/}"; else echo "$s"; fi.

Bullfrog answered 21/4, 2015 at 1:29 Comment(1)
Excellent resource. Side note, if you need the Path to a file use ${PATHNAME%/*}.Admiral
A
20
awk '{print $NF}' FS=/ input-file

The 'print $NF' directs awk to print the last field of each line, and assigning FS=/ makes forward slash the field delimeter. In sed, you could do:

sed 's@.*/@@' input-file

which simply deletes everything up to and including the last /.

Aplasia answered 21/4, 2015 at 1:36 Comment(1)
Note that the at sign @ is merely used here as a delimiter, replacing the usual forward slash /. The latter cannot be readily employed because it constitutes the target. Equivalent commands would be sed s|.*/||' or sed 's:.*/::'.Legerdemain
H
3

Meandering but simply because I can remember the syntax I use:

cat file | rev | cut -d/ -f1 | rev

Many ways to skin a 'cat'. Ouch.

Helsa answered 27/4, 2019 at 23:1 Comment(1)
This is way too expensive, though it does the job well.Wellpreserved
I
1

One more way:

Use the basename executable (command):

basename /path/with/many/slashes/and/file.extension

>file.extension

basename /path/with/many/slashes/and/file.extension .extension

OR

basename -s .extension /path/with/many/slashes/and/file.extension 

> file

Indican answered 18/10, 2020 at 15:27 Comment(1)
This basename is probably the "correct"/Unix way, though there is a sed way.Virescent

© 2022 - 2024 — McMap. All rights reserved.