DontParseLs and DontReadLinesWithFor.
The problem you are asking about could be fixed by using double quotes instead of single quotes, but then, you need to escape any shell metacharacters which you do not intend for the shell to interpret (in other words, you'd have to backslash-escape the dollar sign in the address range 1,$
, or use single quotes next to double quotes, and probably change to a different delimiter to cope with slashes in the value of the variable $i
; '1,$'"s:^:$i:"
).
But your entire task can be performed as a single command.
IFS=$'\n' find $(cat listall) -mindepth 1 -maxdepth 2 -print >listZ
The IFS
is only strictly necessary if listall
contains file names with whitespace in them. You would still in principle need to escape any other shell metacharacters (asterisks, square brackets, question marks). A more robust workaround would be
while read -r directory; do
printf '%s\n' "$directory"/* "$directory"/*/*
done <listall >listZ
(I misunderstood your question at first, and assumed listall
contained file names, not directory names. I'm keeping my original answer below.)
sed "s:^:$i/:" listall >listZ
provided you don't actually need an empty space on the first line (maybe add '1i\ '
to the sed
script then). The address range 1,$
is the default in sed
, so specifying it is redundant. Because there can only be one substitution per line, the /g
flag is also redundant (it means, substitute all occurrences on one line, instead of just the first). If there could be colons in $i
, use a different delimiter.