简体   繁体   中英

Copying files from and to locations named in text files

I have text file file1.txt with content

./home/a/a1/a1.1
./home/a/a1/a1.2
./home/a/a2/a2.1
./home/a/a2/a2.2
./home/a/a3
./home/a/a4
./home/b/b1
./home/b/b2

and one more text file file2.txt with content:

./copy/a/a1/a1.1
./copy/a/a1/a1.2
./copy/a/a2/a2.1
./copy/a/a2/a2.2
./copy/a/a3
./copy/a/a4
./copy/b/b1
./copy/b/b2

I have created script to copy all text files from

./home/a/a1/a1.1    to     ./copy/a/a1/a1.1
./home/a/a1/a1.2    to     ./copy/a/a1/a1.2
........
....

and similarly for other lines.

here is the script:

file1='/home/superjaggu/Desktop/file1.txt'
    file2='/home/superjaggu/Desktop/file2.txt'
    while IFS= read -r lineA && IFS= read -r lineB <&3; do
    cp *.txt $linA $lineB
    done <$file1 3<$file2

but its copying file1.txt and file2.txt to the target directory instead of copying text files from source.

please help.

The *.txt is wrong. It tells cp to copy all files matching *.txt in the current working directory (actually it tells the shell to find those files and pass their names to cp if you want to nit-pick). Actually cp considers *.txt and $lineA to be input files and $lineB to be the destination directory. BTW you also have a typo in the variable name, the variable is named $lineA and not $linA . The corrected script would be:

file1='/home/superjaggu/Desktop/file1.txt'
file2='/home/superjaggu/Desktop/file2.txt'
while IFS= read -r lineA && IFS= read -r lineB <&3
do
    cp -- "$lineA"/*.txt "$lineB"
done <"$file1" 3<"$file2"

Finally worked:

file1='/home/superjaggu/Desktop/file1.txt'
    file2='/home/superjaggu/Desktop/file2.txt'
    while IFS= read -r lineA && IFS= read -r lineB <&3; 
    do
    #cd $lineA      
    cp ${lineA}/*.txt ${lineB} 2>/dev/null
    #cd -v '/home/superjaggu/Desktop'
    done <$file1 3<$file2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM