Moving files with specific names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Moving files with specific names
# 1  
Old 10-27-2013
Moving files with specific names

Hi,
I have a list of names (in a text file) like this:
Code:
SRR1234
SRR5678
SRR4321
SRR8876

I'd like to have a unix code to find all the files which have any of above strings in their name and move them to a specific directory. I have my files distributed in many subdirectories so it has to search them all.
Thanks
# 2  
Old 10-27-2013
Try:
Code:
xargs -I% find /source -name "*%*" -exec mv {} /destination \; < list.txt


Last edited by bartus11; 10-27-2013 at 05:18 PM.. Reason: added pattern matching
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-27-2013
Code:
cat list.txt | while read pattern; do find sourcedir -name "*$pattern*" -exec mv '{}' destdir/ \;;done

Emanuele
This User Gave Thanks to targzeta For This Post:
# 4  
Old 10-27-2013
Quote:
Originally Posted by a_bahreini
I'd like to have a unix code to find all the files which have any of above strings in their name and move them to a specific directory. I have my files distributed in many subdirectories so it has to search them all.
Have you made any attempt to solve the problem yourself? If so, show us your code. If not, did you at least search the forum? This has been answered before.



Quote:
Originally Posted by bartus11
Code:
xargs -I% find /source -name "*%*" -exec mv {} /destination \; < list.txt

Quote:
Originally Posted by targzeta
Code:
cat list.txt | while read pattern; do find sourcedir -name "*$pattern*" -exec mv '{}' destdir/ \;;done

There is no need to traverse the filesystem hierarchy more than once, much less once per pattern. A much more efficient approach runs find only once, piping its output into grep. Whatever survives is moved.

The OP did not address basename collisions. If they're a possibility, then clobbering needs to be addressed.

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Red Hat

Moving files with specific dates

Hi, These are the list of files in one directory in the server : # ls -lrt total 10120 -rw-r--r-- 1 root root 4484 Jul 8 2011 install.log.syslog -rw-r--r-- 1 root root 51890 Jul 8 2011 install.log -rw------- 1 root root 3140 Jul 8 2011 anaconda-ks.cfg drwxr-xr-x 2 root root... (2 Replies)
Discussion started by: anaigini45
2 Replies

3. Shell Programming and Scripting

Remove all files with specific file names in directory

If I have 5 files in a directory, what is the best way to remove specific files in it? For example, snps.ivg probes.ivg Desired output probes.ivg probes.txt all.txt Basically, removing those files with "snp" in the filename regardless of extension. Thank you :). (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Homework & Coursework Questions

Shell Scripting , Moving Old file to specific folder

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: There are files stored like 14.Aug.2014.log, 15.Aug.2014.log etc. in a folder $HOME/log you need to find out all... (4 Replies)
Discussion started by: shajoftaj
4 Replies

5. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

6. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

7. Shell Programming and Scripting

Help with moving files with same names from subdirectories?

I was wondering if someone could help me with this: I have multiple directories and subdirectories with files in them. I need to move all files from all directories and subdirectories into one root directory. However, when i do find /home/user -mindepth 1 -iname "*" -type f -exec mv {} . \; ... (3 Replies)
Discussion started by: r4v3n
3 Replies

8. Shell Programming and Scripting

Moving specific files

Hello, I am trying to move specific files to a specific folder. I have a virus script that runs and quarantines the files by changing the ownership to -r--------. This has worked fine but I am wanting to actually move the infected files to a folder called quarantine. I have came up with a basic... (4 Replies)
Discussion started by: Stud33
4 Replies

9. UNIX for Dummies Questions & Answers

Moving files and changing names

i have many files with extention filename.ASN_ERROR~ at a path. I want to move these files to another path and change extension to .ASN There are more then 80,000 files so i cant use manual commands muneebr (5 Replies)
Discussion started by: muneebr
5 Replies

10. UNIX for Advanced & Expert Users

Moving specific data between databases

Dear All, I have 2 databases, There is a lot of data in both the databases, i would like to move some data from one database to the other. I would like to accept 2 parameters from the user, i.e. emplyee id & dept, on entering the 2 i will unload all the data from the tables to the flat files.... (2 Replies)
Discussion started by: lloydnwo
2 Replies
Login or Register to Ask a Question