Locate the files in the first column and copy the files in 2nd column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Locate the files in the first column and copy the files in 2nd column
# 15  
Old 03-07-2018
Quote:
Originally Posted by RudiC
Your statement / request is not quite clear. You use locate yourself in post#1. Do you want all files located (including the ones whose file names are supersets of the search term) to be copied to your target directory given in your data file? If several files with identical file names exist, they will overwrite each other - which one should survive?
To give you a starting point, you might want to consider / analyse this:
Code:
while read FN DN
    do  [ -d $DN ] || echo mkdir $DN
        for LN in $(locate $FN)
          do    [ ! $FN = ${LN##*/} ] && continue
                echo cp $LN $DN 
          done
    done < datafile

It will check the resp. file name against the one in datafile and copy only if identical, but will not check for overwriting. Directories are checked for existence and created if non-existent. It will of course depend on the locate-DB to be up to date, and on those names not containing white space as these would confuse the for loop.
Give it a try an comment back


Instead of locate i change it to "find /specific_directory -name"
Hi Rudi, care to share why it worked? Do you mind explaning it line by line please? Also why While? WHy not another for loop?
THanks

---------- Post updated at 12:21 PM ---------- Previous update was at 12:18 PM ----------

THis code belongs confuse me
Code:
while read f d

is the same with

Code:
awk '{print $1, $2}'

---------- Post updated at 12:25 PM ---------- Previous update was at 12:21 PM ----------

Quote:
Originally Posted by Aia
Not truly hard. The hard part is for you to recognize the way that you discern what files need to be copied when you do it manually and communicate it in a way that can be translated into an automation script without getting unexpected results. I hope I have clearly pointed out that accepting the result from locate is not it.

For example the snippet below it might be alright if you understand that locate MUST never return a matched directory, a partial match in filename or directory name and never returns neither with spaces on them. Otherwise, you MUST accommodate for those conditions.




Would this work?
Code:

homepath="/home/data"
while read f d; do
    if [[ -n $f ]] && [[ -n $d ]]; then
        paths=$(locate $f)
        while read -r p; do
            if [[ -f $p ]] && [[ ! -f ${homepath}/${d}/${p##*/} ]]; then
                 mkdir -p "${homepath}/${d}" && cp "$p" "${homepath}/${d}"
            fi
        done <<< "$paths"
    fi
done < data.txt


Please explain why,
Code:
while read f d

will correspond to column 1 and column 2 in a file. If you can explain that to me. I will not rely on for loop anymore. ill start writing script with while.. i can do if statement and for loop..simple scripting for linux admin...but that while is really help ful,but need to understand why
# 16  
Old 03-07-2018
A for loop is _almost_ never a good choice to iterate over lines in a file, nor for dealing with file/directory paths since the default separator is white space. That means that if a filename or path contains spaces it might give you an undesirable result.

The read built-in command is good at reading lines and better at tokenization of the lines.
If you give it one variable, it will read the whole line into it.
If you give it two variables it will split the line into two substrings at the first space
If you give it three variables it will split the line into three substrings at the first and second space.
And so on.
read doesn't loop, it reads once. Thus the while loop to help read to imitate an iteration of all lines.

Summary:

Iterate over every line in datafile, splinting it into two tokens: f (filename) d (directoryname). The f and d could be any place holder names.
Code:
while read f d; do ... done < datafile

---------- Post updated at 10:14 AM ---------- Previous update was at 09:54 AM ----------

Quote:
Originally Posted by kenshinhimura

---------- Post updated at 12:21 PM ---------- Previous update was at 12:18 PM ----------

T[h]is code confuse[s] me
[...]
Code:
awk '{print $1, $2}'

It would require that you know a bit about how AWK works. In this example each record is indexed into fields using white spaces as field separator and it is asking it to just output the values of fields one and two.
These 2 Users Gave Thanks to Aia For This Post:
# 17  
Old 03-07-2018
Quote:
Originally Posted by kenshinhimura
. . .
Hi Rudi, care to share why it worked? Do you mind explaning it line by line please? Also why While? WHy not another for loop?
THanks
. . .
Little to add to Aia's excellent explanation. man bash and man awk help, as almost always. My proposal was a "proof of concept", not a full blown solution to your (not quite understood) problem.

Code:
while read FN DN                                        # read TWO fields from stdin (here redirected from datafile: file name - directory name)
    do  [ -d $DN ] || echo mkdir $DN                    # test for existence of target directory; create if missing (remove echo)
        for LN in $(locate $FN)                         # loop through matching names from locate-DB
          do    [ ! $FN = ${LN##*/} ] && continue       # test for non-exact match; skip cp if not identical
                echo cp $LN $DN                         # copy located name to target directory (echo for testing purposes; remove if OK)
          done                                          # end of for loop
    done < datafile                                     # end of while loop incl. redirection from datafile

# 18  
Old 03-07-2018
Not sure how you want the script to react if locate identifies more that one exact match for a file. RudiC's current script will silently overwrite the file with each subsequent identified match.


If you would like to keep the first match found and then move on to the next file replace:

Code:
echo cp $LN $DN                    # copy located name to target directory (echo for testing purposes; remove if OK)

with

Code:
echo cp $LN $DN && break           # copy located name to target directory (echo for testing purposes; remove if OK) and move onto next file


You could also detect the file was already there and report some sort of warning like Warning: Not replacing existing file xxx with /path/to/xxx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy files if column 5 in a file contains ā€œVā€

I have number of csv files (like tmo_2019*). In these files some files have 5th column value as V. I want to copy those files having 5th column value as V to specific directory /test/V_files/. I tried to extract file names by below but not able to complete command for copy. find -type f -iname... (4 Replies)
Discussion started by: Bops
4 Replies

2. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

3. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

4. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

5. Shell Programming and Scripting

Join 2nd column of multiple files

Dear All, I have many files formatted like this: file1.txt: 1/2-SBSRNA4 18 A1BG 3 A1BG-AS1 6 A1CF 0 A2LD1 1 A2M 1160 file2.txt 1/2-SBSRNA4 53 A1BG 1 A1BG-AS1 7 A1CF 0 A2LD1 3 A2M 2780 (5 Replies)
Discussion started by: paolo.kunder
5 Replies

6. Shell Programming and Scripting

Difference between 2 files, one with 1 column and 2nd file with multiple columns

Hi, I need to find the difference between 2 files in unix and write the result in the new file File1: A B File2: X 123 hajkd Y 345 adjfka A 123 djafjhd B 678 dsndjks Output file: X 123 hajkd Y 345 adjfka Thanks. (6 Replies)
Discussion started by: nani1984
6 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

9. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

10. Shell Programming and Scripting

Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data. I would like to generate one single file from all these files as follows: One instance of 1st column Text, followed by 2nd column Data separated by... (5 Replies)
Discussion started by: subhap
5 Replies
Login or Register to Ask a Question