How to get the full (with path relative to / ) filename in one command?

It happens, you work in the console, and you need to copy /полный/путь/к/файлу , for example, to use it as an argument to scp in the next console. You have to call pwd to copy the path to the current folder, and ls to copy the file name. Can this be done in one team?

  • For example, find 'pwd'/имя_файла (quotes around pwd are inverse). Or a bunch of options, for example, with readlink , as indicated in the answer below. The number of commands and / or arguments does not have any meaning at all, for there are aliases. - user6550

4 answers 4

readlink -f will show /полный/путь/к/файлу.ext , additionally "reveal" all symbolic links and replace them with "canonical" paths. An example is indicative:

 $ cd /tmp $ mkdir foo $ touch foo/bar.ext $ ln -s foo/bar.ext baz.ext $ readlink -f foo/bar.ext /tmp/foo/bar.ext $ readlink -f baz.ext /tmp/foo/bar.ext 

Here /tmp/baz.ext is a symlink on /tmp/foo/bar.ext .

The proposed solution works in Linux and FreeBSD, but does not work in Mac OS - they have their own atmosphere there .

    I will offer the option a bit more interesting:

     readlink -m файл | tr -d '\r\n' | xsel -b 

    I.e:

    1. We read the full path
    2. Immediately copy it to the clipboard, pre ...
    3. ... by cutting out the final CR / LF (if you don’t want them to be added when pasting)

      For files relative to the current directory

       echo ~+/file 

      or with the same result

       echo $PWD/file 

        you can, for example, substitute what is contained in the $PWD environment variable (or in any other environment variable), even without the “command”, but only with a keyboard shortcut. unless, of course, your shell uses gnu / readline to edit the command line.


        for example, such a command will bind the alt + o keyboard shortcut by substituting the value of the $PWD variable (and a slash at the end for convenience) to the current cursor position:

         $ bind '"\eo": "$PWD/\e\Ce"' 

        here \eo is alt + o (and, for example, \Co is ctrl + o ).

        so that this binding is created automatically in each shell session, you can add the above command, for example, to the start script of the shell being used - ~/.${SHELL##*/}rc .

        or even better, add the line, which in the example above was transferred to the bind shell builtin command, directly to the ~/.inputrc (the conf. file used by gnu / readline ):

         "\eo": "$PWD/\e\Ce" 

        inspired by this answer: Bash: call script with customized keyboard shortcuts?