Use terminal to copy entire folder to new nested path - terminal

I am trying to copy a folder and its contents using the terminal. The destination path does not necessarily exist. For instance;
The folder i want to copy lies at ./my-folder and i want to copy it to ./folder-a/folder-b/folder-c/folder-d/<--put it here!-->.
Assume folder- a, b, c and d does not exist.
I have tried using cp -R, cp -a and cp -f without success.
I get the error No such file or directory.
I am on macOS Monterey, using z-shell.
How can i make the copy?

Related

Failed to copy list of files to another folder

I have a text file called "list.txt" that contain all the directories of the files that need to be copied to a new folder (dir_newfolder). I wrote the code like below:
for file in $(cat list.txt); do cp ${file} dir_newfolder; done
I got list of errors: cp:"file_name":No such file or directory. The file_names are the lines pulled out from the "list.txt". But when I copy each file_names from the error message and use cp to copy to the new folder. There is no error.
I am using mac os terminal.
Thanks in advance.
Copy a file or folder locally
In the Terminal app on your Mac, use the cp command to make a copy of a file.
For example, to copy a folder named Expenses in your Documents folder to another volume named Data:
% cp -R ~/Documents/Expenses /Volumes/Data/Expenses
The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.
in your case:
make sure you are providing correct path list.txt and the correct path for destiny folder, also i mentioned how to access file variable in double quotes , try this code it's working for me
for file in $(cat ~/Documents/list.txt); do cp "$file" ~/dir_newfolder; done

Copy command in Mac Terminal fails with "No such file or directory"

This should be straightforward, but I'm getting weird results.
I have a folder with subfolders containing ~4000 files. I'm trying to copy just the files of a certain filetype to another folder, while preserving the subfolder hierarchy.
Command:
cp -R /Users/Steve/Desktop/Hardscapes/*.LOB /Users/Steve/Desktop/Temp
fails with the message:
"/Users/Steve/Desktop/Hardscapes/*.LOB: No such file or directory".
I created the command by typing cp -R then dragging the source folder to the terminal window, adding *.LOB after the /, and dragging the destination folder to the terminal window.
Troubleshooting:
replacing *.LOB with *.* gives the same error.
cp -R /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp copies the entire Hardscapes folder to Temp, with all its subfolders and files.
Thanks for your help and suggestions.
EDIT: The folder Hardscapes contains only other folders. If I run the command above using one of those folders as the source, the contents are copied faithfully. The folder Hardscapes itself contains no .LOB files - they're only in the subfolders.
So maybe that's the issue, cp can't find any files corresponding to Hardscapes/*.LOB? But I thought the -R switch was supposed to tell it to look through all the subfolders.
Next I put a file named Test.LOB in the Hardscapes folder. The command above copies only that file and none of the subfolders. It looks like the -R switch is not doing its job. Do I have the syntax right?
Try this:
rsync -a --prune-empty-dirs --include '*/' --include '*.LOB' --exclude '*' /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp
As you already mentioned, directory Hardscapes itself contains no .LOB files. That's why your mask /Users/Steve/Desktop/Hardscapes/*.LOB results in matching no files at all.

Shell command for copying the contents of one folder to another

Due to the way the Plesk Extension on my web server works, I am trying to write a shell command that fires after a deployment. This simply needs to copy the contents of one folder to another.
Currently, I am using this:
cp -r /deployed-site/public/ /httpdocs/
However, this only seems to work if the destination folder is empty. Every time a deployment occurs, I want the contents of the first folder copied and pasted into the second?
I would say it's better to clean the destination folder before copying files:
rm -rf /httpdocs
cp -r /deployed-site/public/ /httpdocs/

cp command in macOS doesn't create a new folder if it doesn't exist

cp -R /Desktop/One.java /Desktop/Nine
The folder Nine is not created. Instead, a new file Nine is created without any extension.
If cp is used with -R, then the target (/Desktop/Nine) is created with the same file type as the source (regular file). If the source is a directory (it isn't), the full directory hierarchy will be copied.
In your case, the source is a single regular file, in which case cp -R will act identically as cp (with no -R).
If you want to copy the file /Desktop/One.java into a new directory /Desktop/Nine:
mkdir /Desktop/Nine
cp /Desktop/One.java /Desktop/Nine

Copying from mount share drive to local folder through script

This is my first time trying to work with Linux Scripts so this may be something obvious.
Here is what I am trying to do:
Remove all contents from local folder - rm /home/user/Documents/Exercise/
Copy files from a shared windows network drive - cp smb://server/arc/Exercise%20Files/Word/
So from my understanding my command should look like this
rm /home/user/Documents/Exercise/
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
But anytime I try and run either of the above commands I get the following error:
"rm: cannot remove `/home/user/Documents/Exercise/': Is a directory"
"cp: cannot stat `smb://server/arc/Exercise%20Files/Word/': No such file or directory"
What am I doing wrong?
Kind Regards,
M
Based on your request and your test, let me point what is not written properly:
Remove all contents from local folder
rm /home/user/Documents/Exercise/
Error says rm: cannot remove /home/user/Documents/Exercise/': Is a directory
You should
rm /home/user/Documents/Exercise/*
which will delete everything inside the directory, but not the directory.
Copy files from a shared windows network drive
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
Error says cp: cannot stat smb://server/arc/Exercise%20Files/Word/': No such file or directory
You should check if route smb://server/arc/Exercise%20Files/Word/ is correct. Then, use the following:
cp smb://server/arc/Exercise%20Files/Word/* /home/user/Documents/Exercise/
You can't delete a directory if it has content within it.
To delete the content and the directory at the same time, use the following command:
rm -r /home/user/Documents/Exercise/
This recursively deletes the directory and any content within it.
To copy the file, I believe you have to mount the directory beforehand, like so:
mount -t cifs //server/share /mnt/mount_directory -o user=username
Can you confirm if that works?
Remove / Delete Command:
rm -rfv /home/user/Documents/Exercise/*
Copy Command:
cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/ /home/user/Documents/Exercise/

Resources