Wednesday, March 5, 2014

Getting a random file from a directory

I had a massive directory of files that I wanted to pick a few random files to test before I imported the whole set. For this post lets say I want 100 random files from some_dir and moving them to this_dir.

First you could use:

$ LIST=$(ls -1 ./some_dir | sort -R | tail -n 100)

but coreutils provides a shuffle program...

$ LIST=$(ls -1 ./some_dir | shuf -n 100)

From there you can copy the files using

$ for i in $LIST; do echo $i; cp "./some_dir/$i" ./this_dir/; done

Substituting in our variable:

$ for i in $(ls -1 ./some_dir | shuf -n 100); do echo $i; cp "./some_dir/$i" ./this_dir/; done

But that line looks messy because the source directory has to be repeated.

I think this one looks better, but to use this we can to use find to output the relative path:

for i in $(find ./some_files/ -print -type f | shuf -n 10); do echo $i; cp "$i" ./this_dir/; done

Next we can throw that in a function:



And after defining it call it like:

cp_rand_subset 100 some_dir this_dir

Or

cp_rand_subset 57 ./lots_files /home/share/samples/

Thursday, July 25, 2013

LVM resize

My root partition recently ran out of space and I needed to resize it.

So I booted into a live usb and then, after activating my volume groups:

$ sudo vgchange -a y

I looked up lvm resize online and came up with the command:

$ sudo lvextend -L+10G /dev/mapper/big-root

Next I rebooted and ran "df -h". It was the same size and I was annoyed and confused. After some routine poking around I noticed the man page suggested the use of a "-r" flag to actually resize the file system. Who knew?

Final command:

$ sudo lvextend -r -L+10G /dev/mapper/big-root


PS: Oh and if you need to reduce the size of the partition because you ran the command without modifying the filesystem and you don't want to learn how to use "resizefs"

$ sudo lvreduce -L-10G /dev/mapper/big-root


UPDATE: A reader has suggested that the previous code block might be misleading. To reduce the size of a lvm partition,  you should use the following command with the "-r" flag or you will risk losing data. The file system must be resized before the lvm partition's size is reduced. However in the previous example, my filesystem was the size of the partion after reduction.

$ sudo lvreduce -r -L-10G /dev/mapper/big-root


Note: Commandlinefu says it better and faster:
http://www.commandlinefu.com/commands/view/12195/lvextend-logical-volume

Monday, April 22, 2013

extension ram files

I was helping my girlfriend watch some videos that she needed for a class that she was having trouble playing on her mac. Once I saw that ubuntu wasn't able to play them out of the box I looked at the extension and found a solution on ubuntu forums.

The solution suggested that I try mplayer to play the file:

$ mplayer -playlist file.ram

Success! I could watch the video streaming.

 However, it also pointed out that extension ram files were just real player's playlist and the file was simply a text file. I opened the file and it contained one line which was the url to the video. A .rm file, real players video file, that I was sure I could covert with ffmpeg.

The url was rather long so I used cat and wget to download the whole file.


$ wget `cat file.ram`


Next, I used a tutorial on ehow to find the correct flags for converting using ffmpeg. (I am lazy and why read man pages when you have examples you can search and find)


$ ffmpeg -i IN.rm -vcodec mpeg4 -sameq -acodec aac -ab OUT.mp4


It seems I am never going to remember that avconv is the new replacement for ffmpeg. But no matter what I tried I couldn't seem to get it to work.

I decided because I knew it would run in mplayer that mencoder was the logical choice.


$ mencoder IN.rm -aid 0 -vid 1 -ovc lavc -oac lavc -o OUT.mp4


And that .... worked a little but the video seemed poorly made.

Next i tried something called transmageddon which was a gui and seemed to spin rather than work. More to come if I get it to work...


Sources:

http://ubuntuforums.org/showthread.php?t=1177723
http://www.ehow.com/how_6306550_convert-realmedia-mp4-using-linux.html

Wednesday, April 17, 2013

Wireless Broadcom

So I have had this issue where my wireless card would one day suddenly disappear. I had a wireless usb so I used that for a month or so and finally I decided that I would just reinstall and see if that fixed it. It did like magic but then a couple days later poof gone. Now if you are reading this there is a good chance you have search for linux and broadcom related information and you have found a ton. Ultimately I fixed mine using information from a few sources:

First you can list your network related hardware using:

$ sudo lshw -C network

Confirm that you have a broadcom chip. In my case the broadcom chip was listed as "*-network UNCLAIMED". Next you can check for some sort of blocking

$ rfkill list all

In my case before I fixed the issue my wireless device wasn't even listed. After that I would run:

$ lspci -vnn | grep 14e4:

In my case after finding that command I check back on the table at http://linuxwireless.org/en/users/Drivers/b43#Supported_devices. My device was listed as 14e4:4727 which is BCM4313 (same as lshw listed) Next I looked at http://linuxwireless.org/en/users/Drivers/brcm80211 I found my chip in a table listed under "
brcmsmac" from my research I knew that "brcmsmac" was one of the options for broadcom drivers. So I tried modprobing it as I had done for 'b43' and 'brcmfmac' earlier.

$ sudo modprobe brcmsmac

And success!

Lastly if you don't want to run that command every time you turn your machine on you could add "modprobe brmsmac" on a line immediately before "exit 0" in the file /etc/rc.local


The four most helpful and complete sources:
https://wiki.archlinux.org/index.php/Broadcom_wireless#broadcom-wl
http://linuxwireless.org/en/users/Drivers/brcm80211
http://linuxwireless.org/en/users/Drivers/b43#Supported_devices
http://askubuntu.com/questions/235279/my-wifi-adapter-is-not-working-at-all-where-to-start-troubleshooting



Tuesday, April 16, 2013

Forward and Back Buttons

For what ever reason perhaps just nostalgia possibly because I happen to use them more than expected, I have forward and back links on my website. The buttons are JavaScript powered and I have had "noscript" tags warning users of their lack of functionality since I added the links years ago. However, I don't like that they still exist without scripting. Anyway, I decided that I was going to use scripting to draw the buttons.

<script type="text/javascript">
            document.write('\x3Ca href="javascript:history.back()">Back\x3C/a>');
            document.write('\x3Ca href="javascript:history.forward()">Forward\x3C/a>');
            document.write('\x3Chr />');
</script>

Note: that by using '\x3C' rather than '<', I eliminated a potential problem in the parsing of the page and made it so that they would validate again. 

Saturday, April 6, 2013

Image Magick IV: watermark

I decided to try and start signing my pictures.


$ for i in *.JPG; do echo -e "$i... \c"; composite -watermark 15% -gravity southeast sign-1.png $i $i; echo done; done

It needs more tweaking but that will do for now.

Tuesday, March 12, 2013

Play DVDs

To play dvds first you must install a package:


sudo apt-get install libdvdread4


Next you will run this command and like magic it works:


sudo /usr/share/doc/libdvdread4/install-css.sh

You may need to reboot: