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
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.
How can you quickly get the complete path to a file for use in terminal?
The downside of readlink is that it will work even if the file doesn’t exist. This can perpetuate bugs in very odd ways.
Just drag and drop the file in the terminal.
@Kupiakos: for me, gnome-terminal happily translates the dropped file path to ‘/home/alexcohn/.gvfs/…’
All good answers; Here is a tip for another situation.
If you are browsing your files using nautilus and you want the complete path of your current directory, then press CTRL+L . This changes the breadcrumb buttons temporarily back to the old-style address bar, allowing you to copy the path.
Exactly what I was looking for, I mean the terminal is a great place to ls but there is those times you work in a file folder views : ‘ )
If it’s an executable, then execute (in a terminal):
In addition to dragging the icon, there are a few ways to get the full path without nautilus (or thunar, konqueror, et al.). You would then triple-click or click-drag and copy, potentially saving this in your clipboard manager*, and paste it where you need.
(pastie, klipper, glippy, glipper, anamnesis)
- You can use find in a directory above your file. (If you don’t know where it is, start where your shell drops you, [generally] in the top of your home directory.)
find . | egrep filename - You can use locate to get the filename. (Run sudo updatedb if that hasn’t been done recently.)
A more realistic example of using find would be something like :
$ find | egrep askubuntu | grep txt ./askubuntu-temp.txt ./drDocuments/web/meta.askubuntu.txt ./other/stuff/askubuntu.txt.iteration.1 ./other/stuff/askubuntu.txt.iteration.2 [. ]
To cut out the ones you don’t like, e.g.:
find | egrep askubuntu | grep txt | egrep -v iteration find | egrep askubuntu | grep txt | egrep -v 'iteration|meta|other'
locate is used much the same way, though grep is frequently more necessary:
locate myfile | egrep home | egrep -v 'mozilla|cache|local|bin|\.pyc|test' | grep \.py
This isn’t the most efficient way to type this, but usually if I’ve lost a file, I do this iteratively, adding grep clauses as I go.
How do I get the absolute directory of a file in Bash?
I have written a Bash script that takes an input file as an argument and reads it.
This file contains some paths (relative to its location) to other files. I would like the script to go to the folder containing the input file, to execute further commands. In Linux, how do I get the folder (and just the folder) from an input file?
Are you giving the full path to the input file or just the path relative to the current working directory?
7 Answers 7
readlink -f relative/path/to/file
To get the directory of a file:
dirname relative/path/to/file
You can also combine the two:
dirname $(readlink -f relative/path/to/file)
If readlink -f is not available on your system you can use this * :
function myreadlink() < ( cd "$(dirname $1)" # or cd "$" echo "$PWD/$(basename $1)" # or echo "$PWD/$" ) >
Note that if you only need to move to a directory of a file specified as a relative path, you don’t need to know the absolute path, a relative path is perfectly legal, so just use:
cd $(dirname relative/path/to/file)
if you wish to go back (while the script is running) to the original path, use pushd instead of cd , and popd when you are done.
* While myreadlink above is good enough in the context of this question, it has some limitation relative to the readlink tool suggested above. For example it doesn’t correctly follow a link to a file with different basename .