copying files with wildcards


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copying files with wildcards
# 1  
Old 02-15-2012
Bug copying files with wildcards

Hello, I am attempting to copy a series of files using a wildcard into a new subdirectory. however, I am clearly doing something wrong as it is not working. I want to copy all files in the directory that start with the letters kl but have other letters after this initial two letters into another directory.

here is what I have tried so far. I admit that I am not strong in Unix


Code:
cp - r  /home/jgold/smwcv/error_infect/test/surfaces/threestage/kl*   /home/jgold/tmp
-bash: /bin/cp: Argument list too long

altix350:Wed Feb 15:jgold[5002] ~/smwcv/error_infect/test/surfaces/threestage > cp "kl*" /home/jgold/tmp
cp: cannot stat `kl*': No such file or directory

altix350:Wed Feb 15:jgold[5003] ~/smwcv/error_infect/test/surfaces/threestage > cp kl* /home/jgold/tmp
-bash: /bin/cp: Argument list too long

altix350:Wed Feb 15:jgold[5004] ~/smwcv/error_infect/test/surfaces/threestage > cp -r  kl* /home/jgold/tmp
-bash: /bin/cp: Argument list too long

altix350:Wed Feb 15:jgold[5005] ~/smwcv/error_infect/test/surfaces/threestage >
find . -type f -name "kl*" cp "kl*" /home/jgold/tmp;

Also once this is done I want an easy way to move these files from the remote unix server to my desktop.

Last edited by methyl; 02-15-2012 at 05:32 PM.. Reason: correct code tags an lay it out a bit
# 2  
Old 02-15-2012
hi,
there are many similar posts here, i.e. have a look at:
https://www.unix.com/unix-dummies-que...-long-ssh.html

see ya
fra
This User Gave Thanks to frappa For This Post:
# 3  
Old 02-15-2012
You didn't do anything wrong exactly, but there were just too many filenames to cram into one cp call. Handling that many filenames gets a bit more complicated. Too many arguments is too many arguments -- ls kl* or any other variations like that won't work either. You'll need to print a full listing with ls, filter what you want with grep, and read in a loop. Put them through a stream so we can read them one at a time in other words, instead of trying to cram them all into one line.

Code:
ls /home/jgold/smwcv/error_infect/test/surfaces/threestage/ |
grep "^kl" |
while read LINE
do
        echo cp -r /home/jgold/smwcv/error_infect/test/surfaces/threestage/"$LINE" /home/jgold/tmp
done

Notice how grep takes a different kind of expression than you'd do in the shell. It doesn't match the entire line, it can match part of the line anywhere, so we have to stick a ^ in the front to tell it "only look for kl at the beginning of the line". And we don't have to match the rest of it since knowing it has a 'kl' in front is good enough.

Then we read lines one by one and feed them into cp one by one. Remove the 'echo' once you've tested it and are sure it does what you want.
# 4  
Old 02-15-2012
This should be be more robust:
Code:
cd /home/jgold/smwcv/error_infect/test/surfaces/threestage/
find . -type f -name "kl*" -print | while read filename
do
       cp -p "${filename}" /home/jgold/tmp
done


Last edited by methyl; 02-15-2012 at 05:38 PM.. Reason: no cd!
This User Gave Thanks to methyl For This Post:
# 5  
Old 02-15-2012
Quote:
Originally Posted by methyl
This should be be more robust:
Code:
/home/jgold/smwcv/error_infect/test/surfaces/threestage/
find . -type f -name "kl*" -print | while read filename
do
       cp -p "${filename}" /home/jgold/tmp
done

That will flatten any directories it finds! All files will end up dumped in one big folder.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 02-15-2012
@Corona688 I quote from post #1:
Quote:
I want to copy all files in the directory that start with the letters kl but have other letters after this initial two letters into another directory.
No mention of subdirectories.

(My reference to "more robust" was of course referring to the examples in post #1).

Quote:
I want to copy all files in the directory that start with the letters kl but have other letters after this initial two letters into another directory.
The more times I read this sentence the more ambiguous it becomes. Is it the filenames which start with "kl" or the directory names which start with "kl" I wonder?
I've gone for the files and Corona688 has gone for the directories.

Last edited by methyl; 02-15-2012 at 06:06 PM..
# 7  
Old 02-15-2012
Quote:
Originally Posted by methyl
@Corona688 I quote from post #1:


No mention of subdirectories.
He used -r, which implied he needed it, meaning some had to be directories. He might have just put it on habitually.

Your command would recursively hunt down all files named 'kl*' even if it was nested 5-deep in the current folder, not just the current folder where he wanted it to look.
Quote:
I've gone for the files and Corona688 has gone for the directories.
My solution works for both...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files

I'm trying to do this exact same thing, so far I have created this to move files i've named my script CP.sh #!/bin/bash cd /root/my-documents/NewDir/ for f in *.doc do cp -v $f root/my-documents/NewDir $f{%.doc} done When i go to run this in the console i type, bin/sh/ CP.sh but it... (7 Replies)
Discussion started by: MKTM_93_SIMP
7 Replies

2. Shell Programming and Scripting

Copying files

All, I need to grab and rename common files from several unique directory structures. For example, the directory structures looks like: /unique_dir/common/common/common/person_name_dir/common_file.txt There are over 90,000 of these text files that I'd like to put in a single directory as... (5 Replies)
Discussion started by: hburnswell
5 Replies

3. Shell Programming and Scripting

Files copying - [ Listed files alone. ] - Shell script

Hi All, I am doing this for svn patch making. I got the list of files to make the patch. I have the list in a file with path of all the files. To Do From Directory : /myproject/MainDir To Directory : /myproject/data List of files need to copy is in the file: /myproject/filesList.txt ... (4 Replies)
Discussion started by: linuxadmin
4 Replies

4. Shell Programming and Scripting

Copying Files

Hi All, I'm trying to list some files from my log directory and files are like this log.20110302_20.gz log.20110302_21.gz log.20110302_22.gz log.20110302_23.gz log.20110303_00.gz log.20110303_01.gz log.20110303_02.gz ............ log.20110311_22.gz log.20110311_23.gz... (2 Replies)
Discussion started by: thelakbe
2 Replies

5. Shell Programming and Scripting

Perl, open multiple files with wildcards

I have a question regarding Perl scripting. If I want to say open files that all look like this and assign them to a filehandle and then assign the filehandle to a variable, how do I do this? The file names are strand1.fa.gz.tmp strand2.fa.gz.tmp strand3.fa.gz.tmp strand4.fa.gz.tmp ...... (6 Replies)
Discussion started by: japaneseguitars
6 Replies

6. Shell Programming and Scripting

rename multiple files with wildcards

Hi All I am having hundred over file in the below pattern. AA050101.INI BB090101.INI . . ZX980101.INI Need to rename these files with an extension .bak AA050101.INI.bak BB090101.INI.bak . . ZX980101.INI.bak (5 Replies)
Discussion started by: karthikn7974
5 Replies

7. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

8. Shell Programming and Scripting

copying files

hi I want to copy all files from the current directory and move to .archive file. Moreover,I want to add .bak to each file name, that will be copied. How can I do that? (4 Replies)
Discussion started by: tjay83
4 Replies

9. Shell Programming and Scripting

Using cat to combine files using wildcards

How do I use cat (presumably with a sh script) to combine all the files in a directory without listing them individually. Thank you for your patience with this very elementary question.:) (3 Replies)
Discussion started by: Enobarbus37
3 Replies

10. UNIX for Dummies Questions & Answers

Move files using wildcards ???

Hi all, Would like to rename all files using wildcards - if at all possible! As an example I have the following files: Nov01_df Nov02_df Nov03_df ...... Nov28_df Nov29_df Nov30_df I'd like to have these renamed as "df??" where ?? is the number from the original file name. Any... (5 Replies)
Discussion started by: Cameron
5 Replies
Login or Register to Ask a Question