Thursday, August 2, 2012

Command Line Copy/Paste

At times I find myself grabbing and copying some text from a GUI editor, and wanting to save it all into a file somewhere for command line access and manipulation (e.g. log files). My usual approach used to be one of the following (numbers in parentheses are the number of keystrokes used):
  1. Open Spotlight(2); Open TextEdit(2+O(1)+1); [PASTE](2); [SAVE](2); type in the new filename(N); Select WITH THE MOUSE to save it as a plain text file and NOT as an RTF as is TextEdit's freaking default setting, and click SAVE(3 mouse seeks, 3 clicks!); Close TextEdit(2 or 4, I close the window first with Command-W before Command-Q to avoid the file re-appearing when I open TextEdit the next time); Move this file to where I want it through Terminal. (O(N)). At least 15 keystrokes AND 3 mouse seek+clicks - it's a freaking long process for a task so punctual.
  2. type "vi <filename.txt>" in Terminal(4+N); Press "i" to enter INSERT mode(1); [PASTE] the contents(2); ESC, Save and Exit with ":wq"(6). 13+N keystrokes in total, much better considering the mouse is never needed.
The second option sounds way better, although sometimes when I copy some text many MB long, vi dutifully attempts to load it all up into memory and display it for me on the screen. This can take a while, plus for especially lengthy texts, it encourages dreaded pagination to kick in.

So I Googled around for a bit and found two shiny little gems that will redeem my inter-Terminal-GUI interfaces (at least on my Mac OS X):
  • pbcopy < inputStream
  • pbpaste > outputStream
As straightforward as they should be. 8+N keystrokes, and no further user interface required. Would be absolutely perfect to automate GUI tasks, if I knew how to handle GUI's from the command line in detail.

Credits where credit's due:

Friday, July 27, 2012

Command-Line Media Conversion

Tired of searching for those ever-changing media converter applications, or for an online conversion site that is actually working at any one time?

Search no more!! Install ffmpeg (and sox too) and learn how to convert media from most formats into most other formats... easily-peasily!

Common usage:

ffmpeg -i input.mp4 out.mkv
Done! Give it whichever input file in any format, tell it which file to output to (with the appropriate extension), and press [ENTER]! You'll have your toasty new file in a few seconds, minutes, hours, days, months, or years, depending on your computational power, file size, network speed (if applicable). Most likely a few seconds will suffice. Convert AVI-MP3, MP3-WAV, AVI-MKV, MOV-AVI, MKV-MP3, MOV-AIFF, or whatever you want! (Just make sure it makes sense - you can get audio out of video, but most probably not the other way around).

Also consider sox, an audio-specialized application with a wider spectrum of formats, precise control over channels, sample rates, dithering, and many other audio-specific attributes, effects, and synthesizers, plus other great tools like direct file playing and recording:
  • Play a music file: play musicFile.mp3
  • Recording yourself into a wave file: rec myRecording.wav
  • Convert all the .raw files in your current directory to wave files: for f in *.raw; do sox -r 16000 $f ${f/.raw/.wav}; done

There ya go! Apply and enjoy! Credits due:

VI Capitalization

Have you found yourself in the need to either capitalize or de-capitalize a large amount of text, and realized that your text editor did not have that capability, so you proceeded to re-type all of your text in your desired capitalization?

Re-type no more!!! vi will save you, as it has save many others so often before!!

  1. Open your file in vi.
  2. To convert all to uppercase, press gggUG
  3. To convert all to lowercase, press ggguG
  4. Done!
Alternatively, you can type in either of these longer vi commands:
  • <ESC>:%s/.*/\L&/
  • <ESC>:%s/.*/\U&/
though surely variations of these allow you much greater flexibility for more precise and/or complex substitutions.
Enjoy!

And of course, credits due to:


http://www.lefred.be/?q=node/75

Thursday, July 26, 2012

Finding/opening your smallest/largest images/files

So I have a folder of about 14K pictures and videos, and I decided to try to find the smallest images in my set (I was actually looking for the darkest ones, and it kinda worked). So I made up this command, and it works out nicely:

open $(find . | grep JPG | xargs ls -all | sed 's/  */ /g' | cut -f 5- -d' ' | sort -nr | tail -n 500 | cut -f 5 -d' ')



Logical steps:
  1. find . : Shows all files inside the current directory, and recursively inside all subdirectories.
  2. grep JPG : Remove all no-JPG files from the list (folders, system files, video files, etc).
  3. xargs ls -all : Re-list all the files, this time with all file attributes (size, permissions, etc).
  4. sed 's/ */ /g' : Replace all space sequences with a single space.
  5. cut -f 5- -d' ' : Grab only the attributes starting from the file size column (this might vary depending on your ls default attributes. Tweak the "5" to accomodate your own settings).
  6. sort -nr : Sort descendingly by file size.
  7. tail -n 500 : Grab only the 500 last files (I didn't want to open ALL of them).
  8. cut -f 5 -d' ' : Grab only the actual filename (remove all other attributes).
  9. open : Open them in Preview! (Or whatever your default image-viewing application is).
  10. View!

Google Web Search Keyboard Navigation

A few days ago, I realized that Gmail has the best keyboard UI among all of the Google products. Its keyboard amenability is a highly desirable feature among users who like performing tasks without frequently switching over to the mouse.

So today I was telling a friend about this, and about how even Google Web Search does not have such a keyboard-friendly interface. I opened a Google Web Search window to demonstrate how one couldn't navigate up and down the searches with "j" and "k", and how one could not open a search result with "o". But then I thought maybe it was navigable with arrow keys. So I tried it, and sure enough, it works!!! Steps here:
  1. Search for something on Google.
  2. Press [TAB] to remove focus from the main search text box.
  3. Press [UP] and [DOWN] to move up and down the search results!
  4. Press [ENTER] to open that search result! This also replaces your current browser tab. OR...
  5. Press [Command]-[ENTER] to open the search result on a new tab! I imagine it works with [Ctrl]-[ENTER] on Windows machines.
Now I'm curious to see what other Google products I can find keyboard shortcuts for. I've noticed Google Play is notoriously lacking in these, but maybe YouTube and Maps have matured to the point of keyboard interactivity.

And... ENJOY!!

Wednesday, July 25, 2012

Bash Pause

How do you pause a bash script, either to prompt a keypress or to actually get some input into the program? Easy peasy, use the read -p command!

Such as:
read -p "Press [Enter] key to start backup..."

Thanks!:
http://www.cyberciti.biz/tips/linux-unix-pause-command.html

Wednesday, June 27, 2012

Folder sizes in Mac OS X Finder

So my hard drive is reaching that point when it feels like I'm stuffing the socks inside the jars to save space. Performance is degraded, typing is slow, and you want to get rid of the useless big files. What do I do? I try to remember where the big files are, and go looking for them sequentially. What if I can't remember about those 200 useless YouTube videos I downloaded that one time, or about that music folder a friend passed me through his USB key and I never listened to? Wouldn't it be nice if the computer could tell you: "Hey, there's this 10Gb bunch of files you haven't used in 2 years - maybe you'd like to clear THOSE up?"

Well, now you can! On Mac OS X finder, anyway. I'm sure this feature's been around for ages, but it's only been a few months since I figured it out, and though it's not handy every day, when it is it saves you a LOT of time.

Finder can show you the size of entire folders. It takes some time to compute and some space to store, I'm sure, but oh yes it can. Steps:

  1. Go to Finder.
  2. View -> Show as List (Or WeirdClover-2)
  3. View -> Show View Options (Or WeirdClover-J)
  4. Check checkbox "Calculate all Sizes", and if unchecked, also check "Size".
  5. Done!
Now you should see the aggregate digital space consumed by everything inside it. Isn't it nice? Sort by the "Size" column, and you should start to free up space really quickly by deleting that folder at the top of the list that takes of 15% of the hard drive (Zipf's Law).

And, enjoy!