Little Girl's Mostly Linux Blog

TextFileListInCommands

Use a text file list in commands

This page was last updated on October 12, 2009.

Either of these two methods will let you use a text file containing a list as input for a command. The list can contain existing file names complete with paths for use in complex commands.

Method 1

Replace FILENAME with the name of the text file and COMMAND with the command you’d like to run on the items listed in the text file:

COMMAND $(cat FILENAME)
  • Example:
    1. Create a text file on your Desktop named mylist.txt and put this information into it:
    2. mytestfile1.txt
      mytestfile2.txt
      mytestfile3.txt
      
    3. Open a terminal window.
    4. Type this command to change to the Desktop directory:
    5. cd Desktop
    6. Type this command to use the text file to create three files:
    7. touch $(cat mylist.txt)
    8. Look on your Desktop or type this command in the terminal window to see the three files you created:
    9. ls
      • The result should be something like this:
      • mytestfile1.txt mytestfile2.txt mytestfile3.txt

        Note that if you had other files on the Desktop, these will also be included in the list.

Method 2

Replace FILENAME with the name of the text file and COMMAND with the command you’d like to run on the items listed in the text file:

cat FILENAME | xargs COMMAND
  • Example:
    1. Create a text file on your Desktop named mylist.txt and put this information into it:
    2. mytestfile1.txt
      mytestfile2.txt
      mytestfile3.txt
      
    3. Open a terminal window.
    4. Type this command to change to the Desktop directory:
    5. cd Desktop
    6. Type this command to use the text file to create three files:
    7. cat mylist.txt | xargs touch
    8. Look on your Desktop or type this command in the terminal window to see the three files you created:
    9. ls
      • The result should be something like this:
      • mytestfile1.txt mytestfile2.txt mytestfile3.txt

        Note that if you had other files on the Desktop, these will also be included in the list.


Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: