0

I am a newbie to linux . I have been learning about cat command when i tried this .

harish@harish-Lenovo-G50-45:~$ cat file
Videos/Arrow/Season*
harish@harish-Lenovo-G50-45:~$ cat file | ls -l

The command displays the content of the current folder instead of the folder mentioned in the file .. But when i did this

harish@harish-Lenovo-G50-45:~$ cat file
Videos/Arrow/Season*
harish@harish-Lenovo-G50-45:~$ ls -l $(cat file) 

The contents of the expected folder displays correctly . Why cant i not use the pipeline in this case ?

JNevill
  • 46,980
  • 4
  • 38
  • 63
Harish Ganesan
  • 135
  • 1
  • 2
  • 11
  • you can't "pipe" text to `ls` for use as filenames. ls doesn't accept input that way. you'd have to use xargs, e.g. `cat file|xargs ls -l`. xargs takes data from stdin and passes it to the specified command (`ls -l`) as command-line arguments. – Marc B Oct 31 '16 at 17:10

1 Answers1

2

In the first example you are passing the output of cat file to the input of ls -l. Since ls -l does not take any input, it does not do anything regarding the output of cat file. However in the second example you are using $(cat file) which puts the output of cat file in the place of an argument passed to ls -l, and this time ls -l has the text inside file in the right place for doing something with it. The issue here is noticing the difference between the standard input of a program and the arguments of a program. Standard input is what you have when you call scanf in C, for example; and the arguments are what you get in the argv pointer passed as parameter to the main procedure.

lamg
  • 364
  • 2
  • 6