0

I want to open an image from a specific date and time inside the terminal. I located the image with the following command:

# find . -type f -name '*2018-05-24*' | grep 18-46-31
./Screenshot from 2018-05-24 18-46-31.png

Now I want to open this image from the output above in the terminal but the command below doesn't seem to work.

# find . -type f -name '*2018-05-24*' | grep '18-46-31' | xargs xdg-open

What am I doing wrong? I used xargs(may be I'm using xargs wrong) to try to pass the output from grep as an option to xdg-open but I'm getting:

xdg-open: unexpected argument 'from'
Try 'xdg-open --help' for more information.
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
bit
  • 1,106

1 Answers1

4

xargs, by default, splits the input on any whitespace, so in the end, it runs xdg-open with the four arguments ./Screenshot, from, 2018-05-24, 18-46-31.png. With GNU xargs you could use xargs -d '\n' to have it split on newlines instead, but you could do the whole thing with find:

find . -type f -name '*2018-05-24*18-46-31*' -exec xdg-open {} \;

(Strictly speaking, that's not the same as your find+grep combination, since the pattern forces the date and time to be in this order, and the time also has to be part of the filename itself. find -name -name '*2018-05-24*' | grep 18-46-31 would also match ./18-46-31/2018-05-24.png.)

ilkkachu
  • 138,973
  • thanks I will try your suggestion but I don't fully understand what your'e doing with the middle widlcard *, -exec, {} and the backslash. Can you update your answer with an explanation of what these mean and what they do? Btw, isn't there a way that my command using pipe could be modified to make this work. – bit Jun 03 '18 at 19:52
  • 1
    @MyWrathAcademia See here for an explanation of find -exec: https://unix.stackexchange.com/questions/389705/understanding-the-exec-option-of-find – Kusalananda Jun 03 '18 at 20:13
  • @MyWrathAcademia, the asterisks just mean "any number of any characters", so -name '*2018-05-24*18-46-31*' matches filenames with 2018-05-24 and 18-46-31 and anything between, before and after them. You could swap a space in place of the middle *, if you know your filenames have just the space between the date and the time. – ilkkachu Jun 03 '18 at 20:19
  • @Kusalananda thanks, exactly what I was looking for. – bit Jun 03 '18 at 20:36