Copy files matching multiple conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files matching multiple conditions
# 1  
Old 10-16-2013
Copy files matching multiple conditions

Hello

How do i copy files matching multiple conditions. Requirement is to search files starting with name abc* and def* and created on a particular date or date range given by the user and copy it to the destination folder.

i tried with different commands.

below one will give the list , but don't know how to copy.

Code:
ls -l | grep -e "abc" -e "def" |grep -i "May 21"

below one copies on one condition, don't know how to insert multiple condition

Code:
find -iname "abc*" -exec cp -i -t ~/test {} +

# 2  
Old 10-16-2013
Have you considered something like:-
Code:
grep -i "May 21" *abc* *def*

The output will list the filenames found followed by the matching record. Have a look at the -h & -l flags of grep if you want to exclude wither of these.

I hope that this helps.

Robin
Liverpool/Blackburn
UK
# 3  
Old 10-16-2013
Quote:
Originally Posted by NarayanaPrakash
below one copies on one condition, don't know how to insert multiple condition

Code:
find -iname "abc*" -exec cp -i -t ~/test {} +

You can specify an OR in the find conditions - find \( -name "abc*" -o -iname "def*" \) -exec stuff....
# 4  
Old 10-16-2013
Thanks Carol

but it throwing me error
Code:
Usage: find [-H | -L] Path-list [Expression-list]

# 5  
Old 10-16-2013
Must be a user error, my name's not Carol! Smilie

What's your OS? If it's SunOS/Solaris then try /usr/xpg4/bin/find.

EDIT: Actually, it's probably just the Path-List that was missing from your original command. Try:
Code:
 find . \( -iname "abc*" -o -iname "def*" \) -exec cp -i -t ~/test {} \;


Last edited by CarloM; 10-16-2013 at 09:06 AM.. Reason: filename typo
# 6  
Old 10-16-2013
The first parameter of find is a directory name. It can be a dot for the current directory or should be a forward slash for directory names not the backslash and for a simple or list you can drop the brackets.

Try:-
Code:
find / -name "abc*" -o -iname "def*" -exec stuff....

Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy subfolder and files to matching directory

The bash executes but returns no results and the set -xv showed while the $run variable in blue, was extracted correctly, the $match value in green, was not, rather both values in home/cmccabe/Desktop/f1 were extracted not just the matching. There will always be an exact match from the $run to... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

4. Shell Programming and Scripting

Matching multiple fields from two files and then some?

Hi, I am working with two tab-delimited files with multiple columns, formatted as follows: File 1: >chrom 1 100 A G 20 …(10 columns) >chrom 1 104 G C 18 …(10 columns) >chrom 2 28 T C ... (4 Replies)
Discussion started by: mbp
4 Replies

5. Shell Programming and Scripting

Help with matching entries in multiple files

Hi, I am pretty new to Linux and I have a question. I have 3 tab delimited text files which look like this: FileA: PROTEINID DESCRIPTION PEPTIDES FRAMES GB://115298678 _gi_115298678_ref_NP_000055.2_ complement C3 precursor 45 55 GB://4502027 ... (8 Replies)
Discussion started by: Vavad
8 Replies

6. UNIX for Dummies Questions & Answers

copy all files matching the request and change the extension at the same time

Hi everyone When I'm starting my script I'm giving to it two parameters: script.sh ext1 ext2 I need to copy all files in a directory fitting ext1, to the same folder, with the same names, but with the changed extension to ext2. Till now I've just managed to do it for only 1 file, but I... (16 Replies)
Discussion started by: vacuity93
16 Replies

7. UNIX for Dummies Questions & Answers

copy multiple files

Hi, I am facing this problem, however i am not finding any solution. Kindly help I have the list of files to be search , i need to search for those files and copy the files to a folder. Really its urgent. MG_0281.JPG Tdfa_0077.JPG The%20SirehSet%20Geduing%20KpgGlam%20.jpg... (4 Replies)
Discussion started by: umapearl
4 Replies

8. UNIX for Dummies Questions & Answers

Copy multiple files

Hi i have 1000 files is a directory, which are serially numbered (file1,file2,file3...). I would like to copy every 200 files to different directories. many thanks in advance. (6 Replies)
Discussion started by: saint2006
6 Replies

9. Shell Programming and Scripting

Pattern matching for following conditions

Hi all, i want a pattern which satisfies the following conditions like a-z A-Z 0-9 / or _ and it's limit of 64 characters since i tried using $1="/var" grep -E '{64}' "$1" When i tried this for bourne shell, it shows error because grep cannot be used of -E. suggest me if u have... (2 Replies)
Discussion started by: Nandagopal
2 Replies

10. Shell Programming and Scripting

[Help]RegEx, Putty, Copy Files Insensitive Matching, SSH

Alright, basically we're in the whole where we can't tar/gzip a folder since its to big so how do I copy files to a new folder for example I got files from a-Z, i want to copy all files which starts with a A or a into another folder heres file structure ./backups/A ./backups/B... (11 Replies)
Discussion started by: Lamonte
11 Replies
Login or Register to Ask a Question

Featured Tech Videos