Piping `find` to 'tail`

Hari K

I want to get the last two lines of the find output and copy them somewhere. I tried

find . -iname "*FooBar*" | tail -2 -exec cp "{}" dest \;

but the output was "invalid option --2" for tail.

Also, my file or directory name contains spaces.

l0b0

The following should work on absolutely any paths.

Declare a function to be able to use head and tail on NUL-separated output:

nul_terminated() {
    tr '\0\n' '\n\0' | "$@" | tr '\0\n' '\n\0'
}

Then you can use it to get a NUL-separated list of paths from your search after passing through tail:

find . -exec printf '%s\0' {} \; | nul_terminated tail -n 2

You can then pipe that to xargs and add your options:

find . -iname "*FooBar*" -exec printf '%s\0' {} \; | nul_terminated tail -n 2 | xargs -I "{}" -0 cp "{}" "dest"

Explanation:

  1. find files in the current directory (.) and below with a name containing foobar (case insensitive because of the i in -iname);
  2. for each file, run (-exec) a command to
  3. print each file path ({}) followed by a NUL character (\0) individually (\;);
  4. swap newlines and NUL characters (tr '\0\n' '\n\0');"
  5. get the last two lines (that is, paths; tail -n 2, "$@");
  6. swap newlines and NUL characters again to get a NUL-separated list of file names (tr '\0\n' '\n\0').

The xargs command is a bit harder to explain. It builds as many cp ... "dest" commands as necessary to fit in the maximum command length of the operating system, replacing the {} token in the command with the actual file name (-I "{}" ... "{}"), using the NUL character as a separator when reading the parameters (-0).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Piping of grep is not working with tail?

From Dev

will tail with piping read whole file?

From Dev

Piping tail -f to cut to sed produces no output

From Dev

Piping echo, tail and sed leads to wrong output

From Dev

Bash : Piping Find into Grep

From Dev

Piping from a find into grep

From Dev

Piping find results into another command

From Dev

Issue with real-time log inspecting piping tail, grep and cut

From Dev

Why does piping `mysql` to 'tail' change the output format?

From Dev

issue with piping find into sed (find and replace)

From Dev

Combining find grep and tail

From Dev

Bash: tail, find, and cat

From Dev

NSTask calling perl and piping in find not working

From Dev

Confused about piping commands from find to commandX?

From Dev

NSTask calling perl and piping in find not working

From Dev

quoting problem when piping find output into xargs

From Dev

Correct location for piping and redirecting output in find -exec?

From Dev

Piping Find and Move Command Output to a file

From Dev

piping empty find result to du through xargs results in unexpected behavior

From Dev

Piping find -name to xargs results in filenames with spaces not being passed to the command

From Dev

How to open a found file with vi? Piping 'find' output to vi

From Dev

Different behaviors between find -exec and piping through xargs

From Dev

Why is find piping in non directories when the “-type d” test is used?

From Dev

Regex to find if a url has a tail of a file name

From Dev

Find command, -exec tail and file browser

From Dev

How to find value of a key on tail -f

From Dev

iOS NSRegularExpression how to find the first matching "TAIL" of pattern like: HEAD(.*)TAIL

From Dev

How to find tail rows of a data frame that satisfy set criteria?

From Dev

Tail multiple files and output as additional column with 'find' results

Related Related

  1. 1

    Piping of grep is not working with tail?

  2. 2

    will tail with piping read whole file?

  3. 3

    Piping tail -f to cut to sed produces no output

  4. 4

    Piping echo, tail and sed leads to wrong output

  5. 5

    Bash : Piping Find into Grep

  6. 6

    Piping from a find into grep

  7. 7

    Piping find results into another command

  8. 8

    Issue with real-time log inspecting piping tail, grep and cut

  9. 9

    Why does piping `mysql` to 'tail' change the output format?

  10. 10

    issue with piping find into sed (find and replace)

  11. 11

    Combining find grep and tail

  12. 12

    Bash: tail, find, and cat

  13. 13

    NSTask calling perl and piping in find not working

  14. 14

    Confused about piping commands from find to commandX?

  15. 15

    NSTask calling perl and piping in find not working

  16. 16

    quoting problem when piping find output into xargs

  17. 17

    Correct location for piping and redirecting output in find -exec?

  18. 18

    Piping Find and Move Command Output to a file

  19. 19

    piping empty find result to du through xargs results in unexpected behavior

  20. 20

    Piping find -name to xargs results in filenames with spaces not being passed to the command

  21. 21

    How to open a found file with vi? Piping 'find' output to vi

  22. 22

    Different behaviors between find -exec and piping through xargs

  23. 23

    Why is find piping in non directories when the “-type d” test is used?

  24. 24

    Regex to find if a url has a tail of a file name

  25. 25

    Find command, -exec tail and file browser

  26. 26

    How to find value of a key on tail -f

  27. 27

    iOS NSRegularExpression how to find the first matching "TAIL" of pattern like: HEAD(.*)TAIL

  28. 28

    How to find tail rows of a data frame that satisfy set criteria?

  29. 29

    Tail multiple files and output as additional column with 'find' results

HotTag

Archive