Copy list of files from a keyword list to another directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy list of files from a keyword list to another directory
# 1  
Old 05-25-2012
Copy list of files from a keyword list to another directory

Hello,

I have a folder with a massive amount of files, and I want to copy out a specific subset of the files to a new directory. I would like to use a text file with the filenames listed, but can't get it to work.

The thing I'm hung up on is that the folder names in the path can and do have spaces in them. Thus "2010-36 Olson" gets broken up into "2010-36" and "Olson".

Here is what I have thus far.

Code:
#!/bin/bash

kw=$1
dest=$2

while read ifile
	do		
		echo "cp $ifile $dest/$ifile"
	done < $kw

# 2  
Old 05-25-2012
Code:
kw=$1 dest=$2

while IFS= read -r ifile; do
  cp -- "$ifile" "$dest/"
done < "$kw"


Last edited by radoulov; 05-25-2012 at 05:45 PM..
# 3  
Old 05-25-2012
Worked like a charm. Thank you very much.

Correct me if I am wrong.

IFS= sets the field separator to nothing, ie. spaces won't break the path up into different chunks of data.

read -r causes \ to be read as \ (not as an escape). Will it then be read as a path delimiter like / is?

I don't know what the -- in cp does.
# 4  
Old 05-25-2012
It's slightly different:
IFS= disables unnecessary word/field splitting
and protects you from loosing leading and trailing IFS characters
(an eventual corner case: usually leading or trailing spaces or tabs).
-r prevent the backslash from escaping special sequences,
-- avoids misinterpretation of file names with leading -.

Consider the following:

Code:
printf '%s\n' ' a\  ' ' a\  ' 'a\:b' 'a\:b'  | {
  read a 
  IFS= read -r b  
  printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
  IFS=: read a b
  printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
  IFS=: read -r a b
  printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
  }

It produces:

Code:
bash-4.1$ printf '%s\n' ' a\  ' ' a\  ' 'a\:b' 'a\:b'  | {
>   read a 
>   IFS= read -r b  
>   printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
>   IFS=: read a b
>   printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
>   IFS=: read -r a b
>   printf 'a --> |%s|\nb --> |%s|\n' "$a" "$b"
>   }
a --> |a |
b --> | a\  |
a --> |a:b|
b --> ||
a --> |a\|
b --> |b|

Or:

Code:
bash-4.1$ mv -a /tmp/
mv: unknown option -- a
Try `mv --help' for more information.
bash-4.1$ mv -- -a /tmp/
mv: cannot stat `-a': No such file or directory

# 5  
Old 05-26-2012
Many thanks for that detailed explanation.

One question. The code you posted :

Code:
kw=$1 dest=$2

while IFS= read -r ifile; do
  cp -- "$ifile" "$dest/"
done < "$kw"

When I run it, it won't copy the last filename in the file. If I add an empty line, it works, but otherwise I end up missing a file.

Any thoughts?
# 6  
Old 05-26-2012
Probably your file is not in proper Unix format in the sense that the last line is missing a linefeed as the last character and then read will not process it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy Files From a big list

Requirement: When I do ls -ltr /home/data/orders I get a huge list of files, I need to copy that last 50 to another directory say /home/work/ later, I will do my ETL process and then again I need to copy from 51 to 100 and so on. What is the command to copy files specifying 1 to 50... (5 Replies)
Discussion started by: eskay
5 Replies

2. Shell Programming and Scripting

I'm trying 2 copy files after using the grep to get a list

for XmlFileName in ${xmlFileNames} do XmlFileName=$(echo $XmlFileName | sed 's|./||') # Remove leading ./ path that find command prefixes to filenames cp $XmlFileName $NEW_DIR/ done (1 Reply)
Discussion started by: emc^24sho
1 Replies

3. Shell Programming and Scripting

How to copy selective list of files to a directory?

Hi I have 3 directories indexes_with_ts indexes_without_ts process_indexes in each directories it contains *.sql how do I accomplish this: for all the files found in indexes_without_ts, copy the corresponding file in indexes_with_ts to process_indexes. i.e. for... (2 Replies)
Discussion started by: jediwannabe
2 Replies

4. Shell Programming and Scripting

Copy files on a list to another directory

Hi. I have a list with file names like testfile1.wav testfile2.wav testfile3.wav and a folder that contains a large number of wav files (not only the ones on the list). I would like to copy the files whose names are on the list from the wav file directory to a new directory. I... (5 Replies)
Discussion started by: Bloomy
5 Replies

5. Shell Programming and Scripting

copy all files that ls -l list

Hi, I need to copy all files from this statement to another folder: ls –l ./apps | grep –E “^l|^v” How can i do that? best regards! (8 Replies)
Discussion started by: xus
8 Replies

6. UNIX for Dummies Questions & Answers

How can i copy a list of files with different names into others directory have the same name?

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (31 Replies)
Discussion started by: t17
31 Replies

7. Shell Programming and Scripting

how can i copy a list of files with different names into others directory have the same name

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (0 Replies)
Discussion started by: t17
0 Replies

8. Shell Programming and Scripting

find list of files from a list and copy to a directory

I will be very grateful if someone can help me with bash shell script that does the following: I have a list of filenames: A01_155716 A05_155780 A07_155812 A09_155844 A11_155876 that are kept in different sub directories within my current directory. I want to find these files and copy... (3 Replies)
Discussion started by: manishabh
3 Replies

9. UNIX for Dummies Questions & Answers

How to List and copy the files containing a string

:confused: I have more than 8000 files in a dir, I need to copy to other dir which containing the "sample" I tried grep -il "1189609240791-1268115603299237276@216.109.111.119 ' | cp /tmp/inv Nothing is happening for long time for 100 file dir too, Any one can help me? (11 Replies)
Discussion started by: redlotus72
11 Replies

10. UNIX for Dummies Questions & Answers

copy and rename list of files

Hi all, I am a newbie in writng unix..I am using ksh shell..Does anyone know how to copy a list o files from directory A to directory B with differnt names? i.e in Dir A, I have RPT101.555.TXT RPT102.666.TXT and I want to copy those files to dir B with new naming convention.. in Dir B,... (7 Replies)
Discussion started by: kinmak
7 Replies
Login or Register to Ask a Question