Remove part of path on Unix

If you wanted to remove a certain NUMBER of path components, you should use cut with -d"https://stackoverflow.com/". For example, if path=/home/dude/some/deepish/dir:

To remove the first two components:

# (Add 2 to the number of components to remove to get the value to pass to -f)
echo $path | cut -d"https://stackoverflow.com/" -f4-
# output:
# some/deepish/dir

To keep the first two components:

echo $path | cut -d"https://stackoverflow.com/" -f-3
# output:
# /home/dude

To remove the last two components (rev reverses the string):

echo $path | rev | cut -d"https://stackoverflow.com/" -f4- | rev
# output:
# /home/dude/some

To keep the last three components:

echo $path | rev | cut -d"https://stackoverflow.com/" -f-3 | rev
# output:
# some/deepish/dir

Or, if you want to remove everything before a particular component, sed would work:

echo $path | sed 's/.*\(some\)/\1/g'
# output:
# some/deepish/dir

Or after a particular component:

echo $path | sed 's/\(dude\).*/\1/g'
# output:
# /home/dude

It’s even easier if you don’t want to keep the component you’re specifying:

echo $path | sed 's/some.*//g'
# output:
# /home/dude/

And if you want to be consistent you can match the trailing slash too:

echo $path | sed 's/\/some.*//g'
# output:
# /home/dude

Of course, if you’re matching several slashes, you should switch the sed delimiter:

echo $path | sed 's!/some.*!!g'
# output:
# /home/dude

Note that these examples all use absolute paths, you’ll have to play around to make them work with relative paths.

Leave a Comment