LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2012, 02:49 PM   #1
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Rep: Reputation: Disabled
unable to copy files


Hello,

Code:
# ls
1  2  3  4  5  6
# cp -rf 1/*/*.* /home/bulkSavedCVs/01/
cp: cannot stat `1/*/*.*': No such file or directory
my directory with name 1 contains many directories and then files in each directory.

What I want to achieve is to copy all files of the all directories inside directory 1

Please advise

Thanks in anticipation
 
Old 04-01-2012, 03:19 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well, the error message is clear: there are no files with a dot in their name inside the first level of subdirectories. If you want to copy all the files in 1 to another directory, first you must be sure there aren't files with the same name, otherwise they will be overwritten. Then you can easily do it using find:
Code:
find 1 -type f -exec echo cp -ip '{}' /home/bulkSavedCVs/01 \;
The echo command is just for testing purposes: it will cause the cp commands to be printed out without actually execute them. In this way you can review the results and if they match your requirement, you can run again without echo. Moreover, you can use option -i of the cp command, so that if it encounters some files with the same name, it will ask confirmation before overwriting. Hope this helps.
 
Old 04-01-2012, 03:19 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,355

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Assuming the directory structure is set up like you said, then it should work

Run:
Code:
ls 1
ls 1/*
and post the output
 
Old 04-01-2012, 04:34 PM   #4
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Code:
# find 1 -type f -exec echo cp -ip '{}' /home/bulkSavedCVs/01 \;
cp -ip 1/2/Abdul Rehman Alvi_original.doc /home/bulkSavedCVs/01
cp -ip 1/2/abdul rabb_original.doc /home/bulkSavedCVs/01
cp -ip 1/2/Abdulhakim Wazeery_original.doc /home/bulkSavedCVs/01
cp -ip 1/2/abdulhakeem.doc /home/bulkSavedCVs/01
cp -ip 1/2/Doc1.rtf /home/bulkSavedCVs/01
cp -ip 1/2/Abdul Waheed Khan_original.doc /home/bulkSavedCVs/01
Files are not copied

Regards
 
Old 04-01-2012, 04:35 PM   #5
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Code:
# ls 1
2
# ls 1/*
abdulhakeem.doc                  Abdul Rehman Alvi_original.doc
Abdulhakim Wazeery_original.doc  Abdul Waheed Khan_original.doc
abdul rabb_original.doc          Doc1.rtf
 
Old 04-01-2012, 04:45 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Raakh5 View Post
Files are not copied
As I told in my previous post, the suggested command doesn't copy files, it just prints out the cp commands to let you review them and see if it is what you want. After checking the result, run again without echo:
Code:
find 1 -type f -exec cp -ip '{}' /home/bulkSavedCVs/01 \;
This is a safe method when using -exec with commands like mv, rm or cp, in my opinion. Especially if you don't even know what the above command line does exactly!
 
1 members found this post helpful.
Old 04-01-2012, 04:56 PM   #7
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Its worked!! appreciated!!

Code:
# find 1 -type f -exec cp -ip '{}' /home/bulkSavedCVs/01 \;
cp: overwrite `/home/bulkSavedCVs/01/Abdul Rehman Alvi_original.doc'? y
cp: overwrite `/home/bulkSavedCVs/01/abdul rabb_original.doc'?
1) I didn't want to overwrite but to keep both files
2) Its very hard to press "y" every time. Can I get rid off this y?

Regards
 
Old 04-01-2012, 05:14 PM   #8
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Thanks from the bottom of my heart to all. For additional questions that I asked this link was very useful for me http://www.cafewebmaster.com/update-...mv-cp-commands

Best regards
 
Old 04-01-2012, 05:19 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Raakh5 View Post
1) I didn't want to overwrite but to keep both files
In this case a one-line command is not enough. You should do some scripting to check the existence of multiple files with the same name and rename the copies. To avoid that, why not copying the whole directory structure?

An alternative is to use the --backup option of the cp command to rename the copies. Please, see man cp for details.
Quote:
Originally Posted by Raakh5 View Post
2) Its very hard to press "y" every time. Can I get rid off this y?
This is in contrast with the requirement in point number 1). If you don't want to overwrite, you should answer no (but the file with the same name will not be copied at all). Anyway, as I told previously, is the -i option that causes the cp command to ask confirmation. Otherwise it overwrites with no mercy!
 
1 members found this post helpful.
Old 04-01-2012, 05:35 PM   #10
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
You pointed well. Okay, for overwriting the file without pressing y what should need to be changed in this command:
find 1 -type f -exec cp -ip '{}' /home/bulkSavedCVs/01 \;

Regards

Last edited by Raakh5; 04-01-2012 at 06:03 PM.
 
Old 04-01-2012, 07:13 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Just remove the -i option from the cp command.
Code:
find 1 -type f -exec cp -p '{}' /home/bulkSavedCVs/01 \;
Sincerely I don't understand what is your requirement. Maybe if you explain what you're trying to achieve, someone can give a more suitable solution or advice.
 
1 members found this post helpful.
Old 04-02-2012, 12:02 PM   #12
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Just remove the -i option from the cp command.
Code:
find 1 -type f -exec cp -p '{}' /home/bulkSavedCVs/01 \;
Sincerely I don't understand what is your requirement. Maybe if you explain what you're trying to achieve, someone can give a more suitable solution or advice.
I appreciated your help and thankful to you. Its copying files now without prompting 'y' to overwrite

what I understood is that start a loop to find folder name "1" that contains type of file (f) and execute cp command from this path (-p) '{}' to this path. Is that right? '{}' why this exists?

Sorry to bother you again

Best regards
 
Old 04-02-2012, 01:48 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Raakh5 View Post
what I understood is that start a loop to find folder name "1" that contains type of file (f) and execute cp command from this path (-p) '{}' to this path. Is that right? '{}' why this exists?
Actually the find command starts the search from "1" (that is, it search recursively inside directory "1"). What to search? Only files (-type f) excluding directories, links or other stuff from the result. What to do on each item found? Files are copied to the destination path (-exec cp blah blah blah).

The command to be executed on every item found is
Code:
cp -p '{}' /home/bulkSavedCVs/01 \;
The strings highlighted in blue are part of the syntax of the -exec predicate. The -p option of the cp command is to preserve permissions, ownership and timestamp of the original files (using it is a habit for me). The argument '{}' is substituted by the results of the find command (the file names) one at a time (take in mind that the cp command is executed multiple times, one for each file). Finally the escaped semi-colon \; is to terminate the command.

Please, see man find for details and look for the -exec predicate.


Quote:
Originally Posted by Raakh5 View Post
Sorry to bother you again
No problem!
 
1 members found this post helpful.
Old 04-02-2012, 02:28 PM   #14
Raakh5
Member
 
Registered: Mar 2012
Posts: 167

Original Poster
Rep: Reputation: Disabled
Thanks from the bottom of my heart. I really learned a lot from you.

Thanks again & best regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I'm unable to copy the *.dat files from VCD's sajinsj Linux - General 9 11-10-2011 09:53 AM
Unable to Copy Files Over To Qnap Nas spaxz Linux - Newbie 2 05-02-2011 07:25 PM
Unable to copy files from CentOS to pendrive nikunjbadjatya Linux - Newbie 6 09-24-2009 08:41 AM
Unable to copy files from CentOS to pendrive nikunjbadjatya Linux - Software 1 09-23-2009 07:35 AM
Unable to copy or create files on samba Thaidog Linux - General 1 01-22-2007 12:54 AM


All times are GMT -5. The time now is 09:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration