6

Possible Duplicate:
Run a command for a specified time and then abort if time exceeds

Looked all over and I can't find the answer...

Simple question: How can I cat a large file for, lets say, 45 seconds and output it to the screen or redirect to another file.

Thanks!

user488244
  • 563
  • 2
  • 6
  • 12
  • For the "or redirect to another file" part of the question, which the accepted answer does _not_ cover at all, see my answer here: [How to use `timeout` with redirection to a file](https://unix.stackexchange.com/a/720853/114401). – Gabriel Staples Oct 13 '22 at 16:43

1 Answers1

10

You could try:

timeout 45 cat yourfile

See also https://stackoverflow.com/questions/5161193/bash-script-that-kills-a-child-process-after-a-given-timeout

student
  • 17,875
  • 31
  • 103
  • 169
  • timeout is cleaner and allows the duration option to be specified in seconds, minutes, hours, as well as the option to specify which signal to send. cleaner and more readable – bsd Feb 08 '12 at 20:14
  • timeout not available on MacOS, at least in bash 3 – Alexander Mills Apr 20 '18 at 22:09
  • @AlexanderMills Install coreutils via Homebrew, use `gtimeout`. – HappyFace Aug 09 '20 at 19:06
  • I just added an answer to this here: [How to use `timeout` with redirection to a file](https://unix.stackexchange.com/a/720853/114401), for those who also need to redirect the output to a file with `> file`. – Gabriel Staples Oct 13 '22 at 16:41