This is a cygwinshell answer (bash is an advanced shell that should be reserved for when standard Posix shell (/bin/sh
) is insufficient). Note that slashes are reversed intentionally.
I see the format in your list.txt
is delimited with commas and whitespace. I am going to assume that this is literal and the reason none of what you've tried so far works. Therefore, I am parsing it with the explicit assumption that comma and then space (,
) is a delimiter and that there is no way to escape them (e.g. if you have a file named apples, oranges.txt
then my code would erroneously parse files named apples
and oranges.txt
).
#!/bin/sh
main_folder="${1:-//internal.company.com/project folder}"
my_folder="${2:-c:/_Masoud/files}"
cd "$main_folder" || exit $?
IFS=', ' find $(cat list.txt) -maxdepth 1 -name \*.xlsx |while IFS= read xlsx; do
mkdir -p "$my_folder/${xlsx%/*}"
cp -a "$xlsx" "$my_folder/$xlsx"
done
I've done some extra work for you to make this more abstract. $main_folder
is taken from your first argument (a missing argument will default to //internal.company.com/project folder
) and $my_folder
is taken from your second argument (if missing, it defaults to c:/_Masoud/files
). Don't forget to quote your command-line arguments if they contain spaces or interpretable characters.
After determining your source and destination, I then try to change directories to the source directory. If this fails, the script will stop with the same exit code.
Now for the loop. I've changed the Input Field Separator ($IFS
) to be the comma and space (,
) we talked about earlier and then glued the contents of list.txt
into its arguments, followed by the requirement of being one level deep (to include PT_NAK05/foobar/baz.xlsx
, use -maxdepth 2
or just remove that clause altogether to view the file tree recursively), followed by the requirement of a name matching *.xlsx
(this is escaped because your shell would otherwise assume you're talking about the local directory). The output of this is then read into a loop line by line as $xlsx
. We recreate the target's parent directory in the new target destination if it's not already present, then we copy the file to that location. cp -a
preserves permissions and time stamps.