LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-11-2008, 01:43 AM   #1
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Rep: Reputation: 15
cp command omitting directories


Hi
I'm using the follwoing command to copy some files from one folder to another, while doing this, i'm getting the following error:
Code:
>find /home/susee/data/A/*/C/ -type f  -name "*.254" | xargs -i cp "{}" /home/susee/databkp/A/*/C/
cp: omitting directory `/home/susee/databkp/A/1/C/'
cp: omitting directory `/home/susee/databkp/A/2/C/'
cp: omitting directory `/home/susee/databkp/A/3/C/'
cp: omitting directory `/home/susee/databkp/A/4/C/'
Please tell me where i've done mistake.

But the same thing when i'm trying without using wild character its working.

Code:
>find /home/susee/data/A/1/C/ -type f  -name "*.254" | xargs -i cp "{}" /home/susee/databkp/A/1/C/
 
Old 09-11-2008, 01:45 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You use cp's -R option to copy directories recursively, as it says in the man page.
 
Old 09-11-2008, 02:26 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The wildcard is being expanded before the command line is executed. This yields multiple directory targets as arguments. The cp command can only copy to a single directory at a time. You'd need multiple cp commands to do what you want. You can do this in a shell loop.
 
Old 09-11-2008, 02:39 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Happy birthday Nylex!
 
Old 09-11-2008, 05:03 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal View Post
Happy birthday Nylex!
Ta jschiwal!
 
Old 09-11-2008, 05:08 AM   #6
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
i tried with -r,this time, its not showing any error, but still the required files couldn't get copied.
 
Old 09-11-2008, 05:09 AM   #7
RMLinux
Member
 
Registered: Jul 2006
Posts: 224

Rep: Reputation: 31
Quote:
Originally Posted by nanda22 View Post
Hi
I'm using the follwoing command to copy some files from one folder to another, while doing this, i'm getting the following error:
Code:
>find /home/susee/data/A/*/C/ -type f  -name "*.254" | xargs -i cp "{}" /home/susee/databkp/A/*/C/
cp: omitting directory `/home/susee/databkp/A/1/C/'
cp: omitting directory `/home/susee/databkp/A/2/C/'
cp: omitting directory `/home/susee/databkp/A/3/C/'
cp: omitting directory `/home/susee/databkp/A/4/C/'
Please tell me where i've done mistake.

But the same thing when i'm trying without using wild character its working.

Code:
>find /home/susee/data/A/1/C/ -type f  -name "*.254" | xargs -i cp "{}" /home/susee/databkp/A/1/C/
i think the "-name" should be change to "-iname" can you try?
-prune ... read "man find".

Last edited by RMLinux; 09-11-2008 at 05:12 AM.
 
Old 09-11-2008, 05:25 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
It would probably be better to use a loop.
Code:
for dir in /home/susee/databkp/A/*/C/
do
  cp "$dir"* "${dir/data/datadkp}"
done
It may be easier to see what you are doing if you do it like:
Code:
for dir in 1 2 3 4; do
cp /home/susee/data/A/$dir/C/*.254 /home/susee/datadkp/$dir/C/
done
Thiis assumes that the target directories exist. If not, you can run::
Code:
mkdir -p /home/susee/datadkp/{1..4}/C

One trick is to precede a cp or mv command with "echo" to print out what command would be executed. If it looks good, hit the up arrow and remove the echo.
 
Old 09-11-2008, 11:18 PM   #9
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by RMLinux View Post
i think the "-name" should be change to "-iname" can you try?
-prune ... read "man find".
"-iname" didnt't work
 
Old 09-12-2008, 01:21 AM   #10
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
It would probably be better to use a loop.
Code:
for dir in /home/susee/databkp/A/*/C/
do
  cp "$dir"* "${dir/data/datadkp}"
done
It may be easier to see what you are doing if you do it like:
Code:
for dir in 1 2 3 4; do
cp /home/susee/data/A/$dir/C/*.254 /home/susee/datadkp/$dir/C/
done
Thiis assumes that the target directories exist. If not, you can run::
Code:
mkdir -p /home/susee/datadkp/{1..4}/C

One trick is to precede a cp or mv command with "echo" to print out what command would be executed. If it looks good, hit the up arrow and remove the echo.
thank you sir, its working fine. but can you please explain it clearly interms of source directory and destination deirectory, because my source and destination files are at different locations, not at the same "/home/susee" path.

source dir: /home/susee/data
destination dir : /home/rhl/databkp

can you please explain the same with above dirs.

and going for other option
Code:
for dir in 1 2 3 4....
its very difficult for me becasue there are more than 300 possible files

thanks in advance
 
Old 09-12-2008, 01:45 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Use 'seq' cmd to generate a sequential num list: http://tldp.org/LDP/abs/html/abs-guide.html#EX53
 
Old 09-12-2008, 01:45 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
It will be helpful to you if you understand how and when the shell expands the * wildcard.

When you create a command line such as:

Code:
ls /path/*/stuff
the shell replaces the * with all possible path components that exist under the directory /path and that also have a directory or file named stuff. This is done before the shell actually runs the command. So if the following existed:

/path/1/stuff
/path/2/stuff/anything
/path/3/stuff/morestuff

/path/4/nostuff

the shell would expand the * into only the first three items (marked in blue), but not the fourth item (marked in red). The command the shell would actually execute would be:

Code:
ls /path/1/stuff /path/2/stuff/anything /path/3/stuff/morestuff
The ls command sees 3 command line arguments. It never sees the * that you typed.

The for loop in jschiwal's response:

Code:
for dir in /home/susee/databkp/A/*/C/
works in exactly the same way; the shell expands the * wildcard, so (assuming the sub-directories named XXX, YYY, and ZZZ) the command is expanded (globbed) to become:

Code:
for dir in /home/susee/databkp/A/XXX/C/ /home/susee/databkp/A/YYY/C/ /home/susee/databkp/A/ZZZ/C/
Again, the for loop never sees the * wildcard.

The problem with your original find command is that you were providing the cp command with too many destination directories. Your command looked essentially like this:

Code:
cp source_files targetDir1 targetDir2 ... targetDirN
and cp only supports copying to a single target directory. To copy to multiple target directories, you must run the cp command multiple times, each time copying to one of your target directories.

This is where the loop comes in handy. You copy many source files into one directory, and do it again to the next, etc.

Now, your job is to explain what your source paths look like and what your target directories look like. This will help you see the pattern required to create a loop. The code provided to you assumes a simple pattern of names ranging from 1 to 4.

Last edited by Mr. C.; 09-12-2008 at 01:50 AM.
 
Old 09-12-2008, 02:38 AM   #13
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Dear Mr C,
thanks lot for your outstanding explanation.

I've source files at /home/susee/data/network/station/channel/file1 file2 file3 file4......
and destination files at /home/rhl/databkp/network/station/channel/file1 file2 file3......

let us assume file4 is generated today, at the end of the day, i want to copy this file4 from source to destination dir.
The "network" is variable, which has values like... A, B, C ,D.... upto some 30
In each network we've it's corresponding station files... like network A contains, A1,A2,A3,A4
network B contains, B1,B2
network C contains, C1,C2,C3,C4,C5,C6

like this we've around 300 station files (from all networks)

channel type varies channel1, channel2, channel3 for every station.
in that everyday one new file comes.
so what i was tyring is, take backup of each file at the end of everyday.

i used "for loop" for network variable as it has only 30 values.
But Coudn't that go with station, bcoz it has aroud 300 values.

this is why i used wild character instead using name.

i hope u understood the problem.
 
Old 09-12-2008, 10:40 AM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Let's step back a moment. Do you know about rsync ?
 
Old 09-12-2008, 11:12 PM   #15
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
no,i dont about this "rsync" command
 
  


Reply

Tags
command, cp, directories


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
Omitting directories... WHY??? vonedaddy Linux - Newbie 4 11-23-2007 12:44 AM
Omitting Directory Error carlosinfl Linux - General 3 03-27-2007 04:21 PM
omitting space in shell script Fond_of_Opensource Linux - Newbie 10 11-18-2006 09:18 AM
cp: omitting error jsolanky Linux - Software 10 11-28-2004 09:09 AM
formating console output (omitting parts you do not want) bpk Linux - Newbie 6 06-10-2004 07:32 PM


All times are GMT -5. The time now is 05:47 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration