Is there an easy way I can print the full path of file.txt ?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The <command>
dir> <command> file.txt
should print
/nfs/an/disks/jj/home/dir/file.txt
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
Use readlink:
|
|||||||||||||||||||||
|
|
This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.
|
||||
|
|
|
In windows you can
|
|
Works on Mac, Linux, *nix: This will give you a quoted csv of all files in the current dir:
The output of this can be easily copied into a python list or any similar data structure. |
|||
|
|
|
For Mac OS, if you just want to get the path of a file in the finder, control click the file, and scroll down to "Services" at the bottom. You get many choices, including "copy path" and "copy full path". Clicking on one of these puts the path on the clipboard. |
|||
|
|
|
I know that this is an old question now, but just to add to the information here: The Linux command
There are some caveats to this; please see https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/. |
|||||
|
This is explanation of what is going on at @ZeRemz's answer:
|
||||
|
|
|
Another Linux utility, that does this job:
|
|||
|
|
|
This works with both Linux and Mac OSX ..
|
|||
|
|
|
I know there's an easier way that this, but darned if I can find it...
|
|||||||||||||||||
|
|
In Mac OSX, do the following steps:
|
||||
|
|
|
the below prints the absolute path of file.txt, which in this example is supposed to be in the current directory.
Let us suppose that file.txt is in the home directory of user foo. All of the below commands are equivalent and they all will return the same /home/foo/file.txt in Linux or /Users/foo/file.txt in OS X:
|
|||
|
|
|
I like many of the answers already given, but I have found this really useful, especially within a script to get the full path of a file, including following symlinks and relative references such as
Which will return the full path of the
I can then use the Hope this helps, Dave |
|||
|
|
|
Usually:
Alternatively, just for the current folder:
|
|||
|
|
|
This will work for both file and folder:
|
|||
|
|
|
the easiest way I found is
it works well for me |
||||
|
|
|
For Mac OS X, I replaced the utilities that come with the operating system and replaced them with a newer version of coreutils. This allows you to access tools like The Homebrew version appends a 'G' (for GNU Tools) in front of the command name -- so the equivalents become Instructions for how to install the coreutils/GNU Tools on Mac OS X through Homebrew can be found in this StackExchange arcticle. NB: The |
|||||
|
|
If you are in the same directory as the file:
Replace |
|||||
|
Copies the text to your clipboard and displays the text on the terminal window. :) (I copied some of the code from another stack overflow answer but cannot find that answer anymore) |
|||
|
|
|
I suppose you are using Linux. I found a utility called
|
|||||||||||||||||
|
|
You could use the fpn (full path name) script:
|
||||
|
|
Will find all the links to the file with the same inode number as file.txt adding a Do note that
Will output:
|
||||
|
|
or
|
|||||
|
|
This will give you absolute path of the file:
|
|||||||||
|
|
You can save this in your "shell.rc" or just put in console
example:
will output
|
|||||||||
|
|
Beside "readlink -f" , another commonly used command:
This also give the full path and file name at console Off-topic: This method just gives relative links, not absolute. The |
|||||
|
|
In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:
For e.g. if the script was launched like this: |
|||||
|
|
The following usually does the trick:
|
|||||||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?