Home > Blockchain >  Join multiple file names of files in a directory to a string
Join multiple file names of files in a directory to a string

Time:12-09

I have a directory with many files.

An executable <executable> has to be called with its name followed by all files in a directory (file_1 ... file_n) and an <outputfile>:

Example:

executable file_1 ... file_n outputfile

How can I read all files from the same directory and produce a string as above that can be executed?

CodePudding user response:

This script will print out the string you want with proper quoting on filenames:

#!/bin/bash

printf -v files '%q ' *
cmd="executable $files outputfile"
echo "$cmd"

You can actually execute the command with eval "$cmd". Use of eval is safe here since filenames are quoted properly.

CodePudding user response:

executable $(ls | xargs echo) outputfile

If you don't have spaces in the filenames.

  • Related