Can someone let me know how to run "ls *.txt
" as a shell command in go.
I have run using exec.Command("ls", "*.txt")
, It shows *.txt file not found.
Can someone let me know how to run "ls *.txt
" as a shell command in go.
I have run using exec.Command("ls", "*.txt")
, It shows *.txt file not found.
Wildcards like this are evaluated by the shell, not by ls
, and exec
does not use a shell. You could do something like:
exec.Command("bash", "-c", "ls *.txt")
However, it's unnecessarily complicated & inefficient to list files this way in your Go program. You'd be better served just scanning the files directly in Go using e.g. ReadDir
.