Paul’s Linux Blog

Just Another Uber-Geek

Browsing Posts in Shell Scripting

Thanks to this great little script you no longer need to work out which program to use to uncompress multiple file archive formats.
Add this script to your .bashrc file and use as follows:
extract file.zip

 
extract () {
if [ -f $1 ] ; then
case $1 in
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.bz2)   <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvjf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.gz)    <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvzf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.xz)    <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvJf $1    ;;
*.bz2)       bunzip2 $1     ;;
*.rar)       unrar x $1     ;;
*.gz)        gunzip $1      ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>)       <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvf $1     ;;
*.tbz2)      <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvjf $1    ;;
*.tgz)       <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvzf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">zip</span>)       unzip $1       ;;
*.Z)         uncompress $1  ;;
*.7z)        7z x $1        ;;
*.xz)        unxz $1        ;;
*.exe)       cabextract $1  ;;
*)           echo "\`$1': unrecognized file compression" ;;
esac
else
echo "\`$1' is not a valid file"
fi

Right now I need to convert some flv's to avi so I can watch them on my ps3.

Once again a little googling and 5 minutes later I have the method. Once again I find that mencoder comes to the rescue.

 mencoder -oac copy -ovc lavc -o output_name.avi input_name.flv

I love mencoder it probably one of my most used commands these days. I travel to work on the train every day and it's an hour each way. I find that wathcing a DVD is ok but it drains the battery on the laptop and also generates heat not to mention I don't like carrying my DVD around in my drive all day.

The solution is MENCODER. more on this to follow.

I found myself today needing to move some files around but unfortunately they had spaces in them and would not move as expected.

A quick solution soon found using the infamous 'FIND' command and some examples.

the most basic kind of search is as follows:

 find . -name "*.avi" -print

It quite simply finds all files the have .avi on the end.

However sometimes you need a bit more, say you want to see the file sizes for these files:

 find . -name "*.avi" -exec ls -l {} ;

Now we should get the file attributes for each file found, note the {} expand during the execution of the script and are replaced by the filenames found.

Lets say we want to move these files into the same directory, but some files we don't want to move, we could use the following command for this:

 find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec cp -v {} /usb01/ ;

The command in this example says find in directory "/" files with .avi AND (-a) NOT (!) Clip or clock, then execute the copy command (cp) filename into /usb01/

The next part is to remove original files, this could all be done on the same command line but I like to check things have worked before committing to a delete.

 find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec rm -v {} ;

496 find / -name "*.avi" -exec file {} ;

I also cam across the following command that search the path ie the directory name as well, I have not used it yet but thought I would add it.

You can match the entire filename with -path instead of -name.

 
find . -path '*/139.P/*smooth' -exec mv "{}" destination ;

Hope this explains some of the mysteries of the find utility.

The other day I connected my laptop to my work via VPN, great I thought now to connect to my laptop from my work PC using SSH computer say's NO! :(

I could log into to my work desktop however from the laptop, It turns out my works firewall blocks the port 22 so ssh fails to connect, not wanting to give up I thought maybe using an SSH tunnel to my work PC and then use the work PC to connect back to my laptop through the tunnel, after some googling I finally worked it out and managed to get it to work. :)

The code below works as follows:

-N stops the command from executing on remote server, without it you end up logged onto the remote server.

-R Remote Port

What this happens is the port 22 on the localhost (my laptop) uses ssh to connect to the work desktop and binds to port 2210, after entering my password the connection is made.

ssh -N -R 2210:localhost:22 work.desktop.com

To connect to my laptop I use the below command, this makes an ssh connection from my work desktop to it's port 2210, as this a tunnel the ssh is automatically forwarded to my laptop and tada the connection is made. :)

ssh -p 2210 localhost

See toic.org for more info

Powered by WordPress Web Design by SRS Solutions © 2010 Paul’s Linux Blog Design by SRS Solutions