Printing path in linux

How can I generate a list of files with their absolute path in Linux?

I am writing a shell script that takes file paths as input. For this reason, I need to generate recursive file listings with full paths. For example, the file bar has the path:

./foo/bar (from the folder ken) 

It seems like an obvious requirement, but I can’t see anything in the find or ls man pages. How can I generate a list of files in the shell including their absolute paths?

28 Answers 28

If you give find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:

or if your shell expands $PWD to the current directory:

find simply prepends the path it was given to a relative path to the file from that path.

Greg Hewgill also suggested using pwd -P if you want to resolve symlinks in your current directory.

This is helpful, but I think user431529’s response below is more valid: ls -d -1 $PWD/**/* but I guess find $PWD also works (tested in bash)

@Brian Why? find $PWD is simple. The ls incantation is complex and unwieldy (unless you alias it). find is not dependent on shell glob expansions, so will work in any shell. find is also a lot more flexible, I can get a recursive listing of all files, or perhaps of all directories, or maybe I want a listing of all xml files, or all files changed in the last week. All that is possible with find , but not easily with ls .

I don’t get it, this command does not give me the absolute path of the file I, while other answers down on the list does the job.

gives the full absolute path. But if the file is a symlink, you’ll get the final resolved name.

Nice. Does not work on the BSD Variant of readlink (i.e Mac).I use gnucoreutils on mac. And hence can use greadlink which works with the above solution.

if you install bash tools via brew install coreutils , then the executable will be installed as /usr/local/opt/coreutils/libexec/gnubin/readlink

Use this for dirs (the / after ** is needed in bash to limit it to directories):

this for files and directories directly under the current directory, whose names contain a . :

In bash, ** is recursive if you enable shopt -s globstar .

ls -d -1 $PWD/**/* does not recurse. Wish I could take my +1 back. You can do ** for each depth you need to go though.

Читайте также:  Linux для hp proliant

The ** operator is a recursive globbing operator. If you use the command in a shell that supports it (such as zsh), it will work properly.

ls -d1 «$PWD/» to also catch the hidden (dot) files. For me the recursive version in the answer didn’t prepend the absolute path to files in subfolders.

ls -altd ..//**/* is my preference when recursing on all subdirs

That will give the path of the current directory, and all the files and directories below it, as well. That’s probably not what people are looking for.

This looks only in the current directory. It quotes «$PWD» in case it contains spaces.

This is still a very valid answer, but it would be good to include the info that this does not work recursively (which is what I was looking for in fact!)

Command: ls -1 -d «$PWD/»*

This will give the absolute paths of the file like below.

[root@kubenode1 ssl]# ls -1 -d "$PWD/"* /etc/kubernetes/folder/file-test-config.txt /etc/kubernetes/folder/file-test.txt /etc/kubernetes/folder/file-client.txt 

You get list of absolute paths in working directory.

The $PWD is a good option by Matthew above. If you want find to only print files then you can also add the -type f option to search only normal files. Other options are «d» for directories only etc. So in your case it would be (if i want to search only for files with .c ext):

Note: You can’t make an alias for the above command, because $PWD gets auto-completed to your home directory when the alias is being set by bash.

Actually, -d doesn’t mean only directories — it means it treats directories like files. So if you ls -d /home, you’ll get back «/home», not a listing of what’s in /home.

@Travis: he was talking about the option to find, not ls. In his case «find / -type d» would find only directories — as he said.

If you need to specify an absolute path or relative path, you can do that as well

ls -1 $FILEPATH | xargs realpath 

If you give the find command an absolute path, it will spit the results out with an absolute path. So, from the Ken directory if you were to type:

find /home/ken/foo/ -name bar -print 

(instead of the relative path find . -name bar -print )

Therefore, if you want an ls -l and have it return the absolute path, you can just tell the find command to execute an ls -l on whatever it finds.

find /home/ken/foo -name bar -exec ls -l <> ;\ 

NOTE: There is a space between <> and ;

You’ll get something like this:

-rw-r--r-- 1 ken admin 181 Jan 27 15:49 /home/ken/foo/bar 

If you aren’t sure where the file is, you can always change the search location. As long as the search path starts with «/», you will get an absolute path in return. If you are searching a location (like /) where you are going to get a lot of permission denied errors, then I would recommend redirecting standard error so you can actually see the find results:

find / -name bar -exec ls -l <> ;\ 2> /dev/null 

( 2> is the syntax for the Borne and Bash shells, but will not work with the C shell. It may work in other shells too, but I only know for sure that it works in Bourne and Bash).

Читайте также:  Pycharm linux установка centos

Источник

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.

Источник

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:

Читайте также:  Настроить vpn подключение linux

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.

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?

Источник

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