Basic Unix cp command help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Unix cp command help
# 1  
Old 03-14-2012
Error Basic Unix cp command help

I am new to unix so this is probably a pretty basic question.

I am trying to write several commands on one line that creates a directory called bf in the current directory, then copy all files within that directory and any subdirectories, that do not start with the letter c to the new bf folder.
When it copies the files in the sub directory it must also copy the folder, but copying only the files within it that dont start with c.

so far ive got this which copies everything but im not really sure what to do from here...
Code:
mkdir bf | cp -r * bf

thanks

Last edited by Franklin52; 03-14-2012 at 05:02 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-14-2012
Code:
 
mkdir bf && cp -rf * bf/ && find . -type f -name "c*" -exec rm {} \;

copying all the files to bf directory, then delete the files which starts with c
# 3  
Old 03-14-2012
thanks for your help.

this gives an error - cp: cannot copy a directory, 'bf', into itself, 'bf/bf' and the command puts a folder bf in the newly created bf folder.

what needs changing?
# 4  
Old 03-14-2012
dont create the bf directory in the same directory.
create it some where.. once the copy is completed, move it to your necessary location
Code:
 
mkdir /tmp/bf && cp -rf * /tmp/bf/ && find . -type f -name "c*" -exec rm {} \;

# 5  
Old 03-14-2012
make a bash script of everything

Code:
#vi a.sh 

#!/bin/bash
NEW_BF="/new_bf"
BF="/bf"
mkdir "${NEW_BF}"
cp -pr ${BF}/* ${NEW_BF}
cd  ${NEW_BF}
find . -type f -name c* -exec rm {} \;


Last edited by hedkandi; 03-14-2012 at 03:28 AM.. Reason: correction
# 6  
Old 03-14-2012
The professional approach is to use "cpio" not "cp -r". It will preserve permissions and create subdirectories as required.

Code:
mkdir bf
find . -type f ! -name c\* -print | cpio -pdumv bf

The use of ! to mean "not" is described in "man find".

The combination of "find" and "cpio" is described in the "man" pages for both commands.

As others have realised, this command will only work while the subdirectory "bf" is empty. If you run the command a second time it will of course find files in the subdirectory "bf".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic doubt in UNIX

Hi, I'm new to this and very much interested to learn unix. Can any one explain me the symbols y we use this is scripting(~ and $). It would be great if some one explain with the eg. Thanks Naveen A (2 Replies)
Discussion started by: Pranaveen
2 Replies

2. Solaris

Basic FAQS in unix

can any body tell me this followings in details when do we use this & in which senario we most use this 1.GSD raising 2.MOSFET checks 3.Audit remedation 4.KBS fixes thanks in advance (0 Replies)
Discussion started by: wkbn86
0 Replies

3. Solaris

basic unix question

Hello, I'm new to solaris and have an experience with linux. When we see network interface I can see qfe, hme, le0. What is that mean? Is it depend on the network card? (11 Replies)
Discussion started by: mokkan
11 Replies

4. UNIX for Dummies Questions & Answers

Basic unix command help plz

I was wondering what command lines i could use to do the following. 1. mail a file to a user with a subject line "HELLO". Also, send a Blind carbon copy to a different user? 2. Display the number of files AND directories in a given directory? 3. Display the last 5 files in a given... (4 Replies)
Discussion started by: tragic54
4 Replies

5. Solaris

Basic Unix installation help

Hi, I am a novice in Unix installation. Was experimenting with it. During installation, i created 2 partitions ( what i am calling ). One for the OS which was named SOLARIS & other was named PRI_DOS. Now on completion of installation, where has my PRI_DOS portion gone. How do i... (8 Replies)
Discussion started by: vibhor_agarwali
8 Replies

6. UNIX for Dummies Questions & Answers

Unix basic help

What command would I use to list the first lines of all text files within my Unix directory or within any directory inside there? I was using "find" , "head" and "-exec" commands like this: find ~/Unix -name "*.txt" -exec head {} \; But its not perfectly working, please help me.... (2 Replies)
Discussion started by: carrera911
2 Replies

7. Shell Programming and Scripting

basic unix file command!

Hi there, I am looking to create a single file called outputa.txt, which will show the contents of my directorya and its sub directories, and all file and directory permissions. What command would be used for this? Cheers Kev (2 Replies)
Discussion started by: kev112
2 Replies

8. UNIX for Dummies Questions & Answers

Basic unix

okay, im having some trouble. Go ahead, call me a retard, but i keep getting stuck. Suppose i want to open a Picture of Jesus(for the sake of simplicity) using unix. I type: open Desktop/Pictures/Jesus.jpg It opens, and its all well and good. But, suppose i want to open a picture called Joe... (4 Replies)
Discussion started by: HipCracka
4 Replies

9. Tips and Tutorials

Unix Basic Command

A at : execute commands at a specified time/date. awk: a scripting language, especially useful for manipulating text and automation. B bash : invokes the Bourne Again Shell (standard on most boxes). batch: execute comands when load permits. bc : interactive C-like calcultor (integers... (6 Replies)
Discussion started by: binhnx2000
6 Replies

10. UNIX for Dummies Questions & Answers

Basic Unix tools

I like to know the bare minimum development/ testing tools which can be used with Unix environment wherein the applications are written in different combination - C++ COBOL.. I like to know list of development, performance, testing tools that can be used in Unix . Thanks in advance ls1429 (1 Reply)
Discussion started by: ls1429
1 Replies
Login or Register to Ask a Question