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.

4
  • Drop single quotes or don't escape stars: snames=( '*address.txt' -o -name '*.log') Commented Aug 8, 2021 at 7:56
  • What does dirname /foo/bar/ return? Commented Aug 8, 2021 at 7: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 Commented Aug 8, 2021 at 8:10
  • @jkeys Read this answer 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...) Commented Aug 8, 2021 at 8:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.