1

I have cobbled together a Bash script to randomoly select an image from multiple sub directories and it works well.

I now need to have two .txt files containing the image name and the directory name the image came from. The contents of the two txt files will be inserted into an article on a Joomla website.. Image source is multiple directories in: /home/my-dir/public_html/images/archive-pics/

The destination directory for random image and the two .txt files is : /home/my-dir/public_html/images/archive-pic-of-day/

The desired format in the .txt files is: " 005.jpg " in the name.txt file and " outside-buildings " in the dir.txt file ... without the quotes. The image source is ....archive-pics/outside-building/005.jpg.

Any suggestions on the code needed to create the two .txt files please when the random image is selected. I'm not a coder but I can follow instructions.

My script to select the image is:

#!/usr/bin/bash
#pic-of-day.sh: Randomly picks an image

#  /bin/bash /home/my-dir/pic-of-day.sh ...  for cron
# first, delete the existing image in the pic of the day folder
rm /home/my-dir/public_html/images/archive-pic-of-day/*.jpg

# now, look through the archive images and pick one to copy to the
# pic of the day folder
cp "$(find /home/my-dir/public_html/images/archive-pics/ -iname '*.jpg' \
        -print0 | shuf -z -n1)" \
    /home/my-dir/public_html/images/archive-pic-of-day/ 

Thank you for your comment @Pourko ..... perhaps I didn't explain very well ...

The image is displayed on a website page via an article container which will have an accompanying descriptive text boiler plate. Because we have multiple subdirectories containing the images, I want to insert text at an appropriate point in the boiler plate that identifies where the event had occurred. All the subdirectories are labeled as the event. A simplistic example is: blah, blah ... this project took place in the [blacksmith-shop] over several years blah, blah. The text within the [ ] would be the subdirectory name containing the image. I do not want the entire path nor do I want the file name at that point. The story itself will be either above or below the image.

To further complicate the issue, I have to use a Joomla extension to import the content into the article and the extension recognizes .txt files. That is why I thought two simple .txt files containing the data would work like a charm.

Right now, I don't need the file name but I can forsee that we may need it in the future. A new image along with a story is displayed daily. I'm sorry, but I don't see how your solution would work into my article.

4

You can simply create a symbolic link to the randomly chosen file. That way you don't need to copy the files.

pic_dir='/home/my-dir/public_html/images/archive-pics'
link_dir='/home/my-dir/public_html/images/archive-pic-of-day'

rand_pic="$(find "$pic_dir/" -type f -iname '*.jpg' -print0 | shuf -z -n1)"
ln -sf "$rand_pic" "$link_dir/pic_of_the_day.jpg"

# to extract just the names, and save them in text files...
basename -- "$rand_pic" > "$link_dir/pic.txt"
basename -- "$(dirname -- "$rand_pic")" > "$link_dir/dir.txt"
4
  • thank you ... I tried to comment but too many characters .....newbie !!! I've appended to the question and deleted my answer post.... – rbur Apr 12 at 1:29
  • This is more clear now. Fixed the answer. – Pourko Apr 12 at 1:38
  • @Pourko ...well ... 2/3 of the way there. I think the ln command is creating a symbolic link which I cannot use in the Joomla article. I can however link to a actual image in the pic-of-day directory. In my cp code sample, I copy the image which I can leave as it's native file name (009.jpg) or save it as a consistent [random.jpg] file name. If you can provide the actual image that way ... I'm a happy camper. thanks for your help. – rbur Apr 12 at 14:47
  • HOORAY ... I did it.... I added a [ && cp $rand_pic "$link_dir/random.jpg" ] to the end of your "rand_pic=...." command to copy the image to the directory. I now have all three files and it seems to work perfectly. Couple days of actual live testing will prove it. Thank you very much @Pourko for your guidance. – rbur Apr 12 at 16:26

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.