Peter
Peter

Reputation: 3184

LFTP Put file in specific directory?

I'm working on the following bash script that I need to upload specific files that are over 10 MB:

#!/bin/sh

FTP_HOST=ftp.ftphost.com
FTP_USER=myftpuser
FTP_PASS=securepassword

[[ -n "$1" ]] || { echo "Usage: findlarge [PATHNAME]"; exit 0 ; }
FILES=`find "$1" -type f -size +10000k -printf '%p;'`

OLD_IFS="$IFS"
IFS=";"
FILES_ARRAY=( $FILES )
IFS="$OLD_IFS"

for fl in "${FILES_ARRAY[@]}"
 do
   INVERSE=`${fl//\/var\/www\/html/\/public_html}`
   lftp -u $FTP_USER,$FTP_PASS -e "cd '${INVERSE}'; put '${fl}';quit" $FTP_HOST
 done

It's used for a specific CDN that needs larger files uploaded to their servers what it's attempting to do and where I don't know how to continue is the files on the host side are found at /var/www/html/ but the remote server needs them placed at /public_html/. I need the same file structure on the remote server as I do on the host server.

The issue that I'm running into is $INVERSE still includes the actual file name and the path.. I need a way to trim off the filename from the INVERSE variable so I can properly place files in the correct directory.

Does anybody know of a regex statement that can take something like:

/public_html/path/to/file/filename.zip

and change it to

/public_html/path/to/file/

Upvotes: 0

Views: 2264

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185630

With bash parameter expansion :

$ x=/public_html/path/to/file/filename.zip
$ echo ${x%/*}/
/public_html/path/to/file/

It have advantage of using a bash built in, so it's very light.

See http://wiki.bash-hackers.org/syntax/pe

Upvotes: 1

You could use:

$(dirname "$INVERSE")

Does exactly what you want =)

Also, using regex with sed:

$(echo "$INVERSE" | sed -e 's,/[^/]*$,/,')

Explaining the regex:

  1. Match a slash
  2. Match as many characters that aren't slashes as you can
  3. Match the end of the line

On the replacement, we just make sure to not remove the last slash.

Upvotes: 1

Related Questions