0
  1. I know that I can do tr a b < file, but how do I do that for ls? So for example, How to feed ls -l a list of files from a 'file'. I tried the below and it did't work.

    $ cat>test
    A
    B
    C
    $ ls -l < test
    
  2. I'm not clear on what "<<" or here document is used for.

  3. One of my class exercises is to use vi to create a file with “:” on line 1 and “6/3” on line 2. Using line 1 as the delimiter and line 2 as the field, cut and display column2 from a file.

Am I supposed to loop and read the 2 lines into a variable? Do I use sed -n '#p' (file)? Is there an elegant 1 line solution?

closed as too broad by Sparhawk, muru, sourcejedi, Prvt_Yadv, Mr Shunz Apr 15 at 9:36

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    You should have asked three different questions. – Franklin Piat Apr 9 '15 at 20:55
3

A simple approach is:

ls -ld -- $(< file )

(assuming ksh or compatible shell)

but it has some issues, e.g. with whitespace or wildcard characters in filenames. So better use another approach:

xargs -rd '\n' ls -ld -- < file

(here assuming the GNU implementation of xargs for those -r and -d options).

1
  1. ls does not read from standard input, so input redirection does not do anything. What you want is command substitution.

    ls -l `cat list`
    

    or

    ls -l $(cat list)
    
  2. Here documents are used to redirect standard input to fixed strings provided in the script instead of a separate file. You can also do it from the command line, but doing so has limited value and is not common.

  3. This does not make sense.

  • So for #3 my current solution is: LINE1: $delim=$(head -n1 viFile) LINE2: $nameField=$(tail -n1 viFile | bc) LINE3: $cut -d$delim -f$nameField someFile. Supposing there is a file with two columns delimited by ":". – Ben Apr 9 '15 at 16:25
0

Answer part one:

  1. you can use while loop to pass file content to 'ls' commnad.

    while read line ; do
    ls -l $line 
    done < file
    
  2. dirty way to do it:

    counter=1
    while read line ; do
        if [ $counter -eq 1 ] ; then 
            delimiter=$line
            counter=$((counter+1))
        elif [ $counter -eq 2 ] ; then
            field=$line
            counter=$((counter+1))
        else
            echo File ended.
        fi
    done < inputfile
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.