1

I'm trying to find files from a list and copy them to another place. So I started my test loop like that:

# cat ~/my_filelist.txt | while read file ; do echo "$file" ; done

After I hit enter all filenames from my_filelist.txt are print to stdout, fine.

Now I replaced "echo" with "find" command like that:

# cat ~/my_filelist.txt | while read file ; do find . -name "$file" ; done 

I thought that find will print the results to stdout, but nothing happens O_o

I can see that find is working but where is the ouput?

Could somebody tell me what I'm doing wrong?

Many thanks in advance!

gonzo
  • 11
  • 3
  • What is the purpose of `.` beetween find and -name? `.` is a substitute for a `source`. Try to use `./` or `~/` or `/`. – TheSAS Oct 30 '13 at 13:50
  • 1
    @TheSAS The first argument to `find` is always the directory for `find` to walk. Besides, arguments are (almost) never interpreted by the shell as program names. `echo rm -rf /` will print `rm -rf /`, not delete everything. – Blacklight Shining Oct 30 '13 at 13:55
  • 1
    Are the filenames in `my_filelist.txt` absolute or relative to `.`? They have to be basenames, without paths; `-name` only looks at the basename of each file it encounters. If you want to use paths, use `-path` instead. – Blacklight Shining Oct 30 '13 at 13:58
  • `.` is not a correct directory. Try to `find . -name foo` - error: paths must precede expression. – TheSAS Oct 30 '13 at 14:02
  • 1
    @TheSAS `find . -name foo` works perfectly fine for me on both Debian (GNU `find 4.4.2`) and on Mavericks (can't tell what version of `find` this is). – Blacklight Shining Oct 30 '13 at 14:05
  • @Blacklight This doesn't work in RHEL, find (version 4.2.27). So lets wait for a questionstarter. – TheSAS Oct 30 '13 at 14:08
  • In my_filelist.txt are only filenames like 001_FF_2013-02-02.TIF – gonzo Oct 30 '13 at 14:09
  • @TheSAS when I pick a filename from my and do a 'find . -name 012_DU_2012-01-01.TIF' in the current directory the file is found and the filname + the path is print to stdou like: – gonzo Oct 30 '13 at 14:14
  • /my_current_folder/DU_2012/012_DU_2012-01-01.TIF that path I want to use to copy the file to another place – gonzo Oct 30 '13 at 14:16
  • 1
    now tried this `cat ~/my_filelist.txt | while read file ; do find . -name "$file" -exec cp{} /my/destination/folder \; done` and it ends up with `>` like something is missen – gonzo Oct 30 '13 at 14:18
  • 3
    You need a semicolon after the `find`. The escaped semicolon lets `find` know that that's the end of the `-exec` part, then you need another semicolon before the `done` to mark the end of the `find` invocation for the shell: `cat ~/my_filelist.txt | while read file ; do find . -name "$file" -exec cp {} $destination_dir \; ; done;` – Blacklight Shining Oct 30 '13 at 14:30
  • @BlacklightShining thx for the semicolon tip. But unfortunately nothing is piped to `cp{}`. In htop I can see `find` working `find . -name 152_TU_2011-05-09.TIF ? -exec cp{} /mnt/share/corrupt ;`. But no files are copied :( – gonzo Oct 30 '13 at 14:57

0 Answers0