329

I'm trying to write a post-commit hook for SVN, which is hosted on our development server. My goal is to try to automatically checkout a copy of the committed project to the directory where it is hosted on the server. However I need to be able to read only the last directory in the directory string passed to the script in order to checkout to the same sub-directory where our projects are hosted.

For example if I make an SVN commit to the project "example", my script gets "/usr/local/svn/repos/example" as its first argument. I need to get just "example" off the end of the string and then concat it with another string so I can checkout to "/server/root/example" and see the changes live immediately.

4 Answers 4

480

basename does remove the directory prefix of a path:

$ basename /usr/local/svn/repos/example
example
$ echo "/server/root/$(basename /usr/local/svn/repos/example)"
/server/root/example
3
  • 2
    basename is definitely what I'm looking for. How can get the basename of an argument stored into a variable though? E.g. SUBDIR="/path/to/whatever/$(basename $1)"
    – TJ L
    Jul 20, 2010 at 20:38
  • 6
    @tj111: sounds like is no $1, or $1 is empty
    – sth
    Jul 20, 2010 at 20:59
  • 1
    unfortunately, if you wrap commands, basename is not a good idea. just something to keep in mind
    – dtc
    Jul 20, 2016 at 20:36
145

The following approach can be used to get any path of a pathname:

some_path=a/b/c
echo $(basename $some_path)
echo $(basename $(dirname $some_path))
echo $(basename $(dirname $(dirname $some_path)))

Output:

c
b
a
1
  • 4
    does not work with paths that have spaces... you can overcome that with quotes... which somehow works echo "$(basename "$(dirname "$pathname")")"
    – Ray Foss
    Aug 15, 2019 at 14:52
94

Bash can get the last part of a path without having to call the external basename:

dir="/path/to/whatever/"
dir="${dir%/}"             # strip trailing slash (if any)
subdir="${dir##*/}"

This uses Bash's parameter expansion to remove the part of the string before the last (remaining) slash.

10
  • 3
    On my Mac, using substring notation is more than order of magnitude faster than dirname / basename for the case where you're doing something trivial to each of a few thousand files.
    – George
    Jun 26, 2014 at 1:24
  • 1
    d=/home/me/somefolder;subdir="/$d/${1##*/}" I ended up with something like //home/me/somefolder// the $d actually comes from a loop for d in $(find $SOMEFOLDER -maxdepth 1 -type d); Using subdir=$(basename $d) works as expected. May 5, 2015 at 2:02
  • 2
    @ButtleButkus: You should use while instead of for to iterate over the output of find (find -print0 | xargs -0 is better) or use globbing: for d in $SOMEFOLDER/*/ (the final slash works like -type d - you can use ** in Bash 4 for recursion if you shopt -s globstar, but an "Argument list too long" message is possible). Note that the ${1} portion of the command represents the first argument of a script or function. You may need to use ${d##*/} or some other variable or argument specification or make sure that an argument is being passed in $1 May 5, 2015 at 2:55
  • 6
    @DennisWilliamson Thanks a lot for sharing. For any future readers who start to wonder why it is not working or I am the only stupid out here 😂. Above answer assumes that $1 contains the path from which last component is to be taken out. I missed that part. My use case: target_path='/home/user/dir1/dir2/dir3/'; target_path="${target_path%/}"; last_component=${target_path##*/}; echo $last_component - Works 😉 Apr 13, 2018 at 11:10
  • 2
    See here for an explanation of how and why ${1##*/} works: unix.stackexchange.com/a/171786/15070
    – Matt
    Apr 15, 2018 at 10:50
3

To print the file name without using external commands,

Run:

fileNameWithFullPath="${fileNameWithFullPath%/}";
echo "${fileNameWithFullPath##*/}" # print the file name

This command must run faster than basename and dirname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.