how to copy the directory but not copy certain file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to copy the directory but not copy certain file
# 1  
Old 08-26-2012
how to copy the directory but not copy certain file

Hi experts



Code:
 
cp bin root src /mnt

but not copy bin/bigfile


any help?
( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum)

Thanks
# 2  
Old 08-26-2012
If your find supports the wholename option, and if your cp supports both --parent and -t then this might do the trick. It works for a small directory that I tested with. (These are two big if's)

Code:
find bin root src -type f ! -wholename bin/bigfile | xargs  cp --parent -t /mnt

It will not copy bigfile only from the bin directory; if it exists in other directories it should be copied.

If you want to test this to see what would be copied, make the following changes which will show the copy command for each source file. The list will be one copy per file to make it easy to read even though the command above will not run one copy per file, but will invoke the cp command once for multiple files.

Code:
find bin root src -type f ! -wholename bin/bigfile | xargs -n 1 echo cp --parent -t /mnt

The output will be something like:
Code:
cp --parent -t  /mnt  bin/foo

The -t specifies the target directory allowing it to be placed at the head of the list making invocation with xargs more efficient, but showing the command this way might be confusing since the normal parameter arrangement is source followed by destination.

Another way to do it would be to use tar. This is my 'go-to' solution for such things. I'm unsure as to whether the --exclude option is recognised by all/most versions of tar. If not you can use -X filename, but that requires creating filename and populating it with a list of files to exclude. Not difficult, but not as simple as the example below:

Code:
tar -cf - bin src root --exclude=bin/bigfile | (cd /mnt; tar -xf )

Alternative with -X
Code:
echo "bin/bigfile" >/tmp/exlist.$$
tar -cf - bin src root -X /tmp/exlist.$$ | (cd /mnt; tar -xf )
rm /tmp/exlist.$$

Hope these get you going.
This User Gave Thanks to agama For This Post:
# 3  
Old 08-26-2012
fast solution:
Code:
cp bin root src /mnt; rm /mnt/bin/bigfile

serious: assuming you have bash with "extended pattern matching operators" try (replacing ls with cp):
Code:
ls /root /src /bin/!(bigfile)

# 4  
Old 08-26-2012
Quote:
Originally Posted by RudiC
fast solution:
Code:
cp bin root src /mnt; rm /mnt/bin/bigfile

serious: assuming you have bash with "extended pattern matching operators" try (replacing ls with cp):
Code:
ls /root /src /bin/!(bigfile)


Hi the bigfile is a big file I don't want to copy it to /mnt and then delete it because the /mnt has no big space
# 5  
Old 08-26-2012
Quote:
Originally Posted by yanglei_fage
Hi the bigfile is a big file I don't want to copy it to /mnt and then delete it because the /mnt has no big space
OK, what about the second suggestion? Are you using bash?
# 6  
Old 08-26-2012
Quote:
Originally Posted by yanglei_fage
Hi the bigfile is a big file I don't want to copy it to /mnt and then delete it because the /mnt has no big space
Quote:
Originally Posted by RudiC
OK, what about the second suggestion? Are you using bash?

yes the bash, the second suggestion changed the directory structure
# 7  
Old 08-26-2012
Is it only files directly under ./bin ./root ./src that need to be copied, or do you need to copy the whole directory trees (excluding bigfile)?

If as I suspect you want the whole directory tree excluding bigfile you could try rsync:

Code:
rsync -ax --exclude=bin/bigfile bin root src /mnt

You can also use --max-size=300K with rsync to skip any file bigger than 300KB

Last edited by Chubler_XL; 08-26-2012 at 07:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help - match file name and copy to Directory

I am trying to sort the following files from folder Bag to Apple, Cat Food, Dog Food. I can get all of the files I want into a new folder, but not sure of the best approch to get them to their final directory My Files ========== apple.1234.ext apple.1235.ext cat food 101.ext Cat Food... (2 Replies)
Discussion started by: mtschroeder
2 Replies

2. Programming

how to copy file to a directory

Hello, I've been spending a lot of hours trying to imitate cp copying a file to a directory. cp I just can't seem to write to a specified directory, it only creates a copy on the current directory. any hints/tips will help! Thanks! here's the code i've been trying to manipulate: ... (1 Reply)
Discussion started by: l flipboi l
1 Replies

3. Shell Programming and Scripting

Copy file after searching in a directory

Hi, I am looking for an answer for following senario: I have a text file (base.txt) which consist list of files to be searched like: base.txt abc.txt def.txt fgh.txt Now i am going to search all the listed files in another directory after reading them one by one, once i found the... (10 Replies)
Discussion started by: apjneeraj
10 Replies

4. UNIX for Dummies Questions & Answers

How to copy a file to a directory?

Hello all, I've been researching this problem for days, and have gotten no luck . =/ How do you copy a file to another directory without being in the same directory as the file? So, for example, say I wanted to copy the file 'my.txt' that is in the directory ' /export/hom0/user/asdf ' to the... (9 Replies)
Discussion started by: kvnqiu
9 Replies

5. Shell Programming and Scripting

find and copy file to another directory..

Hi Everybody, i want a samll help to write a script. i had source location with :/user/bin (bin contains subdirectories with like names emails etc and had several files in each subdirectory) and target location with :/usr/scripts (having same subdirectories names and had some files)... (1 Reply)
Discussion started by: Reddy482
1 Replies

6. Shell Programming and Scripting

Copy contents of a directory only if a file exists

I'm looking to write a script that will check the contents of a directory, and if any files exist in that directory copy them to a temporary folder. The target files are only resident for a few seconds, so I think the script needs to be running constantly. Any pointers would be really... (3 Replies)
Discussion started by: danceofillusion
3 Replies

7. Shell Programming and Scripting

Copy the latest file to a directory

Hi Team, I wish to copy the latest file of pattern "MyFile*" to some other location. I need to do all the operation in a single command separated by |. ls -rt <MyFile*> | tail -1 | <copy command>. How can I do? Please help me. Thanks, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

8. Shell Programming and Scripting

Copy the latest file from one directory to another

Hi All, I am in the directory a/b/processed the files in this directories are -rw-r--r-- 1 owb users 330 Aug 8 chandantest.txt_08082008 -rw-r--r-- 1 owb users 220 Aug 7 chandantest.txt_07082008 -rw-r--r-- 1 owb users 330 Aug 6... (3 Replies)
Discussion started by: chandancsc
3 Replies

9. Solaris

Copy files from the file to another directory

I have created a file that has list of all the files I want to copy into another directory.Is there a way to do it? Thanks In advance (4 Replies)
Discussion started by: shreethik
4 Replies

10. UNIX for Dummies Questions & Answers

copy file to a directory by sequence

Hi I am new in unix and look for help in urgent. I have a list of data files that located in a directory, and need to copy to another directory for loading. The condition here is, the list of data files has to be copy over by sequence, and if there is no file in targetted directory already. ... (4 Replies)
Discussion started by: fooky
4 Replies
Login or Register to Ask a Question