Copy files to a dir using from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files to a dir using from a list
# 1  
Old 06-28-2012
Copy files to a dir using from a list

Hi all,
I'd very grateful for some help with the following:

I have a directory with several subdirectories with files in them. All files are named different, even between different subdirectories. I also have a list with some of those file names in a txt file (without the path, just the file name), like:

a.jpg
d.jpg
f.jpg
...

I would like to copy all the files in the file list to a different directory without copying all of them, but I can't figure out how to. I know how to use find to locate any file path and copy it, but don't know how to make it for a list of them.

Thanks,
foracoffee
# 2  
Old 06-28-2012
Code:
find /path/to/root > allfiles

awk -F"/" 'NR==FNR '{ A[$1]++; next } $NF in A' filelist allfiles | while read LINE
do
        echo cp "$LINE" /path/to/dest
done

rm allfiles

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-28-2012
there are no duplicates, all names are different

---------- Post updated at 06:40 PM ---------- Previous update was at 06:16 PM ----------

when I try to execute the awk part, it shows a > sign each time I push 'enter'. I've been looking and it seems I have to create a file with the awk code? Sorry, haven't used it before
# 4  
Old 06-28-2012
I misplaced a quote, sorry.

The awk code doesn't create a file, the find code does.
Code:
find /path/to/root > allfiles

awk -F"/" 'NR==FNR { A[$1]++; next } $NF in A' filelist allfiles | while read LINE
do
        echo cp "$LINE" /path/to/dest
done

rm allfiles

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-28-2012
Thanks! It works now!

However, it only makes:
Code:
cp . /results

being '/results' my path/to/dest. I've looked at the filelist and the allfiles an they have files in common but don't show as:
Code:
cp file /results

any idea what I might be doing wrong?

---------- Post updated at 07:25 PM ---------- Previous update was at 07:14 PM ----------

It works! Some of the files in the list still have some path, when I removed, it worked fine Smilie

Thanks a lot!
This User Gave Thanks to foracoffee For This Post:
# 6  
Old 06-28-2012
Ah. That could also have been remedied with
Code:
awk -F"/" 'NR==FNR '{ A[$NF]++; next } $NF in A'

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

List and copy dir remotely using shell script

Hi, I am looking for a way to copy the directory on a remote machine that's present on the same remote machine. ex. I have multiple directories on a remote machine "B" created as /abc/LOG_1004 /abc/LOG_1008 /abc/LOG_1012 The script should be able to create a copy of the latest dirs... (1 Reply)
Discussion started by: deo_kaustubh
1 Replies

2. Shell Programming and Scripting

how to copy current date files to another dir

i have directory /abcd and i want to copy all today date files in /xyz directory. i am able to see the files by using below command but not able to understand copy. find . -mtime -1 -type f -exec ls -l {} \; (2 Replies)
Discussion started by: learnbash
2 Replies

3. Shell Programming and Scripting

files copy to dir

Hi, I have a directory which is having many files. I want to copy 10files at a time to another directory. For example. First 10 files to one directory, then next 10 files to another directory and so on. Please let me know if any work around there for it. Thanks (4 Replies)
Discussion started by: Anjan1
4 Replies

4. Shell Programming and Scripting

diff script to copy files to other dir when not exists

I have two directories that are mostly the same: dir1 dir2 Is there an easy way to take the output of diff and copy files from dir1 that do not exist in dir2 - but copy them to the same path (many nested directories). am only trying to copy files in dir1 that do not exist in dir2. ... (5 Replies)
Discussion started by: jvsrvcs
5 Replies

5. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

6. Shell Programming and Scripting

Copy files from input file with dir structure

hi, I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE. Note that i will have other files and directories which i dont want to copy to destination. For example, dir source has following content:... (22 Replies)
Discussion started by: dragon.1431
22 Replies

7. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

8. Shell Programming and Scripting

How to copy specified files from list of files from dir A to dir B

Hello, fjalkdsjfkldsajflkajdskl (3 Replies)
Discussion started by: pmeesara
3 Replies

9. Shell Programming and Scripting

copy files from one dir to another

Hi , I want to copy files from one dir to anothe dir and check if destination dir exists ,if not exist ,has to create but when executing the below schell script ,the destination directory not exist and exit the shell script. #!/bin/sh src_path=/home/owngdw/abc tgt_path=/home/owngdw/abc/xyz if... (6 Replies)
Discussion started by: mohan705
6 Replies

10. UNIX for Dummies Questions & Answers

How to copy N files from one dir to another

Hi, I have a script that can only handLE limited number of input files. I need to be able to write a command (or a script) that: 1> copies N number of files from one directory (A) to another (B). 2> Files that are moved need to be renamed. 3> Files picked to be moved have... (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question