1

I would like to create files from the following list.

There is no problem if the list doesn't have a space ... but the problem is it has a space

user@linux:~$ cat file.txt 
Apples
Bing Cherry
Crab Apples
Dragon Fruit
user@linux:~$ 

Before

ser@pc:~$ ls -l
total 4
-rw-r--r-- 1 user user 44 Jun   9 14:06 file.txt
user@linux:~$ 

xargs touch

user@linux:~$ cat file.txt | xargs touch 
user@linux:~$ 

Instead of creating Apples, Bing Cherry, Crab Apples, Dragon Fruit, the command producing these output.

After

user@linux:~$ ls -l
total 4
-rw-r--r-- 1 user user  0 Jun   9 14:11 Apples
-rw-r--r-- 1 user user  0 Jun   9 14:11 Bing
-rw-r--r-- 1 user user  0 Jun   9 14:11 Cherry
-rw-r--r-- 1 user user  0 Jun   9 14:11 Crab
-rw-r--r-- 1 user user  0 Jun   9 14:11 Dragon
-rw-r--r-- 1 user user 44 Jun   9 14:06 file.txt
-rw-r--r-- 1 user user  0 Jun   9 14:11 Fruit
user@linux:~$ 

What should I do to handle space in this situation?

  • See also the section of [this answer](https://unix.stackexchange.com/a/131767/116858) labelled "What's wrong with xargs?". – Kusalananda Jun 09 '19 at 06:51

1 Answers1

1

With the GNU implementation of xargs, you can specify newline as the separator with the -d/--delimiter option:

xargs --no-run-if-empty --delimiter='\n' --arg-file=file.txt touch --

With short options:

xargs -r -d '\n' -a file.txt touch --
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Jasen
  • 3,715
  • 13
  • 14