Thursday 15 July 2010

linux - Find files in a dir, executing a command with execdir and redirecting -


It seems that I am unable to get a direct answer to this question. I appreciate your help. I am trying to find all the files in a directory with a specific name, read the last 1000 rows of the file and copy it to a new file in the same directory. As an example:

Find the names of all the files in the existing file, copy the previous 1000 lines to ABCLog (which does not exist).

I use the following command without trying any luck:

find "name" xyz.log "-execdir tail-1000 {}> Abc.log \,

The problem is that for all the files in the current directory, they all write abc.log in the CURRENT directory and not in the directory where xyz.log resides. Evidently executed with execdir first is executed and then output is redirected to abc.log.

Can you suggest a way to correct it? I appreciate any information / support.

Edit - I tried to find -name "xyz.log" -execdir sh -c "tail-1000 {}> abc.log" \; As some friends have suggested, but this gives me this error: "sh: ./ail: No such file or directory" error message Do people know what's the problem?

Fortunately to use the solution - print is working fine.

The easiest way is:

  find -name "xyz .log "-execdir sh -c 'tail-1000" {} "& gt; Abc.log '\;   

A more flexible option is to print the first orders and then sh :

  find -name "xyz.log "-printf" tail -1000 "% p" & gt; "% H / abc.log" \ n '| Sh   

You can | Sh Finally when you are trying to figure it out / debugging.

There is some 4.26 and 4.3 versions, though it was fixed in 4.2.x and 4.3. X Edition) that would cause execdir arguments to implement {} instead of a prefix prefixed with ./ . Is applicable to the entire cited string). You can use it to work around:

  find -name "xyz.log" -execdir sh -c 'tail-1000 "$ 1" & gt; Abc.log 'sh {} \;   

sh -c 'script' arg0 arg1 run sh script with arg0, arg1, etc. passed it done. From the conference, the arg0 is the name of the executable (here, "sh"). From the script, you can use the $ 0 (corresponding to "sh"), to the arguments using $ 1 ( {} ) Can reach. e.t.c.

No comments:

Post a Comment