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
Discussions

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
linux - How to copy a file to a new file with a new name in same directory but across multiple directories in bash? - Stack Overflow
I am trying to copy an existing file (that is found in across directories) to a new file with a new name in Bash. For example, 'Preview.json' to 'Performance.json'. I have tried using find * -typ... More on stackoverflow.com
🌐 stackoverflow.com
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 folder and files inside directory to another directory, which are modified within the last 30 minutes
Seems all you are seeking is rsync. Could be as simple as: rsync -a /home/user/abc/* /home/user/xyz/ More on reddit.com
🌐 r/linuxquestions
6
3
December 17, 2020
🌐
Red Hat
redhat.com › en › blog › move-copy-files-linux
Linux fundamentals: How to copy, move, and rename files and directories
November 20, 2025 - You also use the mv command to rename directories and files if the destination doesn't already exist.
🌐
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 - The cp command stands for copy is used to copy files and directories in Linux System. The syntax for cp command. We can simply use the cp command along with the source and destination.
🌐
Network World
networkworld.com › home › blogs › unix as a second language
Copying and renaming files on Linux | Network World
January 23, 2024 - Linux users have for many decades been using simple cp and mv commands to copy and rename files. These commands are some of the first that most of us learned and are used every day by possibly millions of people. But there are other techniques, handy variations, and another command for renaming ...
🌐
Red Hat
ftp.kh.edu.tw › Linux › Redhat › en_6.2 › doc › gsg › s1-managing-working-with-files.htm
Copying, Moving and Renaming Files and Directories
If you want to move a file out of your home directory and into another directory, you would type: or, mv sneakers.txt /home/newuser /home/newuser/tigger using absolute pathnames. Actually, we've already covered half of renaming, because when you copy or move files, you can also rename.
🌐
ZDNET
zdnet.com › home › tech › services & software › open source
How to copy and rename files from the Linux terminal window | ZDNET
July 19, 2023 - Let's say you have a file named zdnet_test.txt in your home directory and you want to copy it to your Documents directory. The command for that process would be: Show more ... One thing to note is that ~/ is a shortcut for /home/USER (where USER is your username), so you don't have to type as much. Also: How to choose the right Linux desktop distribution · If you were already in your home directory (which your terminal app should default to when you first open it), you could short the command to: ... Let's now rename a file.
Find elsewhere
🌐
Hivelocity
hivelocity.net › home › knowledge base articles › how to move, copy, and rename a directory in linux
How to Move, Copy, and Rename a Directory in Linux
January 5, 2024 - So, if you wanted to move /directory1 into a second directory name /directory2, you would use the following command: mv /directory1 /directory2 *Note: If no “/directory2” exists, /directory1 will be renamed as “/directory2”, essentially ...
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"

🌐
ITS Documentation
documentation.its.umich.edu › node › 295
Create, Copy, Rename, and Remove Unix Files and Directories / ITS Documentation
Unix does not have a command specifically for renaming files. Instead, the mv command is used both to change the name of a file and to move a file into a different directory.
🌐
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.
🌐
Linux Hint
linuxhint.com › linux-copy-file-current-directory-rename
Linux Copy File to Current Directory and Rename – Linux Hint
Linux users have been using the ... other commands. Linux Copy a file to Current Directory An obvious way to do this is to use a command like “cp file1 file1-orig.”...
🌐
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 - | By Andrei Dumitrescu, Crystal ... 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)....
🌐
Linode
linode.com › docs › guides › how-to-copy-files-and-directories-in-linux
How to Copy Files and Directories in Linux | Linode Docs
July 18, 2022 - The cp command works similarly on most Linux distributions. The command operates in four basic modes. Copies a file to the same directory. The new file must have a different name.
🌐
High Performance Computing
hpc.nmsu.edu › onboarding › linux › commands › mv
Move/Rename Files and Directories :: High Performance Computing
To move the content of a file/directory to another directory, you just use the mv command. This command doesn’t create a copy/duplicate of files or directories.
🌐
DevPress
devpress.csdn.net › linux › 62eba93020df032da732baf3.html
How to Copy, Move and Rename Files and Directories in Linux System_linux_weixin_0010034-Linux
The cp command stands for copy is used to copy files and directories in Linux System. The syntax for cp command. We can simply use the cp command along with the source and destination. In the above example, we used the command cp file1.txt file2.txt where · cp represents the copy command file1.txt ...
🌐
University of Connecticut
www2.math.uconn.edu › ~vince › MathDoc › GSGuide › s1-managing-working-with-files.html
Copying, Moving, Renaming, and Deleting Files
To copy the file sneakers.txt from your login directory to the tigger subdirectory, just type: To copy and rename that file from sneakers.txt to piglet.txt, type: To move and rename the file, just substitute mv for cp in the above example.