If you want to do something for each line of a text file, there are generally two ways of doing it.
Read the file line by line and do whatever you need to do to the line in the body of the loop:
while IFS= read -r pathname; do ls -d -- "$pathname" done <files.listThe above reads a line at a timelines from the file
files.list, and executesls -d --on each.Related:
Use a utility that makes the looping implicit for you, like
xargsor GNUparallel:xargs -I {} -- ls -d -- {} <files.listThisThe code does the same as the loop above, but usinguses
xargsto executels -d --for each argument read fromfiles.list. An argument is a line, where newlines may be embedded if they are escaped (quotes are also processed, so need tothey must be escaped to be retained).xargs -- ls -d -- <files.listThisThe above would run
ls -d --on as many of the arguments in the file as possible at once. Arguments withOne needs to quote arguments containing embedded tabs or spaces need to be quoted, as wouldand escape individual embedded newlines and quotes.The GNU
parallelutility is a bit more generic thanxargsand allows for more control over the executed jobs.Since the shell is often the slowest language you can pick for processing data, if you need to do something other than executing a command for each line in a file, then it's probably better to do so in a language that makes it easier to do this. Example Examples of such languages:
- Awk
- Perl
- Python
In either case, since you store newline-delimited pathnames in your file, you are disqualified from using pathnames containing newlines (these are legal on Unix systems), unless properly quoted adequately for use with xargs (see the xargs manual).