Reputation: 3184
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
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
Reputation: 5072
You could use:
$(dirname "$INVERSE")
Does exactly what you want =)
Also, using regex with sed:
$(echo "$INVERSE" | sed -e 's,/[^/]*$,/,')
Explaining the regex:
On the replacement, we just make sure to not remove the last slash.
Upvotes: 1