Discussion Locked

cp advance command

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

hi does anyone know of a command that will do the following,

copy file1 to multiple folders. i want to copy file1 to all folders under the home directory.

thanks

3 replies

by abarnes70

You can try the following batch file. It works in Win2000, and probably XP; I don't think it would work in 98 or earlier.
__________________________________________

@echo off
if %1.==. goto syntax
for /r %2 %%y in (.) do copy "%1" "%%y"
goto end

:syntax
echo.
echo manycopy sourcefile [destfile]
echo.
echo where sourcefile is the file to be copied
echo where optional destfile is the root folder to start from
echo default destfile is current directory
echo.
echo.
pause
goto end

:end
________________________________________________

You need to give it the name of the source file to copy; if there are spaces in the file name or path you'll need to enclose it in double quotes. For example, if you wanted to copy C:\FILE1.DOC to the My Documents folder of user ABAR and all it's subfolders, you'd type

manycopy c:\file1.doc "C:\Documents and Settings\abar\My Documents"

Hope that helps.

by cpfeiffe

This is in the linux forum so I'm assuming you want a linux answer. Try this -
cd /home
for dir in `find . -type d|cut -d/ -f2|sort -u`
do
cp file $dir/file
done
I think there is a way to do this with ls instead of find, but I don't know it off the top of my head. This should work nonetheless. The find command will find directories in /home, then cut will just grab the second field (the directory level immediately under /home and not all of its sub-directories, and the sort -u does a unique sort so you don't get the directory multiple times (would normally show up for each of its subdirectories). To see the results before doing the copy just cd to /home and run find . -type d|cut -d/ -f2|sort -u. Play with each command separately if you'd like to get a better feel for what they can all do. Good luck.

by paulo.sedrez

All folders under home directory:

for d in /home/*/. ; do cp file1 "$d" ; done

All folders under home directory, recursivelly:

find /home/ -type d | xargs -i% cp file1 %

All home directories of users that has it under /home:

cut -d: -f6 /etc/passwd | grep '^/home/' | xargs -i% cp file1 %

All home directories of users from NIS that has it undex /home:

ypcat passwd | cut -d: -f6 | grep '^/home/' | xargs -i% cp file1 %

In Unix and Linux, you don't need commands with lots and lots of options and functions; you use simplier commands to do whatever you want.

This discussion is closed to new replies.