0

I am creating a script in Red Hat Enterprise Linux Server release 7.5 (Maipo) to find all files named test*.txt that are 10 days older than 6 months. The original directory that I'm copying from contains a valid file, however when I try to move the files it errors out. The script and the error message is listed below. Why does it fail and what I can do to fix it?

find /INTER/f/d/o/ -type f -name "test*.txt" -mtime +190 -exec mv /INTER/f/d/o/"test*.txt" /INTER_ARCHIVE/f/d/o/ {} \;

mv: target    /INTER/f/d/o/test1.txt    is not a directory
mv: target    /INTER/f/d/o/test2.txt    is not a directory
mv: target    /INTER/f/d/o/test3.txt    is not a directory
mv: target    /INTER/f/d/o/test4.txt    is not a directory

Also, after the files have sat in the archive directory for 7 months plus 10 days delete them. Using the script below, I get the following error.

find /INTER_ARCHIVE/f/d/o/ -type f -name "test*.txt" -mtime +400 -exec rm {};

find: missing argument to `-exec'
Kusalananda
  • 333,661

2 Answers2

1

Your first command:

find /INTER/f/d/o/ -type f -name "test*.txt" -mtime +190 -exec mv /INTER/f/d/o/"test*.txt" /INTER_ARCHIVE/f/d/o/ {} \;

Here, I assume that you'd want to move the found files that match the pattern and are sufficiently old. The mv command bit is malformed and should look like

-exec mv {} /some/destination/path \;

That is, "move the found file ({}) to some destination".

It is unclear from looking at the command where you want the files to be moved, but as you wrote it, you are moving files to the found pathnames. This fails since the mv tries to move multiple files to something that is not a directory.

In you second command,

find /INTER_ARCHIVE/f/d/o/ -type f -name "test*.txt" -mtime +400 -exec rm {};

you have simply failed to terminate the argument to -exec correctly. The last bit should look like

-exec rm {} \;

or simply

-delete

if your implementation of find supports that.

See also "Understanding the -exec option of `find`".

Kusalananda
  • 333,661
0

Your first command:

find ... -exec mv /INTER/f/d/o/"test*.txt" /INTER_ARCHIVE/f/d/o/ {} \;

will pass to mv the arguments /INTER/f/d/o/test*.txt (literally, with the asterisk), /INTER_ARCHIVE/f/d/o/ and then the current file that find is processing. mv sees three arguments, so it expects to move the first two to the third, which then must be directory.

The test*.txt will not expand to any matching filenames, since find doesn't run the -exec command through a shell (you have to use -exec sh '...' \; for that). Not that you'd want to, anyway, since globbing test*.txt here would defeat the point of the -mtime.

What you probably want is

find ... -exec mv {} /INTER_ARCHIVE/f/d/o/ \;

to move the file find is currently processing to /INTER_ARCHIVE/f/d/o/.

Or with GNU mv:

find ... -exec mv -t /INTER_ARCHIVE/f/d/o/ {} +

to process multiple files with one call to mv.

ilkkachu
  • 138,973