Copy files on a list to another directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files on a list to another directory
# 1  
Old 07-08-2011
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 tried to write a Python script for this, but it doesn't work and I wondered if there's an easier option to do that.

Cheers,

Kat
# 2  
Old 07-08-2011
If GNU cp is available (if you're on Linux for example) and if the filenames do not contain pathological characters (newlines or white spaces):

Code:
< list xargs cp -t target_dir --

If the filenames contain white spaces and you have GNU cp (this is will be much slower if the number of files is high):

Code:
< list xargs -I{} cp -t target_dir -- {}

This is similar, but noisy:

Code:
while IFS= read -r; do 
  cp -- "$REPLY" target_dir 
done < list

Another option, if the filenames do not contain embedded newlines
and the total length of their names is below the ARGS_MAX limit
of your system:

Code:
( IFS=$'\n'; cp -- $(<list) target_dir )

If your shell doesn't expand ansi strings $'...', use a literal newline:

Code:
( 
  IFS='
' 
  cp -- $(<list) target_dir 
  )


Last edited by radoulov; 07-08-2011 at 05:10 PM.. Reason: Correction.
This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-08-2011
Thanks! I tried the first option but I received the error

Code:
cp: target `testfile.wav' is not a directory

I executed it like this:

cd'ed into the wav file directory and then:

Code:
< ../list.data xargs cp -- -t ./target_directory


Last edited by radoulov; 07-08-2011 at 05:05 PM.. Reason: Code tags!
# 4  
Old 07-08-2011
Sorry, there was an error in the commands I posted. Try the new versions.
The double dash -- marks the end of options, so it should be after the -t option and its argument.

Last edited by radoulov; 07-08-2011 at 05:13 PM..
# 5  
Old 07-09-2011
Hi,
I didnt understand what is difficult in this.. I have a suggestion.

Code:
for file in `cat list_file `                          /* list_file contains the wav files you want to copy
 do 
 cp source_dir/$file new_dir
 done

# 6  
Old 07-09-2011
That is a useless use of cat and useless use of backticks, inherently dangerous because any file too long for a shell variable to hold will have some of its contents silently lost. It's also very inefficient. There's a much better way to do this, which doesn't attempt to store the entire file in memory at once, and uses pure shell built-ins:
Code:
while read LINE
do
        ...
done < filename


Last edited by Corona688; 07-09-2011 at 05:52 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

3. Red Hat

Unable to copy files due to many files in directory

I have directory that has some billion file inside , i tried copy some files for specific date but it's always did not respond for long time and did not give any result.. i tried everything with find command and also with xargs.. even this command find . -mtime -2 -print | xargs ls -d did not... (2 Replies)
Discussion started by: before4
2 Replies

4. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: twjolson
5 Replies

5. UNIX for Dummies Questions & Answers

Copy files into another directory

I have a folder will a lot of documents (pdf, xls, doc etc.) which users have uploaded but only 20% of them are currently linking from my html files. So my goal is to copy only the files which are linked in my html files from my Document directory into another directory. Eg: My documents exist... (5 Replies)
Discussion started by: ankitha
5 Replies

6. UNIX for Dummies Questions & Answers

How to copy all files into the same directory

Dear All, Again I have another simple question. :confused: I want to write a csh which can copy all files of a current directory with a new name in the same directory, I mean: If I have tree bird apple as files in a directory I want to give ,say number 007 as argument to my csh and it copies... (3 Replies)
Discussion started by: dreamer0085
3 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

Copy files from one directory to another

I need to copy about 13 Tb of data from one directory and subdirectories to the other (another mount point). If I run this as a cron, say between 10 pm and 7 am, not all of the files will be copied over. Is there a way of 'resuming' the copy the following evenings until all files are copied over? (0 Replies)
Discussion started by: hd2006
0 Replies
Login or Register to Ask a Question