help with simple unix file copy and output in directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help with simple unix file copy and output in directories
# 1  
Old 09-15-2009
Data help with simple unix file copy and output in directories

Hi, I am fairly new to unix, and am trying to copy all files with the name "*.cons" within a directory (and all of the many directories within it) to a new directory called "output". There are multiple nested directories, and I would like to just pull out the files with ".cons" and not the other stuff. Do I use the "find ./ -name" option? Can anyone help me?
# 2  
Old 09-15-2009
You might want to read this thread for a short introduction to "find".

I hope this helps.

bakunin
# 3  
Old 09-16-2009
Thank you. I tried: find ./ -iname "*.cons" -type f -exec mv {} /consensus \;
but it doesn't seem to work...
# 4  
Old 09-16-2009
Not bad for the first try. You were almost there:

Quote:
Originally Posted by euspilapteryx
find ./ -iname "*.cons" -type f -exec mv {} /consensus \;
Lets start with the starting directory: you probably want to start in the current working directory, which is always named "." If this is the case you shouldn't obscure this pathname with additional slashes - in the best case this alters the path to something else, rendering it uninterpretable for the system in the worst case.

Btw., it is a good idea to "build gradually" towards the correct command. This way you easily find out where you have gone wrong. We start by simply printing what we find and use "more" to pagewise read output (read the mapage of "more" to see what it does):

Code:
find . -print | more

Ok, this seems to work. Now we want only the files, leaving out the directories:

Code:
find . -type f -print | more

The order of the clauses for "find" doesn't matter (not in your case), but it is a good idea to keep some sort of order to be able to read it easily. I made a habit of always put the "output clause" ("-print" in this case) at the end and the limiting clauses first. Whatever you do, be consistent with yourself.

As next step we limit the files (we already deal with files only, because of the "-type" clause) to the ones which match out name criteria. The clause for that is "-name" (not "-iname", as you wrote, probably a typo):

Code:
find . -type f -name "*.cons" -print | more

Just an observation: in DOS/Windows all files have a filename and an extension. These parts of the filename are divided by a dot. In UNIX there is no such thing as an extension and the dot (if there is one) is a simple part of the name. This makes not a big difference here, but is good to know, because sometimes people implicitly assume the naming convention of DOS/Windows to be in effect, which doesn't have to be the case.

We seem to have gotten all the files we want and left out all the files we don't want (yes? - if not, we will have to start over) and get to the hard part: moving the files. Again we first want to see what we might do to the files and only then do it in fact. There lets "mask" the command with an "echo" command to just print it instead of executing:

Code:
find . -type f -name "*.cons" -exec echo mv {} /consensus \; | more

The output should look like this:

Code:
user@host:/some/where $ find . -type f -name "*.cons" -exec echo mv {} /consensus \; | more
mv ./a.cons /consensus
mv ./b.cons /consensus
mv ./c.cons /consensus
mv ./d.cons /consensus
.
.
.
-More-

If the commands look like the ones you really want to execute remove the "echo" in the "-exec" part and execute the "find"-command once more to actually do it. You should be done. If you are not it will be things outside the scope of "find": does the directory "/consensus" already exist? Are your rights sufficient to write there? Is there enough space in the filesystem to move the files there? Are you allowed to access the files you want to move or only their directory information? Etc., etc.

I hope this helps.

bakunin
# 5  
Old 09-16-2009
If you want to retain the directory tree structure in the target directory it is easier with "find" combined with "cpio -p". See "man find" and "man cpio".

This post is a start:

https://www.unix.com/shell-programmin...g-folders.html
# 6  
Old 09-16-2009
Thank you very much! This is very helpful!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching between directories and mkdir/copy dir/file

I was trying to copy the files inside the path /home/user/check/Q1/dir/folder1/expected/n/a1.out1 and a1.out2 and a1.out3 to /home/user/check/Q2/dir/folder1/expected/n/ if n directory is not present at Q2/dir/folder1/expected/ then directory should be created first. And, script follow the... (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Copy directories in make file

LD:=C:/WindRiver/diab/5.9.3.0/WIN32/bin/dld.exe CFILES:=$(wildcard *.c) OBJFILES:=$(subst .c,.o, $(CFILES)) OBJ_PATH:=$(PRJ_PATH)/out/ ADDOBJFILES := $(addprefix $(OBJ_PATH),$(OBJFILES)) FILES:=C:/EB/tresos/workspace/Test_Spi/output/src copyfiles: cp ... (3 Replies)
Discussion started by: ushacy
3 Replies

3. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. UNIX for Dummies Questions & Answers

copy file using unix in multiple directories

Hi All Genious, I want to copy a file name XYZ .In a directory /HOME/dir/IXOS1/dir1 which contain multiple directories named not in pattern want to copy the XYZ in all of the directories available on path /HOME/dir/IXOS1/dir1 . Thanks in advance . (2 Replies)
Discussion started by: mumakhij
2 Replies

5. Shell Programming and Scripting

copy tail output to an external file in perl

The below code works to tail client.log file. but I want to copy the output into an external file /home/tail.txt Can anyone please help. #!/opt/bin/perl -w open my $pipe, "-|", "/usr/bin/tail", "/var/log/client.log" or die "could not start tail on /var/log/client.log : $!"; print while... (2 Replies)
Discussion started by: sureshcisco
2 Replies

6. UNIX for Dummies Questions & Answers

Help me with this simple file copy from Unix

Hello, Imagine I am on a Unix system, connected to server via telnet. I do 'ls' and I see the file listed - OK. Filename=ems.log. Then I need to copy such file from this server to my Windows network share \\windowsserver\myshare\smith. How can I do that? (2 Replies)
Discussion started by: marlon2010
2 Replies

7. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

8. UNIX for Dummies Questions & Answers

Copy single file to multiple directories

Please help - I need to copy a single file to multiple directories. Dir structure: Parent_Directoy Filename1 Child_Directory1 Child_Directory2 Child_Directory3 Child_Directory4 .... So I need to copy Filename1 to all of the... (2 Replies)
Discussion started by: kthatch
2 Replies

9. UNIX for Dummies Questions & Answers

I want to copy the text output from a 'nohup.out' file.

Hello, I have a nohup.out file that, when executed, outputs a spreadsheet file with four-to-seven columns of number. I want to copy this output (in its entirety), so that I could then paste it on excel@ , or Notepad@. Please help, thanks. (3 Replies)
Discussion started by: Iamthe great
3 Replies

10. AIX

Copy Directories in to UNIX server

I have software on Z drive " I map it " , its about 50 MB , I want to copy the whole directory in to unix server , under some folder ... What are the steps how Ican perform this ? I have UNIX user name and password and rights to create files and folders.. Remember I want to copy all the folder... (3 Replies)
Discussion started by: khan1978
3 Replies
Login or Register to Ask a Question