Copy and rename in the same time (also change filename, not only path):

cp program3.cpp homework6.cpp

Rename only:

mv program3.cpp homework6.cpp
Answer from Cornelius on askubuntu.com
๐ŸŒ
Ask Ubuntu
askubuntu.com โ€บ questions โ€บ 1205513 โ€บ copy-over-a-large-number-of-files-with-new-names
command line - Copy over a large number of files with new names - Ask Ubuntu
Copy and paste the following code into the editor changing /full/path/to/old/directory/ with the full path to your /dir1 directory and /full/path/to/new/directory/ to the full path to your /dir2 directory: #!/bin/bash oldpath="/full/path/to/old/directory/" newpath="/full/path/to/new/directory/" ls -l "$oldpath" > "ls_file.txt" while read f1 f2 f3 f4 f5 f6 f7 f8 f9 do mod="${f6}${f7}${f8}" mod2=$(echo "$mod" | tr -d ' :') name="${f9%.*}" ext="${f9##*.}" new="$f3""_""$name""_""$mod2"".""$ext" echo "$f9"" ---> Will be renamed ---> ""$new" echo "----------------------------" # cp -p "$oldpath""$f9" "$newpath""$new" done < "ls_file.txt"
Discussions

Copy and rename file from shell script
It's hard to tell because the formatting is real messed up You can rename with just: cp /path/to/source/file /path/to/destination/newname More on reddit.com
๐ŸŒ r/bash
28
2
October 26, 2023
copy and rename file in linux bash - Stack Overflow
I'd like to copy and rename multiple files within the same folder. Just like I have the files foo.c foo.h and want to use them as a template for new files named bar.c bar.h. ... Is there some simple solution for this or do I have to create a whole script that opens a folder in /tmp, copy there, rename there and move back? ... Possible duplicate of Rename multiple files, but only rename part of the filename in Bash... More on stackoverflow.com
๐ŸŒ stackoverflow.com
shell - How can I copy a file in a bash script and rename it while copying and place it in the same directory - Unix & Linux Stack Exchange
How would I copy a file "file.doc" and rename it while copying to "file_copy.doc" and place it in the same directory ? And this only by calling the script and adding the file name in the argument:... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
linux - Copying a file from one directory to the next and changing the name - Unix & Linux Stack Exchange
If I wanted to copy a file called myscript from, let's say "/home/myusername" to a directory called "/home/myusername/test" while also renaming it to myscript2... how would I do... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
October 19, 2022
Top answer
1 of 2
3

In an -exec predicate, the symbol {} represents a path that is being considered, starting at one of the starting-point directories designated in the command. Example: start/dir2/Preview.json. You can form other file names by either prepending or appending characters, but whether that makes sense depends on the details. In your case, appending produces commands such as

cp start/dir2/Preview.json start/dir2/Preview.jsonQA

which is a plausible command in the event that start/dir2/Preview.json exists. But cp does not automatically create directories in the destination path, so the result of prepending characters ...

cp start/dir2/Preview.json QAstart/dir2/Preview.json

... is not as likely to be accepted -- it depends on directory QAstart/dir2 existing.

I think what you're actually looking for may be cp commands of the form ...

cp start/dir2/Preview.json start/dir2/QAPreview.json

... but find cannot do this by itself.

For more flexibility in handling the file names discovered by find, pipe its output into another command. If you want to pass them as command-line arguments to another command, then you can interpose the xargs command to achieve that. The command on the receiving end of the pipe can be a shell function or a compound command if you wish.

For example,

# Using ./* instead of * ensures that file names beginning with - will not
# be misinterpreted as options:
find ./* -type f -name 'Preview.json' |
  while IFS= read -r name; do  # Read one line and store it in variable $name
    # the destination name needs to be computed differently if the name
    # contains a / character (the usual case) than if it doesn't:
    case "${name}" in
      */*) cp "${name}" "${name%/*}/QA${name##*/}" ;;
      *)   cp "${name}" "QA${name}" ;;
    esac
  done

Note that that assumes that none of your directory names contain newline characters (the read command would split up newline-containing filenames). That's a reasonably safe assumption, but not absolutely safe.

Of course, you would generally want to have that in a script, not to try to type it on the fly on the command line.

2 of 2
0

If you want to copy Preview.json to a new name like Performance.json, across multiple folders, you can do it like this directly on terminal:

find . -type f -name 'Preview.json' | while read -r file; do \
  dir="${file%/*}"; \
  cp "$file" "$dir/Performance.json"; \
done

This works like finds every Preview.json in subfolders and copies it to Performance.json in the same folder

For example, If you have .....

./project1/Preview.json
./project2/Preview.json

..... Youโ€™ll get

./project1/Performance.json
./project2/Performance.json

Itโ€™s just Bash string slicing:

${file%/*} = everything before the last slash โ†’ folder

${file##*/} = just the filename

Check man bash or Google "bash parameter expansion"

๐ŸŒ
DEV Community
dev.to โ€บ yashsugandh โ€บ how-to-copy-move-and-rename-files-and-directories-in-linux-system-4kpo
How to Copy, Move and Rename Files and Directories in Linux System - DEV Community
May 23, 2020 - Now let's have a look at how to rename a file/directory. ... Yes the mv command is used for both moving as well as renaming a file/directory. ... mv represents move command original represents the current_name original new represents new_name new
๐ŸŒ
Reddit
reddit.com โ€บ r/bash โ€บ copy and rename file from shell script
r/bash on Reddit: Copy and rename file from shell script
October 26, 2023 -

This is a pretty basic question but Iโ€™ve been struggling getting this working for some reason. I am trying to copy a file from one directory to another and renaming it along with the copy. This is being done inside of a shell script, and I have a variable called $filename that stores the NEW file name. Here is the code snippet:

filename="IRcam_fpga_cksm_${checksum}_ver_${version}.pdb"
#filename="This_is_a_file.txt"
echo "filename: ${filename}"
cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/$filename

The output of the echo command on the console is:

.pdbname: IRcam_fpga_cksm_A415_ver_0x0081

  But the file that gets copied to the new directory does not have the correct name. When I use the version of $filename that is commented out, it works perfectly fine.

Top answer
1 of 5
3
It's hard to tell because the formatting is real messed up You can rename with just: cp /path/to/source/file /path/to/destination/newname
2 of 5
2
The issue you're encountering is because you're overwriting the filename variable after you first set it. When you assign a new value to the filename variable, you essentially lose the previous value, which is why the file is copied with the default This_is_a_file.txt name instead of the intended IRcamfpga_cksm${checksum}ver${version}.pdb. To fix this, make sure that you're not overwriting the filename variable in the script. Here's an updated version of your code: bash filename="IRcamfpga_cksm${checksum}ver${version}.pdb" # Uncomment and use the correct $filename variable without overwriting it echo "filename: ${filename}" # Copy the file and rename it cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/"${filename}" Explanation: Define the filename correctly: Ensure that the variable filename has the correct value before it's used. Avoid overwriting filename: The second assignment (filename="This_is_a_file.txt") overwrites the first one, which is incorrect. You should either remove or not assign it again. Ensure proper file path: Use "${filename}" to reference the variable safely in the cp command. If checksum and version are being dynamically generated, ensure those variables are correctly populated before being used to build the filename. Here's an example where checksum and version are assumed to be variables with values: bash checksum="A415" version="0x0081" filename="IRcamfpga_cksm${checksum}ver${version}.pdb" echo "filename: ${filename}" # Now copy the file with the correct name cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/"${filename}" You can try renamer. ai to get easy file renaming solution.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 59101823 โ€บ copy-and-rename-file-in-linux-bash
copy and rename file in linux bash - Stack Overflow
#!/bin/bash # for each file following the pattern "foo." for i in foo.* do # copy file to "bar" + original extension cp $i bar.${i#foo.} done ... Find the answer to your question by asking. Ask question ... See similar questions with these tags.
Find elsewhere
๐ŸŒ
Network World
networkworld.com โ€บ home โ€บ blogs โ€บ unix as a second language
Copying and renaming files on Linux | Network World
January 23, 2024 - This command will move a file to a different directory, change its name and leave it in place, or do both. $ mv myfile /tmp $ mv myfile notmyfile $ mv myfile /tmp/notmyfile ยท But we now also have the rename command to do some serious renaming for us. The trick to using the rename command is to get used to its syntax, but if you know some perl, you might not find it tricky at all. Hereโ€™s a very useful example. Say you wanted to rename the files in a directory to replace all of the uppercase letters with lowercase ones.
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ how to rename a file in linux with simple command line options
Rename a File in Linux with Command Lines - Udemy Blog
September 23, 2021 - If we need to rename a single file in Linux, we have two options: we can create a copy of the file with a new name (and delete the old one) or we can rename the file by moving it (with the MV command).
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ how-to-copy-file-with-different-name-271252
How to Copy File with Different Name in Linux | LabEx
September 21, 2024 - ## Copying Files with Different Names in Linux In the Linux operating system, you can easily copy a file and give it a different name using the `cp` command.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ linux-copy-file-current-directory-rename
Linux Copy File to Current Directory and Rename โ€“ Linux Hint
Easy loops allow us to create backup copies with modified names. ... To copy the big-name file, we copy the files using the syntax of โ€œ-origโ€. ... The mv command is used to rename the file in the Linux system. For this, we need the current_name and new_name of the directory along with the ...
๐ŸŒ
ZDNET
zdnet.com โ€บ home โ€บ tech โ€บ services & software โ€บ open source
How to copy and rename files from the Linux terminal window | ZDNET
July 19, 2023 - This process is almost the same as above, but you're not copying a file to another directory, you're moving it. ... This task is accomplished with the mv command. We'll stick with our example.
๐ŸŒ
LinkedIn
linkedin.com โ€บ posts โ€บ gorakhnath-malusare-115118277_copy-move-rename-files-and-directories-activity-7326859386469945344-2TpE
How to copy, move, and rename files in Linux with Bash commands | GORAKHNATH MALUSARE posted on the topic | LinkedIn
May 10, 2025 - 2๏ธโƒฃ Move: mv [source] [destination] Syntax: mv -v <file-or-dir> <target-dir-or-file> Example: mv -v draft.md documents/ Moves draft.md into the documents/ directory (verbose). 3๏ธโƒฃ Rename: also uses mv Syntax: mv -v <old-name> <new-name> ...