Ls to find to copy

I am looking to do the following:

In a folder with multiple files in it, take the listing (ls) and search another directory for there file names, then take that output and copy the files out.

This is to update a webpage. So a dev writes a new file and puts it in an update folder then i copy it out to the ear direcotry in the proper place

Any ideas would be great!

So basically you want to compare two directories, just for existence of files, not date nor size nor anything else, and then take WHAT output (delta? common files?) to copy which files from where to where?

Yes
If i have /update/test1.txt and /update/test2.txt in a folder i want to search /app/. for those two files then take the path to the files in the app and copy out the files from the update folder to the app folder in the proper sub folder

Not clear. Copy both files, even when they exist? Copy to what subdir? Do you need to check /app/ including its subdirs? Does /update/ have the same subdir structure, and will the new files show up in the right subdir?

Sorry
I have a folder called update with multiple files in it. There are exsisting files with the same name in the app folder. i want to search for each file from the update directory in the app directory then over write the corresponding files in the app directory

so something like this

ls /update
   file1.txt
   file2.txt
   file3.txt

find each file name in /app

copy file from /update to the matching file name in /app (all in different sub directories)

Hope that helps

This is untested, but you may want to try and adapt. It is dependent on files being unique (which should go without saying). Try:

cd /update
ls -1 >newfiles
find /app >allfiles
grep -f newfiles allfiles | while read FN; do echo cp "${FN##*/}" "$FN"; done

Remove echo once you're happy with the results.

Filenames being unique is not enough; no file's name can be a trailing subset of another filename either. For example, if you have files angle.txt and triangle.txt somewhere under /app and there is a file named /update/angle.txt , the while loop will overwrite both files under /app with the contents of /update/angle.txt .

1 Like

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.