Ever been in the situation where things would have been a little easier had we known a command that prints the absolute path to a file you’re viewing in the current directory? I have and I clumsily kept using
pwd and copy-pasted that and then copy-pasted the filename, until I discovered this command -
readlink is a nifty little tool that comes installed by default on Linux boxes (atleast in Ubuntu 16.04 & CentOS 7). The
man page description reads - print resolved symbolic links or canonical file names , and it does that job perfectly!
Use it on a file and it’ll display the absolute path to it. Example,
$ readlink -f examplefile /home/test/temp/exampledir/examplefile
You can also use it with nested links to find out the actual file where the chain ends. For example,
if you have the following structure -
link1 -> link2 -> file ,
readlink will tell you the final destination when you query
$ ls -la total 8 drwxrwxr-x 2 nitin nitin 4096 Apr 20 21:00 . drwxrwxr-x 3 nitin nitin 4096 Apr 20 21:00 .. lrwxrwxrwx 1 nitin nitin 8 Apr 20 21:00 link3 -> ../link2 $ ls -la ../link2 lrwxrwxrwx 1 nitin nitin 15 Apr 20 21:00 ../link2 -> /tmp/final_file $ readlink -f link3 /tmp/final_file
And, its as easy as that! YAY! no more copy-pasting
ls commands for me!