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.comCopy and rename in the same time (also change filename, not only path):
cp program3.cpp homework6.cpp
Rename only:
mv program3.cpp homework6.cpp
If you want to have the files permanently linked use the ln command instead of cp
ln program3.cpp homework6.cpp
This puts a file descriptor (hard link) under the name homework6.cpp to the same file location as program3.cpp
You should be able to do just
cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21
However, if the target directory already exists, this would append the final part of the source path to the destination path, creating
/tf/Custom_App_backups/Custom_App_2017-12-21/Custom_App, and then copy the rest of the tree within that.
To prevent this, use /tf/Custom_App/. as the source. Of course, in that case you might want to rm -r /tf/Custom_App_backups/Custom_App_2017-12-21 first, if you don't want older files lying around there after the copy.
The difference between /some/dir and /some/dir/. was discussed a while back in cp behaves weirdly when . (dot) or .. (dot dot) are the source directory
Alternatively, you can do it like so:
mkdir /tf/Custom_App_backups/Custom_App_2017-12-21 # prepare the target location
cp -R /tf/Custom_app/. /tf/Custom_App_backups/Custom_App_2017-12-21 # copy only the contents
This will allow you to specify your custom location beforehand. Also, notice that it uses the suffix /. This allows you to only copy the contents and exclude its containing folder -- in this case it is the Custom_app folder.
linux - Copying a file from one directory to the next and changing the name - Unix & Linux Stack Exchange
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
Copy and rename file from shell script
Copy folder and files inside directory to another directory, which are modified within the last 30 minutes
Videos
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.
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"
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/$filenameThe 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.