Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java executing linux command

Tags:

java

linux

bash

cat

I`m trying to execute linux commant 'cat' from java code, but it does not working.

Runtime.getRuntime().exec("cat /home/roman/logs/*");  

And it working well for cat of single file

Runtime.getRuntime().exec("cat /home/roman/logs/mylog.log");

My question is how to cat all files on some dir from java ?

like image 682
Roman Iuvshin Avatar asked May 23 '26 04:05

Roman Iuvshin


2 Answers

You could put all files under the dir into a collection and iterate over it:

File[] files = dir.listFiles();
for (File f : files) {
  Runtime.getRuntime().exec("cat "+dir.getAbsolutePath()+File.separator+f.getName());
}
like image 66
Yuval Avatar answered May 25 '26 16:05

Yuval


You can't use * with the exec() command (you would need a shell). A solution could be to write a script and then exec() that script from your java application.

like image 24
talnicolas Avatar answered May 25 '26 17:05

talnicolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!