Skip to main content
added 118 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a timelines from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    ThisThe code does the same as the loop above, but usinguses xargs to execute ls -d -- for each argument read from files.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.list
    

    ThisThe 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 parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each argument read from files.list. An argument is a line, where newlines may be embedded if they are escaped (quotes are also processed, so need to be escaped to be retained).

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the arguments in the file as possible at once. Arguments with tabs or spaces need to be quoted, as would embedded newlines and quotes.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads lines from the file files.list and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    The code does the same as the loop above but uses xargs to execute ls -d -- for each argument read from files.list. An argument is a line where newlines may be embedded if they are escaped (quotes are also processed, so they must be escaped to be retained).

    xargs -- ls -d -- <files.list
    

    The above would run ls -d -- on as many of the arguments in the file as possible at once. One needs to quote arguments containing embedded tabs or spaces and escape individual embedded newlines and quotes.

    The GNU parallel utility is more generic than xargs and allows for more control over the executed jobs.

  3. 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. 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 quoted adequately for use with xargs (see the xargs manual).

added 118 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each line in the file.

    If your list contains quoted pathnames and/orargument read from escaped newlinesfiles.list. An argument is a line, youwhere newlines may usebe embedded if they are escaped (quotes are also processed, so need to be escaped to be retained).

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the pathnamesarguments in the file as possible at once. Arguments with tabs or spaces need to be quoted, as would embedded newlines and quotes.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each line in the file.

    If your list contains quoted pathnames and/or escaped newlines, you may use

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the pathnames in the file as possible at once.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each argument read from files.list. An argument is a line, where newlines may be embedded if they are escaped (quotes are also processed, so need to be escaped to be retained).

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the arguments in the file as possible at once. Arguments with tabs or spaces need to be quoted, as would embedded newlines and quotes.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

missing --s
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each line in the file.

    If your list contains quoted pathnames and/or escaped newlines, you may use

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the pathnames in the file as possible at once.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d for each line in the file.

    If your list contains quoted pathnames and/or escaped newlines, you may use

    xargs -- ls -d <files.list
    

    This would run ls -d on as many of the pathnames in the file as possible at once.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

If you want to do something for each line of a text file, there are generally two ways of doing it.

  1. 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.list
    

    The above reads a line at a time from the file files.list, and executes ls -d -- on each.

    Related:

  2. Use a utility that makes the looping implicit for you, like xargs or GNU parallel:

    xargs -I {} -- ls -d -- {} <files.list
    

    This does the same as the loop above, but using xargs to execute ls -d -- for each line in the file.

    If your list contains quoted pathnames and/or escaped newlines, you may use

    xargs -- ls -d -- <files.list
    

    This would run ls -d -- on as many of the pathnames in the file as possible at once.

    The GNU parallel utility is a bit more generic than xargs and allows for more control over the executed jobs.

  3. 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 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 for use with xargs (see the xargs manual).

added 70 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 154 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading