Get full path to file linux

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?

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.

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

Provides the output:

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.

Источник

Getting the Absolute (Full) and Relative Path In Linux

This article explains absolute paths and how they differ from relative paths, getting them, and how symbolic links are handled.

FileSystem Paths

A path is the location of a file in a file system. It’s the directions to the file in the folder it is located.

A path consists of a string of characters. Some represent directory names, and a separator character separates the directory names from the file name and extension.

Consider the below path:

  • Forward slashes (/) to separate directories from their subdirectories
  • Directory Names – the text between the forward slashes
  • The file name – in this case, file.txt

Relative Paths

Relative paths are paths that are defined in relation to your current position in the file system.

You can find your current position using the pwd command:

Relative paths begin without a forward slash, or a . or ...

  • Paths beginning without a / or with a . start in the current directory
  • Paths beginning with .. start in the directory above the current directory (the parent of the current directory)

Example

If you are in the directory /home/user:

…and it contains a file called test.txt, you need only type:

…to view the contents of the file, as you are in the same directory as that file, and can access it using its relative path.

In the above example, the cd command is used to change the directory, and the cat command reads the contents of the file to the screen.

Absolute Paths

Absolute paths can be used from anywhere on the filesystem. They represent the full path of the file from the root (the very top) of the file system hierarchy. They are absolute because it’s the absolute location of the file on the file system. It is not relative to where you are or where you might be; the path will work when called from any location.

Читайте также:  Как сделать установку windows по сети

Example

Consider again that we are in the directory /home/user:

…and there is a file called another.txt located at /var/temp. Attempting to open it by running:

…will fail because that file doesn’t exist at /home/user. We can, however, access it at its absolute path:

As it provides the full location of the file and does not rely on the relative path we are currently situated.

Soft links (symlinks) are just files that point to another file. The absolute path is still the path to the symlink. If you want the path to the linked file itself, you will need to use the readlink command to find the absolute path of the linked file:

Hard links are also absolute paths – the data exists at multiple absolute paths but in only one location on the physical disk.

Brad Morton

I’m Brad, and I’m nearing 20 years of experience with Linux. I’ve worked in just about every IT role there is before taking the leap into software development. Currently, I’m building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I’m up to.

Источник

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:

you can use which , it give you path of command:

you can use type :

You can use the which command. In case of a command in your $PATH it will show you the full path:

And it will also show details about aliases:

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 obtain the absolute path of a file via Shell (BASH/ZSH/SH)?

Question: is there a simple sh/bash/zsh/fish/. command to print the absolute path of whichever file I feed it?

Usage case: I’m in directory /a/b and I’d like to print the full path to file c on the command-line so that I can easily paste it into another program: /a/b/c . Simple, yet a little program to do this could probably save me 5 or so seconds when it comes to handling long paths, which in the end adds up. So it surprises me that I can’t find a standard utility to do this — is there really none?

Here’s a sample implementation, abspath.py:

22 Answers 22

Try readlink which will resolve symbolic links:

Forget about readlink and realpath which may or may not be installed on your system.

Expanding on dogbane’s answer above here it is expressed as a function:

you can then use it like this:

Читайте также:  Windows 10 не видит ssd диск через usb

How and why does it work?

The solution exploits the fact that the Bash built-in pwd command will print the absolute path of the current directory when invoked without arguments.

Why do I like this solution ?

It is portable and doesn’t require neither readlink or realpath which often does not exist on a default install of a given Linux/Unix distro.

What if dir doesn’t exist?

As given above the function will fail and print on stderr if the directory path given does not exist. This may not be what you want. You can expand the function to handle that situation:

Now it will return an empty string if one the parent dirs do not exist.

How do you handle trailing ‘..’ or ‘.’ in input ?

Well, it does give an absolute path in that case, but not a minimal one. It will look like:

If you want to resolve the ‘..’ you will need to make the script like:

This is better than readlink -e FILE or realpath , because it works even if the file doesn’t exist.

This relative path to absolute path converter shell function

  • requires no utilities (just cd and pwd )
  • works for directories and files
  • handles .. and .
  • handles spaces in dir or filenames
  • requires that file or directory exists
  • returns nothing if nothing exists at the given path
  • handles absolute paths as input (passes them through essentially)

Note: This is based on the answers from nolan6000 and bsingh, but fixes the file case.

I also understand that the original question was about an existing command line utility. But since this seems to be THE question on stackoverflow for that including shell scripts that want to have minimal dependencies, I put this script solution here, so I can find it later 🙂

The find command may help

Lists all the files in or below the current directory with names matching the pattern. You can simplify it if you will only get a few results (e.g. directory near bottom of tree containing few files), just

I use this on Solaris 10, which doesn’t have the other utilities mentioned.

If you don’t have readlink or realpath utilities than you can use following function which works in bash and zsh (not sure about the rest).

This also works for nonexistent files (as does the python function os.path.abspath ).

Unfortunately abspath ./../somefile doesn’t get rid of the dots.

Here’s a zsh-only function that I like for its compactness. It uses the ‘A’ expansion modifier — see zshexpn(1).

There is generally no such thing as the absolute path to a file (this statement means that there may be more than one in general, hence the use of the definite article the is not appropriate). An absolute path is any path that start from the root «/» and designates a file without ambiguity independently of the working directory.(see for example wikipedia).

A relative path is a path that is to be interpreted starting from another directory. It may be the working directory if it is a relative path being manipulated by an application (though not necessarily). When it is in a symbolic link in a directory, it is generally intended to be relative to that directory (though the user may have other uses in mind).

Hence an absolute path is just a path relative to the root directory.

A path (absolute or relative) may or may not contain symbolic links. If it does not, it is also somewhat impervious to changes in the linking structure, but this is not necessarily required or even desirable. Some people call canonical path ( or canonical file name or resolved path ) an absolute path in which all symbolic links have been resolved, i.e. have been replaced by a path to whetever they link to. The commands realpath and readlink both look for a canonical path, but only realpath has an option for getting an absolute path without bothering to resolve symbolic links (along with several other options to get various kind of paths, absolute or relative to some directory).

This calls for several remarks:

  1. symbolic links can only be resolved if whatever they are supposed to link to is already created, which is obviously not always the case. The commands realpath and readlink have options to account for that.
  2. a directory on a path can later become a symbolic link, which means that the path is no longer canonical . Hence the concept is time (or environment) dependent.
  3. even in the ideal case, when all symbolic links can be resolved, there may still be more than one canonical path to a file, for two reasons:
    • the partition containing the file may have been mounted simultaneously ( ro ) on several mount points.
    • there may be hard links to the file, meaning essentially the the file exists in several different directories.
Читайте также:  Bootmgr ошибка при установке windows

Hence, even with the much more restrictive definition of canonical path , there may be several canonical paths to a file. This also means that the qualifier canonical is somewhat inadequate since it usually implies a notion of uniqueness.

This expands a brief discussion of the topic in an answer to another similar question at Bash: retrieve absolute path given relative

My conclusion is that realpath is better designed and much more flexible than readlink . The only use of readlink that is not covered by realpath is the call without option returning the value of a symbolic link.

Источник

How do I list files with full paths in Linux?

In Linux, is there an equivalent to Dir /s /a /b where the full path and filename is listed? I’m new to Linux, and without a GUI, I want to get an idea of the structure of what’s on the hard disk.

6 Answers 6

Use the find command. By default it will recursively list every file and folder descending from your current directory, with the full (relative) path.

If you want the full path, use: find «$(pwd)» .

  • If you want to restrict it to files or folders only, use find -type f or find -type d , respectively.
  • If you want it to stop at a certain directory depth, use find -maxdepth 2 , for example.

Read Finding Files for an extensive manual on GNU find , which is the default on Linux.

Notes:

  • . : current folder
  • remove -maxdepth 1 to search recursively
  • -type f : find files, not directories ( d )
  • -not -path ‘*/\.*’ : do not return .hidden_files
  • sed ‘s/^\.\///g’ : remove the prepended ./ from the result list

For completeness, the ls -lR / command will list the name of each file, the file type, file mode bits, number of hard links, owner name, group name, size, and timestamp of every file (that you have permission to access) from the root directory down. ( l is for long list ie all that info, R is to recurse through directories, / starts at the root of the filesystem.)

There are a number of params to make the output info closer to dir /S /A , but I have to admit I can’t work out how to translate the /B .

For useful info, I would try: ls -lAFGR —si /

  • l = long list (as mentioned above)
  • A = almost all files (doesn’t include . and .. in each dir, but does show all hidden files)
  • F = show file indicator, (one of * for exe files, / for directories, @ for symbolic links, | for FIFOs, = for sockets, and > for doors)
  • G = don’t show group info (remove this if you want to see it)
  • R = recursively list directories (subdirectories) and
  • —si = show the file size in human readable eg 1M format (where 1M = 1000B)

ls can provide an easier to read synopsis of directories and files within those directories, as find ‘s output can be difficult to scan when files are contained within really long directory structures (spanning multiple lines). The corollary is that each file is listed on its own (ie without directory path information) and you may have to go back a couple of pages/screens to find the directories a particular file is located in.

Also, find doesn’t contain the /A information in the DIR command. I’ve suggested a number of attributes in the command I’ve shown (that coincidentally show the extra usefulness that you get from linux over a certain proprietary system), but if you read the man and info pages on ls , you will be able to see what to include or not.

Источник

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