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.
2
  • filename have spaces this is the issue. try find . -type f -name '2018-05-24*18-46-31' -exec xdg-open {} \;
    – francois P
    Jun 3, 2018 at 19:32
  • Good suggestion, will test it.
    – bit
    Jun 3, 2018 at 19:54

1 Answer 1

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.)

4
  • 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 3, 2018 at 19:52
  • 1
    @MyWrathAcademia See here for an explanation of find -exec: unix.stackexchange.com/questions/389705/…
    – Kusalananda
    Jun 3, 2018 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 3, 2018 at 20:19
  • @Kusalananda thanks, exactly what I was looking for.
    – bit
    Jun 3, 2018 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.