0

I am running my bash script with a file as the input file:

./myscript.sh anyfilename.txt

and then here is the script:

#!/bin/bash

input_file=$1
dest_dir="./destination"
mkdir -p "$dest_dir"
cp "$input_file" "$dest_dir"

when I run it, it copies the file to the destination directory using the same name as it is passed to the bash parameter, anyfilename.txt.

Question:

how can I copy the input file into the destination directory by renaming it always as myfile.txt?

2

You can use command like:

#!/bin/bash
input_file=$1
new_file=$2
dest_dir="./destination"
mkdir -p "$dest_dir"
cp "$input_file" "${dest_dir}/${new_file}"

and run the script like:

./myscript.sh anyfilename.txt myfile.txt

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.