I have a bash script which reads a file extension and list files of that extension using 'find' command. After that it reads name of the file. Now after this how can I open that file using name and extension variables if the file is not present in the current directory. This is the script:

find . -type f -name "*.png"
find . -type f -name "*.jpg"
echo "Enter file extension"
read xt
find . -type f -name "*.$xt"

echo "Enter file name"
read fn
xdg-open $fn.$xt

First it lists PNG and jpg files. It'll again print one list after we enter file extension we desire. After that it reads name of file but if the file is not in current directory how it can open it?

  • How does it read the name of the file? Show us your script, or at least a minimal part of it. – muru Mar 20 '16 at 7:45
  • Inserted the script above. Please have a look. – Akash Kamble Mar 20 '16 at 8:27
  • OK. If it's not in the current directory, do you know which directory it is in? You could run something like: find . -type f -name "$fn.$xt" -exec xdg-open {} \;, but I am not sure what you're trying to accomplish with this script. – muru Mar 20 '16 at 8:29

If you want to find a file and then run a command on it, use find's -exec option:

find . -type f -name "$fn.$xt" -exec xdg-open {} \;

This will call xdg-open once on each matching file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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