What you can do, is use find
to print filename using -name
or -iname
flag. xclip
takes input via stdin, so all you have to do is to send the filename via pipe.
$ find -maxdepth 1 -type f -name 'Abraham*Operating Systems*pdf' -printf '%P\n' | xclip -sel clip
The -name
/-iname
flags use simple pattern matching, and -printf
with with %P
format specifier will output just the filename. Note that find
assumes current working directory .
if no directory is specified.
As for your original command
$ ls | head -n 10 | tail -n 5 | xargs xclip -sel clip
there's a few problems with it. One is parsing ls
(reading it's output via command), which is generally not recommended. The output may contain color control characters and other information. In fact there were proposals to add an option flag to ls
so that it can output items separated by \0
character (this is the safe method), but that has been rejected by GNU developers for obvious reasons:
However ls is really a tool for direct consumption by a human, and in that case further processing is less useful. For futher processing, find(1) is more suited.
Another problem with your original command is xargs
usage here. xclip
can read input from stdin just fine, so if you want to send some text to clipboard, it is easy enough to just do something like echo foo | xclip -sel clip
. If you want to copy contents of a file, then you should make that file stdin
xclip -sel clip < /etc/passwd
Copying filename in command-line can be problematic because of special characters such as tabs, newlines, spaces. Generally in command-line you'd use find
. Filemanagers and GUI tools use URI form of filenames, where special characters and UTF-8 symbols are replaced with hex values. For
example,
$ gio info --attributes='uri:' 文er-\ 林中鸟\ -\ 林中鳥-YY神曲-uUX0sZHQMkw.mp3 | awk '/uri:/{print $2}'
file:///home/xie/%E6%96%87er-%20%E6%9E%97%E4%B8%AD%E9%B8%9F%20-%20%E6%9E%97%E4%B8%AD%E9%B3%A5-YY%E7%A5%9E%E6%9B%B2-uUX0sZHQMkw.mp3
This can be passed to xclip
and later pasted into web browser's address bar. Nautilus and File Selection dialogs don't seem to support pasting that from plain-text clipboard though.
See also
xargs
– steeldriver Mar 15 '19 at 10:51