Home > Enterprise >  How to get two outputs from a bash find exec command
How to get two outputs from a bash find exec command

Time:09-25

I host a photo gallery. There is a command you run to automatically add content. It looks like this;

php artisan lychee:sync /path/to/import --album_id="album ID"

This command needs two variables;

  1. path to import
  2. Album ID

I’m running Find against a directory to return any sub directories added within the last 5 days;

find /mnt/Pictures -mtime -5 -mindepth 1
/mnt/Pictures/Paris Trip
/mnt/Pictures/Spain
/mnt/Pictures/America

Now, I can invoke the script to add this content using the -exec function in Find, but the value of {} is the path to import. I need both the path to import AND the name of the directory; such as

php artisan lychee:sync /mnt/Pictures/Spain --album_id="Spain"

This is where I am so far;

find /mnt/Pictures -mtime -5 -mindepth 1 -exec php artisan lychee:sync {} \;

Any ideas?

CodePudding user response:

Just add an sh call to the mix:

find /mnt/Pictures -mtime -5 -mindepth 1 -exec \
    sh -c 'php artisan lychee:sync "$1" --album_id="$(basename "$1")"' _ {} \;

CodePudding user response:

You can use the shell for extracting the name of the directory and using it along with the full path as command arguments.

find ... -exec sh -c '
for d; do
  php artisan lychee:sync "$d" --album_id="${d##*/}"
done' sh {}  

CodePudding user response:

one option is to use a bash loop as so:

find /mnt/Pictures -mtime -5 -mindepth 1 -print0 | \
   while IFS= read -r -d '' pth; do  \
      php artisan lychee:sync "${pth}" --albumId="${pth##*/}"; \
   done;
  • Related