Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Jun 26, 2011

Save Streamed Flash Video [Firefox-4] [Linux]

It cost me almost half an hour to find the location, where Firefox saves the streamed video. And I made a script for it as I often need it. Here's the script, hope it helps you:

file /proc/$(pidof npviewer.bin)/fd/* | grep -i "flash" | \
  cut -d':'  -f1 | xargs -i{} cp {} ./ 

Details of what the command will do:
Fifefox-4 uses an external process (npviewer.bin) to manage external plugins. So what I did was grabbed a copy of the deleted flash file (yea it deletes the file, but keeps a secret opening for itself) from npviewer.
Finally all the cached video are copied to current path named with some random numbers (technically, the number is file-descriptor).

Warning: Execute the command after complete streaming and before closing the firefox(-tab).

Dec 23, 2009

Format Numbers to be Comma Separated

I have a number 1234567890, and I want to insert a comma at the gap of three digits like this:1,234,567,890

If this is the problem running in your head then the following one liner will be helpful to you.

$ echo "1234567890" | sed -e ':a' -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
1,234,567,890

Sep 29, 2009

Bulk upload photos to facebook [BASH]

I got tired of uploading one photo at a time. So I googled a bit, and found few scripts but none did convince me. So I set to write one by myself. And my focus was to keep it short and simple(KISS).

The tool is a python script with following features:
* Bulk upload the pictures to facebook.
* Supports upload from specified folder or current folder (default).
* Allows to choose from available albums.

Usage:
upload2facebook [-a|--aid <album_id>] [<path>]

Install:
(Don't forget to read the notes below)
In Debian (Ubuntu, Kubuntu,...) system:
1. Install from ppa https://launchpad.net/~ssapkota/+archive/ppa
2. Install directly from deb-package -> upload2facebook_0.2_i386.deb , Package Detail

In RPM (Fedora, Centos) system
3. Install the rpm -> http://ssapkota.com.np/downloads/upload2facebook-0.2-2.i386.rpm

4. Get the source, do whatever you like -> upload2facebook_0.2.tar.gz


Note-1:
During the installation of this tool, it will ask you to enter the Application Key and Application Secret Key. To get this two key you need to register an application for yourself at facebook. Follow the following (necessary) steps:
1. Go to http://www.facebook.com/developers and click on Setup Up New Application at the top right corner.
2. Enter the Application Name (I suggest : Photo Uploader)
3. Then click Agree and then Save Changes
4. After this you will get
API Key and Secret. Copy it and save it somewhere; you'd need later. Remember, these keys are meant to be secret and shouldn't be shared (So, did I ;) ).
5. You need to do one more thing to make it work; On the left-navigation click on the Canvas and then enter a URL(may be your website/blog link) as Canvas Callback URL. I don't think this is of any use for command line application, but to make the application work you need to set this.
6. Thats it. Return to the installer and Enter your App Key and App Secret.

Note-2:
1. The tool is developed and tested in Ubuntu and works in all the Debian system.
2. The RPM package is generated using alien and have not been tested.
3. The code is written in python and is OS independent, One can easily port to any other Operation system.
4. If you have tested the tool in other system or have ported it to other OS, please post the status in the comment.

Enjoy the tool. :)

May 24, 2009

Bluk rename files in Linux

Today one of my friend asked me to bulk rename all files in a folder. He had a collection of songs named randomly with no extension. He also wanted to add the extension, mp3 in all files(Yes, he was sure about the encoding)
I opened a terminal in that folder and executed the following command.
NUM=1
for SONG in ./*
do
mv $SONG song$NUM.mp3
let NUM=NUM+1
done

So lets peek what the above script does:
  1. Defined a variable NUM and initialized it with 1.
  2. Defined a foreach loop. Here for each files in current path the for loop is executed.
  3. Rename(move) the file $SONG to song<num>.mp3
  4. Increase the value of variable NUM by 1.
  5. end. ;)
So if you want to rename in a different fashion you can hack the line mv $SONG song$NUM.mp3 in any way you like. But Remember the second argument to the mv command must vary for each loop, otherwise you will end up deleting all files except the last one.

Apr 30, 2009

Utilize Grep Sed and Awk

In this post I will list commands that I use and include at least one of grep,sed or awk.
  • cat filename | grep "phrase"
    Search line containing phrase in file
  • cat filename | grep -v "phrase"
    Search line not containing phrase in file
  • cat filename | grep "phrase1\|phrase2"
    Search lines containing phrase1 or phrase2 in file.
  • sed -i "s/phrase1/phrase2/g" ./filename
    Replace phrase1 with phrase2 in file.
  • sed -i "s/[ ]*\(.*\)[ ]*/\1/g" ./filename
    Trim spaces of each line of the file.
  • awk '{if($7=="2") $7="5"; print;}' file
    Conditional modification. Field no.7 is is replaced by "5" if it is "2". The default field seperator is .
  • uptime | awk 'BEGIN {FS=" "} { gsub(",",""); if (index($0,"day")) {gsub(":"," hours, ",$5);print $3" "$4", "$5" minutes"} else {gsub(":"," hours, ",$3); print $3" minutes"}}'
    Get the Uptime in a proper and clear format.
  • ls | grep -v 'file or folder or regex' | xargs -I{} mv {} /target/folder/
    Move all but one to /target/folder. You can also use it to move all the files/directories in current directory to another directory in the same path.Be Aware: The Target directory should exist. Just to be at the safe side, I suggest not to miss the slash (/) at the end. This will give you warning/error if you messed up something.


For now I have just listed few that came in my mind; just thought to prepare a seed.
I'll promise to grow this list as soon as they come to my mind.

Note: If you have some commands in your mind, that you regularly use, plz post it as comment.