Steam
Steam

Reputation: 9856

Read filenames from a text file and then make those files?

My code is given below. Echo works fine. But, the moment I redirect output of echo to touch, I get an error "no such file or directory". Why ? How do i fix it ? If I copy paste the output of only echo, then the file is created, but not with touch.

while read line           
do           
    #touch < echo -e "$correctFilePathAndName"
    echo -e "$correctFilePathAndName"           
done < $file.txt

Upvotes: 2

Views: 3316

Answers (4)

anubhava
anubhava

Reputation: 785068

If you have file names in each line of your input file file.txt then you don't need to do any loop. You can just do:

touch $(<file.txt)

to create all the files in one single touch command.

Upvotes: 8

CodeReaper
CodeReaper

Reputation: 6145

Ehm, lose the echo part... and use the correct variable name.

while read line; do
    touch "$line"
done < $file.txt

Upvotes: 3

CCH
CCH

Reputation: 1536

try :

echo -e "$correctFilePathAndName" | touch

EDIT : Sorry correct piping is :

echo -e "$correctFilePathAndName" | xargs touch

The '<' redirects via stdin whereas touch needs the filename as an argument. xargs transforms stdin in an argument for touch.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655219

You need to provide the file name as argument and not via standard input. You can use command substitution via $(…) or `…`:

while read line
do
    touch "$(echo -e "$correctFilePathAndName")"
done < $file.txt

Upvotes: 4

Related Questions