Skip to content

One Line Linux “Twitter From File” Command

Dear Reader,

Sometime recently I was surfing around and came across a blog post where a user wrote a PHP script – fully OO, it was very pretty – that would pop the top line off of a file and tweet it, that struck me as odd. Actually, first it struck me as stupid since all twitter.com needs is another mindless bot spewing lines from a file at regular intervals, once I got past stupid it struck me as odd.
Update: ‘tweetFromFile’ PHP Class is the original blog post.

I used to have a friend named Michael Chaney. (I’m pretty sure he and I are friends but we’ve not spoken since June.) Michael once posted in an email that one of his hobbies/quirks was that he would try and find ways to condense complex tasks into a single line of bash script. (I’m paraphrasing) I thought of that email when I read the “Tweet a line from a file” post. It just strikes me that it’s a bit overkill to fire up PHP for a simple task like this.

Before you ask, no, I’m not falling out of love with PHP but it’s a “right tool for the job” issue. PHP can be used to do this, but it’s not really necessary. So, if we are going to ignore the web’s all-purpose sledge hammer, what can we use? How about the tools that come with Linux?

Source Material

First, you are going to need a file to tweet. Now if you are using this to do something stupid like tweet UTC every minute or the status changes of your dorm room lamp then stop right now. I do not want the powers I’m going to teach you used for evil. However, if you are planning something like tweeting the “Dead Parrot” skit one line at a time, read on my friend and make sure you ping me so I can follow you. Either way you will need a file full of single lines less than 140 characters each. For my testing, I started with a Unix fortune file “bofh-excuses”

If you are on CentOS?

yum install fortune-bofh-excuses

If you are not on CentOS, figure it out on your own.

I’ll leave it as an exercise for the user to figure out how to strip out the cruft from the file or just create your own file of wit ready for tweeting. Whatever you do, name the file tweets.txt and put it in your working directory.

Twitter Account

Yes Sparky, you will need a twitter account to play with this code. No Sparky, you can’t borrow mine. If you don’t have one or you don’t want to bother your followers with inane test messages, I suggest registering a new one. Grab one nobody would want. (Not that I did, my test account is @elePHPant)

The Command

Here it is in all its glory for those too anxious to wait for my explanation.

head -n 1 tweets.txt | xargs -r -s 140 -I {} curl -s -d "status={}" -u twitterAcct:p.ass.word  http://twitter.com/statuses/update.json > /dev/null ;sed -i '1d' tweets.txt

For those of you who see line noise from an old modem, let’s go through this line by line. Note: broken up like this is will not work, if you are copying and pasting, use the one above.

1
2
3
4
head -n 1 tweets.txt
xargs -r -s 140 -I {} 
curl -s -d "status={}" -u twitterAcct:p.ass.word  http://twitter.com/statuses/update.json > /dev/null
sed -i '1d' tweets.txt
  1. This command returns the first line from the tweets.txt file.
  2. This command helps us build a command to execute using the output form line #1. The -I {} is critical here as it’s what tells xargs to take the input – in this case coming from stdin – and replace every instance of {} with it. For safety’s sake, the -s 140 makes sure we don’t send twitter anything over 140 characters. Finally, the -r makes sure that it doesn’t call curl if there is no line to pass in.
  3. This is the heart of the command, a call to curl.
    • -s, tells curl to run silent. This does not prevent output but it suppresses curl’s normal output. Anything coming from twitter will still be output.
    • The next option, -d specifies the string of data to be POSTed. Since we need this to be POST instead of GET, we have to specify the data string this way. The string following the -d command is the data to be sent. This is normal HTML name=value pairs separated by ampersands.
    • -u allows us to specify the username:password pair. Twitter uses basic authentication so it’s easy to authenticate simple tools like this. It is also highly insecure since you have to actually type your username and password into the cron script. This is one of these don’t try this at home things.
    • The next parameter is the url to call. If you have questions about how to call twitter’s API, check out the twitter API page for details.
    • Finally, because this command will return data, the final portion of this command will dump anything twitter sends back, in our case, JSON, to /dev/null. There is no error checking on this command, it either works and you see the tweet, or it fails and you don’t.
  4. This command pops the first line off the file using sed, the Serial EDitor. The -i command tells sed to edit the file in place. the ‘1d’ says delete the first line. The final parameter to the sed command is the name of the file.

That’s it, drop all of those into a cron job that runs every 10 minutes, place 100 lines tweets.txt that advertise your blog, weight loss, male enhancement or a porn site and you too can reduce your followers to just the other bots who are following you in hopes that you will follow them so they can spam you.

I really don’t expect anyone to find this useful but it was an interesting exercise in bash, a tool I don’t get to use nearly as much as I would like to.

Until next time,
(l)(k)(bunny)
=C=

p.s. If you don’t follow me on twitter and want to, I’m a real person, not a bot. Follow me at @calevans.

Some Related Content: