I'm trying to get a listing of all the files in a specific directory (/myfiles) and then sftp them to another server. The number of files will vary and so will their names -- they will look like this: Code Translation Table_c_Primary Relationship Type Code Translation Table_e_Transaction Type

I was trying to put all them into an array then do a while loop (based on count) with an sftp put command. However, I can't seem to get this string into an array.

Here is what I have:

export directory=`find -name *Table_\*`
read -a filearry <<< "${directory}"

During testing only ${filearry[0]} is populated. Output of echo ${filearry[0]} is:

Code Translation Table_c_Primary Relationship Type
Code Translation Table_e_Transaction Type
Code Translation Table_f_Appeal Code
Code Translation Table_g_Campaign Codes
Code Translation Table_h_Designation Code
Code Translation Table_i_Designation Purpose
Code Translation Table_j_Address Types
Code Translation Table_k_Degree of Graduation
Code Translation Table_l_Relationship Type
Code Translation Table_m_Activity Role
Code Translation Table_n_Activity Status Club
Code Translation Table_o_Participation Category
Code Translation Table_p_Activity Status Organization
Code Translation Table_q_Restriciton Code
Code Translation Table_c_Primary Relationship Type

Edit: This needs to be an automated script kicked off by cron. I'm not forced into using sftp, but whatever way I use it has to be secure.

I ended up just simplifying the script to upload all the files in the directory no matter what the name is. I have the output going to a log file which is sent as an text email to a couple people in my office. The format of this email isn't great. All the files are listed on the same line making it hard to read -- Is this fixable?

if [ "$(ls -A $DIR)" ]; 
    printf "=====================================================\n"
    printf " $DIR contains files.\n"
    printf "=====================================================\n"
    sftp username@sftp.server.com:/upload <<EOF
    put -r /myfolder/*
    printf "=====================================================\n"
    printf "Done transfering files.\n"
    printf "=====================================================\n"
    printf "No files to upload in $DIR"

mailx -s 'Document Feed' robert@company.com < /var/log/docfeed.log
  • Do you really need to use sftp or would an alternative such as scp (or even rsync) be acceptable?
    – roaima
    Oct 10, 2017 at 22:09
  • @Robert Are your the owner of the original post?
    – GAD3R
    Oct 11, 2017 at 12:27
  • @Robert It looks like you've created two accounts. I'd advise that you merge them so you don't need your edits to be approved. See unix.stackexchange.com/help/merging-accounts Oct 11, 2017 at 13:38
  • aha! I can make comments now!! Thanks for link Anthony!
    – Robert
    Oct 12, 2017 at 19:57

2 Answers 2


You get an array of filenames like this:

filenames=( *Table_* )

Assuming you can't just use scp to copy all the files with

scp *Table_* user@host:dir/

you can create a batch script for sftp:

printf 'put "%s"\n' *Table_* | sftp user@host:/dir

If you'd like to rename them on the destination, for example replace all spaces with underscores (using the pattern substitution ${parameter//pattern/string}):

for name in *Table_*; do
    printf 'put "%s" "%s"\n' "$name" "${name// /_}"
done | sftp user@host:/dir

Another obvious solution is to create an archive of the relevant file and the transfer this archive to the other host:

tar -cf archive.tar *Table_*

echo 'put archive.tar' | sftp user@host:/dir

I think I'm splitting hair here but fwiw, to accomplish what I originally wanted to do I can use:

 shopt -s nullglob
 for i in "${documentarray[@]}"
 #sftp stuff here


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.