I have a file called "input.txt." that contains one line:
/bin
I would like to make the contents of the file be the input of the command ls
I tried doing
cat input.txt | ls
but it doesn't output the list of files in the /bin directory
I also tried
ls < input.txt
to no avail.
You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.
The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags. If you do not specify a File or Directory, the ls command displays the contents of the current directory.
Conclusion: In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.
The command ls -l is executed as a process, the output (stdout) of which is piped to the input (stdin) of the process for grep key ; and likewise for the process for less . Each process takes input from the previous process and produces output for the next process via standard streams.
You are looking for the xargs
(transpose arguments) command.
xargs ls < input.txt
You say you want /bin
to be the "input" to ls
, but that's not correct; ls
doesn't do anything with its input. Instead, you want /bin
to be passed as a command-line argument to ls
, as if you had typed ls /bin
.
Input and arguments are completely different things; feeding text to a command as input is not the same as supplying that text as an argument. The difference can be blurred by the fact that many commands, such as cat
, will operate on either their input or their arguments (or both) – but even there, we find an important distinction: what they actually operate on is the content of files whose names are passed as arguments.
The xargs
command was specifically designed to transform between those two things: it interprets its input as a whitespace-separated list of command-line arguments to pass to some other command. That other command is supplied to xargs
as its command-line argument(s), in this case ls
.
Thanks to the input redirection provided by the shell via <
, the arguments xargs
supplies to ls
here come from the input.txt
file.
There are other ways of accomplishing the same thing; for instance, as long as input.txt
does not have so many files in it that they won't fit in a single command line, you can just do this:
ls $(< input.txt)
Both the above command and the xargs
version will treat any spaces in the input.txt
file as separating filenames, so if you have filenames containing space characters, you'll have to do more work to interpret the file properly. Also, note that if any of the filenames contain wildcard/"glob" characters like ?
or *
or [
...]
, the $(<
...)
version will expand them as wildcard patterns, while xargs
will not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With