- Get absolute path of files using ‘find’ command
- Get Absolute File Path in Linux
- Use readlink to get file path
- Use realpath to get full file path
- Use the find command to get the absolute file path
- Print full path with the ls command
- Conclusion
- How to get full path name of a Linux command
- 5 Answers 5
- How to show the full path of a file or directory in the terminal?
- 3 Answers 3
Get absolute path of files using ‘find’ command
You can use bash ‘s Tilde Expansion to get the absolute path of the current working directory, this way find prints the absolute path for the results as well:
find ~+ -type f -name "filename"
If executed in ~/Desktop , this is expanded to
find /home/yourusername/Desktop -type f -name "filename"
/home/yourusername/Desktop/filename
If you want to use this approach with the current working directory’s parent directory you need to cd before calling find :
cd .. && find ~+ -type f -name "filename"
find "$(cd ..; pwd)" -name "filename"
Thanks, I was just testing $(cd ..; pwd) . Here it works OK, but if I do it alone in a Terminal, I can’t get the parent dir. I get «Bash:
Yes ( . ) means execute in a subshell, the output gets written to stdout. $( . ) stands for «command substitution». The latter can be used as if it were a variable expansion.
@Scrutinizer — I think you misunderstand how find works. If you use the full path in your search, you get the full path in your output. find /home/your_user -name foo . As OP is using «../» , hard to guess the full path to give in an answer.
Try using the -exec option of find :
find .. -name "filename" -exec readlink -f <> \;
Note: readlink prints the value of a symbolic link or canonical file name.
I like the simplicity of this solution but I found xargs to be much faster. find .. -name «filename» | xargs readlink -f
This worked for me, but will only return the first occurrence.
realpath $(find . -type f -name filename -print -quit)
To get full paths for all occurrences (as suggested by Sergiy Kolodyazhnyy)
find . -type f -name filename -print0 | xargs -0 realpath
Exactly what I needed (just the first occurrence and to print the full path including the filename in the output. Thank you Sir
Try with -printf . This also works with files with blank spaces.
find .. -name «filename» -printf $PWD/»%f\n»
Unless I totally misunderstand, it is as simple as this:
find $(realpath .) -name 'river.jpg'
By specifying the full real path as a start, find will implicitly output this full path as a search result.
The bash command realpath converts the current (or any other directory as ./images) into its real path). the $(realpath .) converts the output to a variable, as if it was typed manually, e.g. /home/myusername
Get Absolute File Path in Linux
Here are different ways to get the absolute file paths in Linux command line.
You can get the full path of a directory with the pwd command:
But how do you get the absolute path of a file in Linux?
There are several ways to print the full path of files:
- readlink
- realpath
- find
- combining ls and pwd
Let me show you these commands one by one. But before that, I suggest brushing up on the basics of the absolute and relative path concept first.
Use readlink to get file path
The purpose of the readlink command is to resolve symbolic links. You can use it to display the full path of a file like this:
[email protected]:~$ readlink -f sample.txt /home/abhishek/sample.txt [email protected]:
Use realpath to get full file path
The realpath command is used for resolving the absolute file names. Among other uses, it can show the full path to a file.
Take a look at this example:
[email protected]:~$ realpath sample.txt /home/abhishek/sample.txt [email protected]:~$
If you use it with a symbolic link, it will show the real path of the original file. You can force it to not follow the symbolic link:
Here’s an example where it showed the full path to the source file by default and then I forced it to show the symbolic link, not its original file.
[email protected]:~$ realpath linking-park /home/abhishek/Documents/ubuntu-commands.md [email protected]:~$ realpath -s linking-park
Use the find command to get the absolute file path
Here’s the thing with the find command. Everything is relative to the directory you give it for search location.
If you give it . it will show the relative path. If you give it the absolute path of the directory, you’ll get the absolute path of the files you are searching for.
Use the command substitution with the find command like this:
You can run it to find the full path of a single file:
[email protected]:~$ find $(pwd) -name sample.txt /home/abhishek/sample.txt
Or, you can use it with a bunch of files matching a certain pattern:
[email protected]:~/Documents/eBooks$ find $(pwd) -name "*.pdf" /home/abhishek/Documents/eBooks/think-like-a-programmer.pdf /home/abhishek/Documents/eBooks/linux-guide.pdf /home/abhishek/Documents/eBooks/absolute-open-bsd.pdf /home/abhishek/Documents/eBooks/theory-of-fun-for-game-design.pdf /home/abhishek/Documents/eBooks/Ubuntu 1804 english.pdf /home/abhishek/Documents/eBooks/computer_science_distilled_v1.4.pdf /home/abhishek/Documents/eBooks/the-art-of-debugging-with-gdb-and-eclipse.pdf
Print full path with the ls command
Now this one is a bit tricky and messy.
You can use the environment variable PWD with ls command like this to display the files and directories with their absolute path:
You get an output like this:
[email protected]:~/test$ ls -ld $PWD/* -r--rw-r-- 1 abhishek abhishek 0 Jul 27 16:57 /home/abhishek/test/file2.txt drwxrwxr-x 2 abhishek abhishek 4096 Aug 22 16:58 /home/abhishek/test/new
However, to print the full path of a file with the ls command, you’ll have to use it like this:
Not the cleanest solution but it works.
[email protected]:~$ ls -l $PWD/sample.txt -rw-r--r-- 1 abhishek abhishek 12813 Sep 7 11:50 /home/abhishek/sample.txt [email protected]:~$
Conclusion
I showed four different ways to get the full file path in Linux. The find and ls commands are common while realpath and readlink are hardly known to many Linux users. It’s always good to learn new things, isn’t it?
How to get full path name of a Linux command
I want to find out the file path of commands in Linux e.g., ls has file path as /bin/ls . How can I find out the exact path of some commands?
5 Answers 5
As pointed out, which would do it. You could also try:
This will list all the paths that contains progName . I.e whereis -b gcc on my machine returns:
gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc
On my machine I have /bin/gcc and /usr/bin/gcc (both of which are links which after many links point to the actual program which is /usr/bin/x86_64-linux-gnu-gcc-9 ), but the former isn’t listed in whereis gcc .
you can use which , it give you path of command:
Well done with the use of type which is generally a builtin, and if not provided by POSIX. type -p (where that option is available) will return a pruned result which can be assigned and used as a command. e.g. myprog=$(type -p someexename) .
You can use the which command. In case of a command in your $PATH it will show you the full path:
mureinik@computer ~ $ which cp /usr/bin/cp
And it will also show details about aliases:
mureinik@computer ~ $ which ls alias ls='ls --color=auto' /usr/bin/ls
Yes you can find it with which command
You did not specify, which shell you are going to use, but I strongly recommend using which , as it does not necessarily do, what you expect. Here two examples, where the result possibly is not what you expect:
(1) Example with bash, and the command echo :
would output /usr/bin/echo , but if you use the echo command in your bash script, /usr/bin/echo is not executed. Instead, the builtin command echo is executed, which is similar, but not identical in behaviour.
(2) Example with zsh, and the command which :
would output the message which: shell built-in command (which is correct, but certainly not a file path, as you requested), while
would output the file path /usr/bin/which , but (as in the bash example) this is not what’s getting executed when you just type which .
There are cases, when you know for sure (because you know your application), that which will produce the right result, but beware that as soon as builtin-commands, aliases and shell functions are involved, you need first to decide how you want to handle those cases, and then choose the appropriate tools depending on the kind of shell which you are using.
How to show the full path of a file or directory in the terminal?
I need to know how the directory name in order to type it out in the terminal. How do I access the names of directories? Windows Explorer used to have a title bar with the full path. Can someone please help me figure out how to see the full path of a certain file?
If you know the path and need to type it out quickly, nothing is better than Tab completion, especially with zsh .
I figured out a way to delete files with BleachBit, thanks for your answer. But for the other query I still need to know how to see the FULL path of any certain file or folder.
3 Answers 3
If you are using nautilus to browse your files, you can toggle the navigation bar by pressing Ctrl + L .
If you are using the terminal, just use pwd to know the absolute path of your current location.
And don’t forget that space characters need to be escaped within the terminal. If you want to access /path/to/the force then you need to do cd /path/to/the\ force .
To display the full path of a file in the terminal just drag the file’s icon into the terminal, and the full path of the file will be displayed enclosed by two apostrophes (single quotation mark characters). It’s that simple.
In Ubuntu 20.04 and later drag and drop of files or directories doesn’t work from the desktop, but does work in other locations including dragging from the desktop in Files file manager.
find can do this quite handily from the terminal. Here’s an example in which I’m looking for the full path of the file Taxes-2013.pdf:
sudo find / -name Taxes-2013.pdf
/home/me/Documents/Taxes-2013.pdf
I’m using sudo so that I can avoid all the permission denied output that I would otherwise get with find when searching from the root of the tree.
If you just want the pathname and want the filename stripped off you can use
sudo find / -name Taxes-2013.pdf | xargs -n1 dirname
Note: If you are in the habit of putting spaces in names this is relevant to you.