Linux show full path to file

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.

Читайте также:  Python ide linux i386

Источник

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:

Get file path in Linux

  • 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.

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

Now this one is a bit tricky and messy.

Читайте также:  Command for linux time

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 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.

Читайте также:  Finding filename in linux

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 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 .

Источник

Оцените статью
Adblock
detector