Unix command CP to copy a file to multiple directories - shell

I have folder structure like this:
/home/
/folder1/
/backup/
/folder2/
/backup/
/folder3/
/folder4/
/backup/
/folder5/
(As you can see, no all directories "folder" have a directory "backup")
I need to copy the script "checker.php" to all "backup" directories only.
"checker.php" is at:
/home/checker.php
I am using this command:
cp /home/checker.php /home/*/backup/checker.php
But it is not working. Please help.

The cp command doesn't allow multiple destination directories.
A way forward is to loop through the folders:
for d in /home/*/backup; do
cp /home/checker.php "$d"
done

Related

renaming files in different folders

i have 1000 folders and each folders contain 5 files with same name that i want to copy and rename in different name.
For example:
My folders path
/home/yuan/data/foldera1/*
/home/yuan/data/foldera2/*
/home/yuan/data/foldera3/*
/home/yuan/data/foldera4/*
.........................
/home/yuan/data/foldera1000/*
And the files i want to rename is
data1.ax.ze to data.a1.z1
datay.ax.z1 to data.a2.z2
dataw.ac.zt to data.a3.z3
data4.an.z5 to data.a4.z4
datax.aa.zq to data.a5.z5
i tried script below: but it doesnot rename files inside each folder
for file in /home/yuan/data/foldera?/*
do
cp data1.ax.ze data.a1.z1
cp datay.ax.z1 data.a2.z2
cp dataw.ac.zt data.a3.z3
cp data4.an.z5 data.a4.z4
cp datax.aa.zq data.a5.z5
done
Hope experts will help me solving this problem.Thanks in advance.
for dir in /home/yuan/data/foldera*; do
cd "$dir"
cp data1.ax.ze data.a1.z1
cp datay.ax.z1 data.a2.z2
cp dataw.ac.zt data.a3.z3
cp data4.an.z5 data.a4.z4
cp datax.aa.zq data.a5.z5
done

BASH: Copy all files and directories into another directory in the same parent directory

I'm trying to make a simple script that copies all of my $HOME into another folder in $HOME called Backup/. This includes all hidden files and folders, and excludes Backup/ itself. What I have right now for the copying part is the following:
shopt -s dotglob
for file in $HOME/*
do
cp -r $file $HOME/Backup/
done
Bash tells me that it cannot copy Backup/ into itself. However, when I check the contents of $HOME/Backup/ I see that $HOME/Backup/Backup/ exists.
The copy of Backup/ in itself is useless. How can I get bash to copy over all the folders except Backup/. I tried using extglob and using cp -r $HOME/!(Backup)/ but it didn't copy over the hidden files that I need.
try rsync. you can exclude file/directories .
this is a good reference
http://www.maclife.com/article/columns/terminal_101_using_rsync_locally
Hugo,
A script like this is good, but you could try this:
cp -r * Backup/;
cp -r .* Backup/;
Another tool used with backups is tar. This compresses your backup to save disk space.
Also note, the * does not cover . hidden files.
I agree that using rsync would be a better solution, but there is an easy way to skip a directory in bash:
for file in "$HOME/"*
do
[[ $file = $HOME/Backup ]] && continue
cp -r "$file" "$HOME/Backup/"
done
This doesn't answer your question directly (the other answers already did that), but try cp -ua when you want to use cp to make a backup. This recurses directories, copies rather than follows links, preserves permissions and only copies a file if it is newer than the copy at the destination.

Bash: scp all files in a directory, but not the subdirectory

I would like to copy all files from a remote destination in a particular directory writegrid, but not the subdirectories, e.g. the files in writegrid/output_files.
This
scp -r cory#blah.com:~/writegrid/* ./
will copy the files in the writegrid/output_files over as well.
Thank you.
Don't use the -r flag. It tells scp to copy recursively.
use '' in your path as below
scp cory#blah.com:'~/writegrid/*' .

How to copy directories in OS X 10.7.3?

Hi I'm trying to copy my rails_projects directory from haseebjaved/Desktop/rails_projects to my home directory, which is haseebjaved.
How can I do this via the Command Line?
Also, can I see my home directory on the UI or only via the Command Line in Mac OS X?
Is it possible to copy directories to and from my home directory via the UI? Or only via Command Line?
Thank you
Is there something special with that directory or are you really just asking how to copy directories?
Copy recursively via CLI:
cp -R <sourcedir> <destdir>
If you're only seeing the files under the sourcedir being copied (instead of sourcedir as well), that's happening because you kept the trailing slash for sourcedir:
cp -R <sourcedir>/ <destdir>
The above only copies the files and their directories inside of sourcedir. Typically, you want to include the directory you're copying, so drop the trailing slash:
cp -R <sourcedir> <destdir>
tl;dr
cp -R "/src/project 1/App" "/src/project 2"
Explanation:
Using quotes will cater for spaces in the directory names
cp -R "/src/project 1/App" "/src/project 2"
If the App directory is specified in the destination directory:
cp -R "/src/project 1/App" "/src/project 2/App"
and "/src/project 2/App" already exists the result will be "/src/project 2/App/App"
Best not to specify the directory copied in the destination so that the command can be repeated over and over with the expected result.
Inside a bash script:
cp -R "${1}/App" "${2}"

Preserve directory tree while copying files with cp

I have about 1000 folders that I want to extract a single file from to upload to a server but I need to preserve the directory tree.
cp */myFile.txt ../newTree
Is what I basically want to do but instead of each file being saved to ../newTree/myFile.txt I want it to be ../newTree/*/myFile.txt where the * is the wildcard from the cp command.
I couldn't find a flag in the man file for this so I'm thinking I may need another utility besides cp
With rsync:
find ./ -name myFile.txt -print0|rsync -0adv --files-from=- ./ ../newTree/
Without rsync:
You can find all files, for each file you create the directory in the newTree, and copy the file to it.
for file in */myFile.txt; do
dir=$(dirname "$file")
mkdir -p "../newTree/$dir"
cp "$file" "../newTree/$dir"
done
Store all the files in a tar archive , then extract it on the server.

Resources