0

I'm copying like this:

cp "$input_file_absolut_path" "$output_directory_absolut_path"

Now I want to do something with the copied file, but i CAN'T just:

copy_absolut_path="$output_directory_absolut_path/$input_file_absolut_path"

Any tip to obtain the absolut path of the copied file?

  • New Stackoverflow users should be forced to read all posts with more than 1000 upvotes: https://stackoverflow.com/a/965072/402322 – ceving Jun 08 '17 at 08:33

3 Answers3

1

No, you have to use the $input_file_absolut_path. But since you copied only the file, not the whole directory structure into $output_directory_absolut_path, you have to do it like this:

copy_absolut_path="$output_directory_absolut_path/$(basename $input_file_absolut_path)"

basename will get you just the filename without the directory structure. Then you concatenate it with the output_directory_absolut_path.

fancyPants
  • 50,732
  • 33
  • 89
  • 96
0

if your filename doesn't contain slash, you can concatenate $output_directory_absolut_path and basename "$input_file_absolut_path"

Kent
  • 189,393
  • 32
  • 233
  • 301
0

Solution

copy_absolute_path="$(cd "$output_directory_absolute_path"; pwd)/${input_file_absolute_path##*/}"

Explanation

If output_directory_absolute_path ends with a /, then $(cd "$output_directory_absolute_path"; pwd) will return the path without the trailing /.

$(input_file_absolute_path##*/} returns everything after the last /.

Shammel Lee
  • 4,092
  • 1
  • 17
  • 20