LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2008, 08:16 PM   #1
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Rep: Reputation: Disabled
BASH SHELL , how to copy all file in dir1 to dir3


BASH SHELL , how to copy all file in dir1 to dir3 and show the output list :

These files from dir1 coppied to dir3
file1 file2 file123 a.bmp caza.jpeg




is it use the for loop ?

for file in `dir1`
do
cp $file dir3
echo $file
done

i don know how set the for loop to all the file in dir1 ?
 
Old 09-03-2008, 08:23 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could simply use "cp -v dir1/* dir2/"
You don't need a loop.
 
Old 09-03-2008, 08:23 PM   #3
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Original Poster
Rep: Reputation: Disabled
but i have to show this output =(

These files from dir1 coppied to dir3
file1 file2 file123 a.bmp caza.jpeg

any idea?

Last edited by CheeSen; 09-03-2008 at 08:28 PM.
 
Old 09-03-2008, 08:27 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
sounds kinda like homework.

you don't have to use a for loop to copy files, you can simply use a file glob (i.e. dir/*)

so if you had some files that looked like so:


Code:
drwxrwsr-x 2 briank cg 4096 2008-09-03 18:22 d1
drwxrwsr-x 2 briank cg 4096 2008-09-03 18:21 d2
-rw-rw-r-- 1 briank cg 0 2008-09-03 18:22 ./d1/filec
-rw-rw-r-- 1 briank cg 0 2008-09-03 18:22 ./d1/filea
-rw-rw-r-- 1 briank cg 0 2008-09-03 18:22 ./d1/fileb
you could simply do this:

Code:
 cp -v ./d1/* ./d2
edit: oops... beat me to it.
 
Old 09-03-2008, 08:35 PM   #5
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Original Poster
Rep: Reputation: Disabled
i want show this output !

These files from dir1 coppied to dir3
file1 file2 file123 a.bmp caza.jpeg

the sample output must like above
 
Old 09-03-2008, 08:45 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 09-03-2008, 08:46 PM   #7
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
edited.

jschiwal is really beating me to the punches today.
 
Old 09-03-2008, 09:02 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Sorry. I'll often spend too much time responding to a post and a mod will post a solution in the meantime.
 
Old 09-06-2008, 02:33 AM   #9
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
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
 
Old 09-06-2008, 03:09 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 10-27-2008, 06:41 AM   #11
keithmarshall
LQ Newbie
 
Registered: Jun 2007
Distribution: OpenSUSE 10.0, Ubuntu 6.06
Posts: 4

Rep: Reputation: 0
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.:

(cd dir1; tar cf - *) | (cd dir2; tar xvf -)
 
Old 10-27-2008, 07:19 AM   #12
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Or even simpler
Code:
cp -Rv dir/* dir2/
 
Old 10-28-2008, 02:07 AM   #13
keithmarshall
LQ Newbie
 
Registered: Jun 2007
Distribution: OpenSUSE 10.0, Ubuntu 6.06
Posts: 4

Rep: Reputation: 0
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.
 
Old 10-28-2008, 03:19 AM   #14
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
I tend to use cp -pR dir1/* dir2/
as permissions tend to be important when I make a copy
 
Old 10-28-2008, 10:04 AM   #15
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Yes, of course, it's a fair bet with GNU/Linux that cp will support the -R option, but other *nixes may not.
Well we are on LinuxQuestions.org

Although I agree tar pipes are a clever use of a utility I think they are a bit too involved for this simple example.

Quote:
I tend to use cp -pR dir1/* dir2/
Yes - I tend to use cp -a when wanting to preserve things but I take your point.

Just my two cents worth..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell how to copy all except file1,directory1,etc bgeddy Programming 5 08-31-2008 07:58 PM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM
how to rename a file and copy a file in a shell zach014 Linux - Newbie 6 11-23-2006 09:23 AM
Bash : Copy a file every one minute macabre_sunsets Programming 15 09-16-2006 12:26 AM
make a file in dir1 appear to be in writeprotected dir2 magicpio Linux - Newbie 2 09-25-2005 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration