1

I often touch up files so they show up at the top of the file manager.

Is there a way to touch a list of files instead of touching each one individually?

touch test1 test2 is not what I am looking for.

Piping ... So the output of a list of files would be sent to the touch command.

Text list of files piped or whatever to touch = files with updated time stamps. A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing. So the output of a list of file would be sent to the touch command.

muru
  • 207,228
fixit7
  • 3,399

1 Answers1

3

Assuming your filenames are listed, one per line, in a text file, with no leading or trailing whitespace (unless the filenames themselves start or end with whitespace), and no filename contains a newline character (since if one did, the list would be ambiguous), this is a good use case for xargs.

The xargs command reads words from input and passes them as arguments to a command. By default it breaks up the words using arbitrary whitespace. Since it's common that filenames contain whitespace, this default behavior probably won't work for you, and could even end up operating on files that weren't in your list. However, GNU xargs (which provides xargs in Ubuntu) has a -d option that can be used to specify an arbitrary delimiter. From your description it sounds like you already have a list of files, one per line. In that case you can specify \n as the delimiter.

Suppose the filenames are listed in a file called files.txt. Then you can run:

<files.txt xargs -d'\n' touch
  • <files.txt redirects from files.txt.
  • xargs -d\'n' touch reads arguments from its input, split at newlines, and passes them to touch.
  • -d'\n' causes the shell to pass the literal text -d\n to xargs. It is xargs that treats the sequence \n specially, knowing you mean a newline.

You can of course put the redirection, <files.txt, anywhere in the command you like (though most people would put it at the very beginning or the very end). If, rather than a file, you have a command that produces the list, you can pipe from that command to xargs. For example, if you have a script in the current directory called make-list, you could run:

./make-list | xargs -d'\n' touch

xargs in Ubuntu does also have a -a option, if you're using an existing file as input and for some reason you prefer to pass it a filename as an operand rather than redirecting to it:

xargs -a files.txt -d'\n' touch
Eliah Kagan
  • 119,640