How to get full path of a file?

后端 未结 30 3022
走了就别回头了
走了就别回头了 2020-12-02 03:22

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

相关标签:
30条回答
  • 2020-12-02 03:49

    On Windows:

    • Holding Shift and right clicking on a file in Windows Explorer gives you an option called Copy as Path. This will copy the full path of the file to clipboard.

    On Linux:

    • You can use the command realpath yourfile to get the full path of a file as suggested by others.
    0 讨论(0)
  • 2020-12-02 03:50

    I know there's an easier way that this, but darned if I can find it...

    jcomeau@intrepid:~$ python -c 'import os; print(os.path.abspath("cat.wav"))'
    /home/jcomeau/cat.wav
    

    jcomeau@intrepid:~$ ls $PWD/cat.wav
    /home/jcomeau/cat.wav
    
    0 讨论(0)
  • 2020-12-02 03:50

    If you are in the same directory as the file:

    ls "`pwd`/file.txt"
    

    Replace file.txt with your target filename.

    0 讨论(0)
  • 2020-12-02 03:50

    I know that this is an old question now, but just to add to the information here:

    The Linux command which can be used to find the filepath of a command file, i.e.

    $ which ls
    /bin/ls
    

    There are some caveats to this; please see https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/.

    0 讨论(0)
  • 2020-12-02 03:50
    find / -samefile file.txt -print
    

    Will find all the links to the file with the same inode number as file.txt

    adding a -xdev flag will avoid find to cross device boundaries ("mount points"). (But this will probably cause nothing to be found if the find does not start at a directory on the same device as file.txt)

    Do note that find can report multiple paths for a single filesystem object, because an Inode can be linked by more than one directory entry, possibly even using different names. For instance:

    find /bin -samefile /bin/gunzip -ls
    

    Will output:

    12845178    4 -rwxr-xr-x   2 root     root         2251 feb  9  2012 /bin/uncompress
    12845178    4 -rwxr-xr-x   2 root     root         2251 feb  9  2012 /bin/gunzip
    
    0 讨论(0)
  • 2020-12-02 03:52

    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 readlink -f (for absolute path to files) and realpath (absolute path to directories) on your Mac.

    The Homebrew version appends a 'G' (for GNU Tools) in front of the command name -- so the equivalents become greadlink -f FILE and grealpath DIRECTORY.

    Instructions for how to install the coreutils/GNU Tools on Mac OS X through Homebrew can be found in this StackExchange arcticle.

    NB: The readlink -f and realpath commands should work out of the box for non-Mac Unix users.

    0 讨论(0)
提交回复
热议问题