Redirect file to ls

155 views Asked by At

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

3

There are 3 answers

1
Ezra On BEST ANSWER

You can use xargs or command substitution to achieve this.

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

    cat files_to_change | xargs ls
    
  2. Use command substitution

    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:

/usr/bin/
/bin/

cat files_to_change | xargs ls and ls $(cat files_to_change) would be equivalent to running:

$ ls /usr/bin/
$ ls /bin/

The output on the console should be what you want.

0
jwg On

You need to use xargs. This very useful utility runs a command with arguments which are passed in as input:

cat myfile | xargs ls
0
norisknofun On

ls 'cat your_file'

beware, the ' is the altGr+7 char (but used for formatting here !)