Home > Net >  I am trying to write a shell script to replace the path with an empty string
I am trying to write a shell script to replace the path with an empty string

Time:04-26

path=/user/macintosh/git/branch/packages/main/media/file.txt

I wanted to replace /user/macintosh/git/branch/packages with empty string "".

So that I should get the output like main/media/file.txt how can we achieve it with bash scripting?

CodePudding user response:

prefix="/user/macintosh/git/branch/packages/"
string="/user/macintosh/git/branch/packages/main/media/file.txt"
foo=${string#"$prefix"}
echo "${foo}"

Take a look at the documentation for the Shell Parameter Expansion

CodePudding user response:

It worked. Thanks again for your solution for this which makes it worked.

I used

#!/usr/bin/env bash
prefix="/user/macintosh/git/branch/packages/"
files=$(find "$prefix" -type f) 
for foo in $files; do 
  path=${foo#"$prefix"}
  aws s3 cp "$foo" "s3://mybucket$path" 
done
  • Related