Search code examples
linuxcommand-linecatls

Linux Commandline Combining files


For a command like this,

ls -rt | tail -n 100 

Will give the latest modified 100 files. What commandline tool should be used to pipe the result of this query to, so that all the filenames that is shows are copied to a single file. eg:

If a folder has 100 files, the above command will give me all the filenames. I want to copy all the data in all the files to a single 101th file. How do I do it?

Commandline to cat each file to the destination is what I am looking for. But I don't know where to start.


Solution

  • ls -rt | tail -n 100 | xargs cat > file

    1. The xargs command repeatedly reads text from its standard input stream, constructs a command-line using that text, then executes the command line.

    2. xags cat means that it treats each line of input text as a separate argument, and constructs a cat command using each input line as an argument to the cat command. It will construct as long a command-line as it can, so it will fork the minimum number of cat processes.

    3. The cat processes therefore read the files you are interested in, and write their content, in order, to the standard output stream.

    4. > file puts that text into the result file