1

Need to print full path of a folder which is from terminal which is not the current directory. For example /media/hardDrive/Music folder has three sub-directories Album_1 Album_2 Album_3.

/media/hardDrive/Music $ ls
Album_1 Album_2 Album_3

Need a command (CMD) that prints full path of a given folder from the current directory without needing to append the directory to the output of pwd.

/media/hardDrive/Music $ CMD Album_1
/media/hardDrive/Music/Album_1
2
  • Why can't you use pwd? Commented Mar 23, 2023 at 0:51
  • pwd will print the current directory. Like in example, it will print /media/hardDrive/Music/. I need to print the subdirectories without changing current working directory. Commented Mar 23, 2023 at 0:53

2 Answers 2

2

I found the command. It can be done using realpath.

>/media/hardDrive/Music/ $ realpath Album_1
/media/hardDrive/Music/Album_1
0

You can also use find to achieve that. realpath may not exist on all the systems.

find $(pwd) -name "FILE_or_DIR_NAME"

Example:

$ cd ~
$ find $(pwd) -name "reports"
/home/tommy/reports
3
  • find is at least as problematic as realpath. They are both defined in POSIX, but find is known to have multiple, slightly incompatible implementations. Commented Mar 23, 2023 at 3:07
  • But that simple syntax should be available widely. realpath may not be installed by default. At least it happens on one of my testing VMs Commented Mar 23, 2023 at 5:25
  • find is also resource intensive and potentially returns multiple results when there are more than one file with the same name. There are better options, like Python/Perl one-liners. realpath seems to be absent on macOS and some old Linux distros from a decade ago. Otherwise, it's provided by coreutils, busybox, FreeBSD, NetBSD, OpenBSD. Commented Mar 23, 2023 at 14:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.