linux,

Get current folder name in Linux without full path

Aug 07, 2022 · 1 min read · 18 views · Post a comment

As you may already know getting the relative path in Linux can be easily achieved by typing the command pwd in your terminal. You may be wondering how to get only the current working directory without a full path? Let me show you some solutions.

Prerequisites

  • Linux
  • sudo privileges

Solution

Step 1. Using echo.

echo "${PWD##*/}"

Step 2. Using printf.

printf '%s\n' "${PWD##*/}"

Step 3. Using the basename tool.

basename "$PWD"

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.