ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
OK, either use file globbing to produce a list of files for your for loop, or use sed or awk to convert the output of "cp -v dir1/* dir2/". Or use "find ./ -type f" to produce the list of arguments.
If you use a for loop, look at using "&&" to output the filename if the file was copied successfully.
This does sound like homework. Otherwise why does the output need to be like that. It is so contrived it is a stupid way of copying files. This is something you can do with a simple single command.
See the manpages or info pages for the "find", "cp" commands. For "for" loops, try "help for".
Also look in the info bash manual for information on variable substitution. You may need that to replace dir1 to dir2 in your copy command, depending on where the CWD is relative to your two directories.
OK, either use file globbing to produce a list of files for your for loop, or use sed or awk to convert the output of "cp -v dir1/* dir2/". Or use "find ./ -type f" to produce the list of arguments.
If you use a for loop, look at using "&&" to output the filename if the file was copied successfully.
This does sound like homework. Otherwise why does the output need to be like that. It is so contrived it is a stupid way of copying files. This is something you can do with a simple single command.
See the manpages or info pages for the "find", "cp" commands. For "for" loops, try "help for".
Also look in the info bash manual for information on variable substitution. You may need that to replace dir1 to dir2 in your copy command, depending on where the CWD is relative to your two directories.
can you explain more detail and with example ? i am not really understand.. thanks
If you use "cp dir1/* dir/", bash expands the asterisk to a list of files in the directory. You can have a for loop that uses dir1/* as arguments. It may work better if you cd into dir1 first. Then you won't have a dirname component.
Take a look at "set *" in the directory you are in. The set command will take the arguments and enter them into $1, $2, $3, etc. just as a command would see them. Look at $1, $2, and $@.
echo $1 $2 $3
echo $@
So bash expands the asterisk to a list of the filenames. You can use that list in a for loop.
echo "These files copied to dir1"
for file in *; do
cp "$file" dir2/ && echo "$file"
done
If you read through the info bash manual, look at the section on variable expansion. This will help if you need to do something like remove the directory part or you want to change part of the filename.
echo "These files copied from dir1 to dir2:"
for file in dir1/*; do
cp "$file" dir2/${file##*/}" && echo "${file##*/}"
done
If the filenames contain white space characters, then you may want to use "find" to produce a list of files instead of fileglobbing.
find dir1/ -maxdepth 1 -type f -exec cp -v '{}' dir2/ \;
The output can be filtered with a sed command like this to leave just the filename:
sed 's/`work\///;s/'"'"'.*$//'
The "work" is my DIR1. I was working interactively. For a script you may want to get DIR1 and DIR2 from arguments to your script and use that in your find and sed commands. Remember the a variable is expanded inside double quotes but not single quotes.
Good Luck. Be sure to try out these commands. I would also recommend you download the "Advanced Bash Scripting Guide" from the www.tldp.org website. Don't let the title scare you. It is composed of well commented examples. Manpages usually don't have any or enough examples, and this book is composed entirely of examples.
Just stumbled across this; seems strange that no one has mentioned using either tar or cpio, to deep copy all files in a given dir, and its subdirs, e.g.:
Yes, of course, it's a fair bet with GNU/Linux that cp will support the -R option, but other *nixes may not. The `tar' method is classical Unix sysadmin stuff, (at least from SVR3 days); just thought it odd that it wasn't mentioned.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.