Search code examples
linuxxargsls

xargs to pipe the output of ls command


I am running the command

ls *my_file.txt* | xargs vim

The shell throws a warning message:

Vim: Warning: Input is not from a terminal

following that the file is opened. Note there is only one instance of *my_file.txt*. On exiting the file, I see that on each ENTER the prompt is not on the next line but continues on the same line. The characters are not typed on the display but are buffered and executed on subsequent enter. Basically, the display gets awry.

The intent is basically to pipe the searched file_name to vim. So any alternative solutions are welcome.


Solution

  • Use find and exec instead of xargs.

    find /search/path/ -type f -name "*my_file.txt*" -exec vim {} \;
    

    You can add more options to find like -depth to restrict traversing recursively, -regex for complex regular expressions for finding files etc.