move files that match specific conditions


 
Thread Tools Search this Thread
# 1  
move files that match specific conditions

Hi all,

I'm new to this forum and bash scripting. I have the following problem, I need to copy some files (from one dir. to another) whose first 5 numbers (subjects' ID) match the directory names. Here a shortened version of what I'm trying to do:

names=(32983_f 35416_f 43579_f) # these are the names of directories where I want to copy the files to

for i in ${names[@]}
do


if [$i = /Users/name/Dropbox/$i_exp.txt]; then

# _exp.txt is the constant part of the files I want to move

cp /Users/name/Dropbox/$i_exp.txt

/Users/name/Desktop/SUBJ/$i
# I want that this file is copied in the corresponding directory (with the same initial 5 numbers as the file)

fi
done


I hope it's clear. Like this obviously doesn't work it says

cp.sh: line 6: [32983_f: command not found,

Could you help me?
Best

Ada

Last edited by ada1983; 10-24-2012 at 12:29 PM..
# 2  
try:
Code:
 
names=(32983_f 35416_f 43579_f) # these are the names of directories where I want to copy the files to
for i in ${names[@]}
do
  if [ -f /Users/name/Dropbox/$i_exp.txt ]; then
    # _exp.txt is the constant part of the files I want to move
    [[ -d /Users/name/Desktop/SUBJ/$i ]] || mkdir -p /Users/name/Desktop/SUBJ/$i
    cp /Users/name/Dropbox/$i_exp.txt /Users/name/Desktop/SUBJ/$i
    # I want that this file is copied in the corresponding directory (with the same initial 5 numbers as the file)
  fi
done


Last edited by rdrtx1; 10-24-2012 at 01:15 PM.. Reason: fixed spacing around square brackets.
# 3  
Quote:
Originally Posted by rdrtx1
try:
Code:
 
names=(32983_f 35416_f 43579_f) # these are the names of directories where I want to copy the files to
for i in ${names[@]}
do
  if [-f /Users/name/Dropbox/$i_exp.txt]; then
    # _exp.txt is the constant part of the files I want to move
    [[ -d /Users/name/Desktop/SUBJ/$i ]] || mkdir -p /Users/name/Desktop/SUBJ/$i
    cp /Users/name/Dropbox/$i_exp.txt /Users/name/Desktop/SUBJ/$i
    # I want that this file is copied in the corresponding directory (with the same initial 5 numbers as the file)
  fi
done

I can see some spacing problems with test right away.
# 4  
Hi Thanks for tour reply,

It still doesn't work, it says

cp.sh: line 6: [-f: command not found
# 5  
Code:
 
names=(32983_f 35416_f 43579_f) # these are the names of directories where I want to copy the files to
for i in ${names[@]}
do
  if [ -f /Users/name/Dropbox/$i_exp.txt ] ; then
    # _exp.txt is the constant part of the files I want to move
    [[ -d /Users/name/Desktop/SUBJ/$i ]] || mkdir -p /Users/name/Desktop/SUBJ/$i
    cp /Users/name/Dropbox/$i_exp.txt /Users/name/Desktop/SUBJ/$i
    # I want that this file is copied in the corresponding directory (with the same initial 5 numbers as the file)
  fi
done

Sorry, include spaces around square brackets.

Last edited by rdrtx1; 10-24-2012 at 01:16 PM..
# 6  
Quote:
Originally Posted by elixir_sinari
I can see some spacing problems with test right away.
Test? Can you be a bit more explicit? Sorry I'm not used to bash yet

---------- Post updated at 06:15 PM ---------- Previous update was at 06:10 PM ----------

ok, yes I see it! I added one space before the -f and at the end of that statement inside the []. It doesn't give me any error now. But it still doesn't do the job, I think I will find my way but can I ask you what is the role of the -f in front and the -d ?

Thanks

Ada
# 7  
-f tests if file exits. -d tests if directory exists.

Last edited by rdrtx1; 10-24-2012 at 01:38 PM..
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 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. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Insert a newline after match in files of specific name under some subdirectories?

Hi I'd like to add the newline: \tuser: nobody", or "<TAB>user: nobody to all files named: docker-compose.ymlin subfolders of pwd with names beginning with 10-20. Within these files, I'd like to find the line (there'll only be one) containing: command: celery workerNOTE: As far as... (2 Replies)
Discussion started by: duncanbetts
2 Replies

5. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

7. Shell Programming and Scripting

How to match the below conditions?

Hello All, I have 2 files with around 2k records.. I am lost how to build a script to create another file with the below:confused::eek: Match Condition1: File1's 12th Columns data should exact match with File2's 19th Column's data Match Condition2: File1's 28th Column's data format is... (12 Replies)
Discussion started by: ailnilanjan
12 Replies

8. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

9. Shell Programming and Scripting

Find files that do not match specific patterns

Hi all, I have been searching online to find the answer for getting a list of files that do not match certain criteria but have been unsuccessful. I have a directory that has many jpg files. What I need to do is get a list of the files that do not match both of the following patterns (I have... (21 Replies)
Discussion started by: nikos-koutax
21 Replies

10. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies

Featured Tech Videos