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.
file <your-file>
, look for ASCII text, or some UTF/latin/... encoding. – SYN Feb 01 '21 at 13:39-f
withtail
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-f
withtail
as it never completes. The link shared by @pLumo helped. We can use ansiblestat
module to meet my requirement. – Ashar Feb 01 '21 at 16:20