Is it possible to redirect output of a file to ls. Example: file has a / in it and I want to direct that to ls to get the content of the / directory. When I try ls < file this does not work
/
ls < file
You can use xargs or command substitution to achieve this.
xargs
The key is knowledge of the xargs command.
If you have a list of files in a file called files_to_change, you can print them with the following one liner:
files_to_change
cat files_to_change | xargs ls
An alternate method is to use command substitution. This works the same as above.
Two different one-liners, using different syntax:
ls `cat files_to_change` ls $(cat files_to_change)
It won't matter if they are files or directories, and you can run any command on them.
If the contents of file_to_change was:
file_to_change
/usr/bin/ /bin/
cat files_to_change | xargs ls and ls $(cat files_to_change) would be equivalent to running:
ls $(cat files_to_change)
$ ls /usr/bin/ $ ls /bin/
The output on the console should be what you want.
You need to use xargs. This very useful utility runs a command with arguments which are passed in as input:
cat myfile | xargs ls
ls 'cat your_file'
beware, the ' is the altGr+7 char (but used for formatting here !)
You can use
xargs
or command substitution to achieve this.Use xargs
The key is knowledge of the xargs command.
If you have a list of files in a file called
files_to_change
, you can print them with the following one liner:Use command substitution
An alternate method is to use command substitution. This works the same as above.
Two different one-liners, using different syntax:
It won't matter if they are files or directories, and you can run any command on them.
If the contents of
file_to_change
was:cat files_to_change | xargs ls
andls $(cat files_to_change)
would be equivalent to running:The output on the console should be what you want.