Linux get file directory

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

Читайте также:  Rar как установить linux

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.

Источник

Get the Full Path of a File in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Files and directories are building blocks of an operating system. As Linux users, we perform a variety of operations on the files and directories. One such operation is finding a full path of a file. The full path of a file is also known as its absolute or canonical path.

In this tutorial, we’ll discuss various ways to find the full path of a file.

2. Setup

Let’s create files and directories structure to use as an example:

$ cd /tmp $ mkdir -p dir1/dir2/dir3/dir4/dir5 $ touch dir1/dir2/file2.txt $ touch dir1/dir2/dir3/dir4/file4.txt $ touch dir1/dir2/dir3/dir4/dir5/file5.txt $ tree /tmp/dir1/ /tmp/dir1/ └── dir2 ├── dir3 │ └── dir4 │ ├── dir5 │ │ └── file5.txt │ └── file4.txt └── file2.txt 4 directories, 3 files

The readlink command prints canonical file names. We can use the -f option of this command to find the full path of a file:

$ cd /tmp/dir1/dir2/dir3/dir4/dir5/ $ readlink -f file5.txt /tmp/dir1/dir2/dir3/dir4/dir5/file5.txt

4. Using the realpath Command

Alternatively, we can use the realpath command to get the absolute path of a file:

$ cd /tmp/dir1/dir2/dir3/dir4/ $ realpath file4.txt /tmp/dir1/dir2/dir3/dir4/file4.txt

5. Using the Combination of basename and dirname Commands

The basename command is useful when we want to strip the directory and suffix from filenames. Similarly, we can use the dirname command to strip the last component from the file name:

$ basename /tmp/dir1/dir2/dir3/dir4/file4.txt file4.txt $ dirname /tmp/dir1/dir2/dir3/dir4/file4.txt /tmp/dir1/dir2/dir3/dir4

We can use the combination of these two commands to find the full path of a file. Let’s create a simple shell script for the same:

$ cat get_full_path.sh #! /bin/bash echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")" $ chmod +x get_full_path.sh $ ./get_full_path.sh file4.txt /tmp/dir1/dir2/dir3/dir4/file4.txt

Firstly, we use the dirname command to find the directory in which a file is located. Then we change the directory using the cd command.

Next, we print the current working directory using the pwd command. Here, we have applied the -P option to show the physical location instead of the symbolic link.

Читайте также:  Linux отправить post запрос

Finally, we use the basename command to print the file name without a directory.

6. Using the find Command

The find command searches for files in a directory hierarchy. We can use this command to print the absolute path of a file:

$ cd /tmp/dir1/ $ find $PWD -type f -name file4.txt /tmp/dir1/dir2/dir3/dir4/file4.txt 

7. Conclusion

In this article, we discussed various practical examples to find the absolute path of a file. First, we discussed the usage of the readlink and realpath commands. Then, we used a combination of the basename and dirname commands. Finally, we saw the example of the find command. We can use these commands in day-to-day life to boost our productivity.

Источник

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.

Читайте также:  Sharing file system on linux

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