0

I'm using this solution https://stackoverflow.com/a/10341883/6400605 to get a list of files from a specified folder, with a predefined -name variable, but it does not work when supliying a directory argument.

#!/bin/bash
if [ -n "$1" ] && [ -f "$1" ] ; then snames=$(basename "$1") ; fi
if [ -n "$1" ] && [ -d "$1" ] ; then snames=( '\* address.txt' -o -name '\*.log' -o -name '\*.rtf' -o -name '\* @unit credentials.doc' ) ; fi
start="$(dirname "$1")";
find "${start}/" -type f \( -name "${snames[@]}" \) -print0 | xargs -r0 -n1 bash -c '
echo "$2"
# ....more commands below 
' _  \;

This works when providing a path+filename, however does not work when providing a directory argument.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
jkeys
  • 113
  • 8
  • Drop single quotes **or** don't escape stars: `snames=( '*address.txt' -o -name '*.log')` – F. Hauri - Give Up GitHub Aug 08 '21 at 07:56
  • What does `dirname /foo/bar/` return? – Shawn Aug 08 '21 at 07:58
  • @F. Hauri stars are required for find to process them as per the other thread solution. I need some kind of quotes to ensure proper filename and special characters in filenames (like @, #, $, etc.) that doesn't break the script - I guess I can do a substitute command to \ them – jkeys Aug 08 '21 at 08:10
  • @jkeys Read [this answer](https://stackoverflow.com/a/68698997/1765658) I just post with a *spaced filename* as sample. This syntax work with any kind of file names (spaced, with UTF-8 characters and or ISO-8859...) – F. Hauri - Give Up GitHub Aug 08 '21 at 08:21

0 Answers0