1

I let the user specify a filename say hello.txt and the number of newest lines say 30 they want from the file.

I then fire the below command to get them the data they are looking for.

tail -30f hello.txt > hello.txt.tmp; mv hello.txt.tmp hello.txt;

The problem is when the user provides some irrelevant file that does not have text or directory the command does not work. For example:jdk.zip

tail -30f jdk.zip > jdk.zip.tmp; mv jdk.zip.tmp jdk.zip;

The above simply never returns.

Thus, I need a solution that I should be able to check if the provided file will work with the tail command or not; before I fire the actual command.

Thus, a directory, a zip file, a PDF, an mp4 file, etc whichever do not work with the tail command should not be tailed.

I'm on Linux system.

Ashar
  • 491
  • maybe use file <your-file>, look for ASCII text, or some UTF/latin/... encoding. – SYN Feb 01 '21 at 13:39
  • 3
    Also note that tail -f will never terminate by itself when reading a file (it will terminate if reading a pipe). It also won't get anything new unless another process is appending to the file. And if you do Ctrl_C it, or send it a signal, it might not flush the last block of the file (the man page says nothing about that situation). – Paul_Pedant Feb 01 '21 at 13:59
  • Can you confirm that you use -f with tail in both cases and that it works the same way as -n 30 -f does, and that one of the commands is doing what you want while the other is not? – Kusalananda Feb 01 '21 at 15:38
  • I have discarded using -f with tail as it never completes. The link shared by @pLumo helped. We can use ansible stat module to meet my requirement. – Ashar Feb 01 '21 at 16:20

0 Answers0