Category Archives: Command Line

Disk and Memory Usage Scripts

At times I like to check my levels of disk and memory usage and it’s more convenient for me to do it from the command line. So, I created a couple scripts for them:

devtop 
 Filesystem      Size  Used Avail Use% Mounted on
 /dev/sda5       9.8G  6.4G  2.9G  69% /
 /dev/sda6       166G   38G  121G  24% /home
memtop 
 PROGRAM                   %MEM    #MEM
 firefox                   10.2    352.98 MB
 gnome-shell               4.1     141.76 MB
 Xorg                      1.5     53.60 MB
 nautilus                  1.2     41.52 MB
 gedit                     1.1     40.59 MB
 gnome-settings-           0.7     26.23 MB
 gnome-terminal            0.6     22.31 MB
 nm-applet                 0.6     21.30 MB
 python2                   0.6     20.89 MB

Saves me a lot of time over having to open a program :).

Convert Videos to PSP



I’m a TED video junkie. I always have videos on my PSP ready to watch. I also like to put YouTube videos on there. I did this enough that I created a script for it that makes putting videos on my PSP real easy:

 pspvidconv <d*> <video(s)> - Convert videos to PSP (d to use directory)

The PSP allows use of a single-depth directory. The directory option (when using d flag) will ask if the user wants to create a new directory, if the answer is no, it will present the existing ones.

Warning: Currently h264 encoding isn’t working. The PSP will report that it is an unknown codec, so mp3g4-xvid is the only option with ffmpeg.

h264enc

Because I’ve found that options and settings change frequently with encoding tools, it is better to have an expert be able to handle them (otherwise, I will spend more time looking options up again). A good program to use is h264enc. It’s a shell script (perl, I believe) and well done; not good for many files as all settings will have to be re-entered but does a good job.

Handbrake

For Handbrake GUI I found this post. I have yet to find any handbrake-cli lines that work.

Markdown to WordPress Format

When I discovered MarkDown I was in love, it was very nice to discover an easy, clean, well-thought-out markup language. I wanted a way to be able take my detailed notes, have them easy to read, and then to be able post them here on the blog that would be in a nice WordPress format. Yesterday, I wrote about discovering pandoc which enables a person to write in markdown and have it converted to HTML. To be able to use the HTML code created by pandoc in WordPress it needed to be slightly edited and cleaned up. Therefore,… I created a bash script for it.

Example conversion from MarkDown format to WordPress format

It works pretty good. Basically it removes tags for paragraphs (<p>), cleans up code blocks, improves formating, as well as doing a few other things. I haven’t gone through all permutations that pandoc conversion can do so a few thing swill be left out, but otherwise it should work for most things. If anything needs to be added please let me know.

Pandoc-flavored markdown: Perfect!

I’d been looking for a way to convert my notes to webpages. Typically I wrote my notes in .txt form and then went through them and added links, formatting… when I was ready to blog them. Recently, I had asked StackOverflow if I could convert MediaWiki format to HTML. I’m an Administrator for the Arch Wiki so I’m very familiar writing this format. This is when I learned about pandoc. Pandoc’s author describes pandoc as, “If you need to convert files from one markup format into another, pandoc is your swiss-army knife…“.

Plain Text Example

I planned to write my notes in mediawiki format and then convert to HTML, however I’d been using StackOverflow lately and started to learn Markdown Prose and really like it. Markdown’s is designed to be easy to write and read: “Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML)“. Markdown is so easy to write that it makes sense that sites like StackOverflow and GitHub are using it.

Markdown, Markup Languange

Pandoc has extended markdown to use code blocks ```, tables, and a few other things and converting is very basic:

pandoc file.md -o file.htm

I’m in love, I’m in love, I’m in love :), as it will save me a good bit of time in editing/creating Documentation. Tomorrow, I’ll post a bash script to clean up the HTML to be able to put it in a WordPress Blog.

makepkg Shortcuts Script

This one is not as important as the pacman related script but I find I use it often too. I maintain several packages in the AUR and it comes in handy to quickly refer to common tasks related to makepkg. The md5sum function still needs a bit of work (i.e. it requires the build function in the PKGBUILD be able to place the md5sums nicely). Otherwise it’s pretty ready. Here’s what it does:

 mp <option> - common makepkg building tasks
  b - build package (install dependencies if required)
  m - update md5sums
  r - remove previous build directories
  s - create source files for upload, prompts for submission to the AUR
  t - create PKGBUILD template

(ar)ch (pa)ckages – a generic package tasks script for Arch Linux

I once saw a wrapper-script for pacman in the forums that was basically a short-hand version of common pacman tasks. I thought this was a good idea and over the last couple years, I’ve expanded on it. It does just about everything I need it to. It’s real basic and I call it arpa. Here is a basic synopsis:

arpa [option] [*package] - a generic package tasks wrapper script
  -e, --explicit - install a package as explicit
  -g, --get      - get/download package upgrade(s)    : -G get pkg upgrades all
  -i, --install  - install a package                  : -I install as dependency
  -l, --list     - list package files                 : -L list pkgs installed
  -o, --owns     - owning package of a file
  -q, --query    - query for an installed package     : -Q query w/ description
  -r, --remove   - remove a pkg and its deps          : -R force, no argue orphs
  -s, --search   - search for a package               : -S search w/ description
  -u, --upgrade  - upgrade system                     : -U upgrade AUR
  -y, --sync     - sync package db

Good for me to have this around so I can remember everything :), and it is in the AUR.

Embedded Scripts in WordPress with GitHub Gist and Update Script

I asked at Stack Overflow recently if I could embed a text file into a webpage. My reason was basic: I wanted to be able to use my newly created GitHub script repository to be my source for scripts I posted on this blog. If I was able to do this, I reasoned, than my script on the blog will be up-to-date when I updated my GitHub repository. Unfortunately, there appears to be no direct way to do this that I could find so I look for an alternative and found GitHub Gist. GitHub Gist’s description:

Gist is a simple way to share snippets and pastes with others. All gists are git repositories, so they are automatically versioned, forkable and usable as a git repository.

I was hoping that there would be a way to link a script but there isn’t. Basically the standard process it to visit the GitHub Gist WebUI paste the script, config, … and then post the link on its own line into WordPress.

Because this creates git repository it means it can be updated. So I wrote a script does two functions: 1) Creates a repository for a file; 2) updates all files listed in the script with a Gist repository.

Works pretty good, there are a couple caveats though. First, Gist does not recognize the interpreter on the first line of a script and instead uses the extension. I tend not to use the .sh extension but I wanted syntax highlighting so the script on the blog now are labeled as name.sh which I guess isn’t a huge deal. Second, each script must have it’s own repository or all the scripts, configs… would be placed when put into a post. Not sure if this a breach of etiquette but I think I’m ok.

The script requires defunkts excellent gist command line upload tool.

The syntax is such:

 ghsync-gist   - Add or update gist repo(s)
  a - Add gist repo for file(s)
  u - Update all gist repos for all files

YouTube from the command line

How to play YouTube videos became a question to me because flash in Linux is all but abandoned and knowing of flash security concerns. Good news is that most videos now are being converted to WebM (Firefox HTML 5 video) but for some unknown reason I find most of them that the sound won’t play. I really enjoyed watching YouTube videos so since I’ve gone to using youtube-dl which is a great command line program (there is also youtube-viewer).

I wrote a wrapper script do that to be able to download a number of varying videos:

youdl 
 youdl "youtube.com/url" # quotes necessary, not spaces

youtube-dl

I like youtube-dl quite a bit too but it takes more setting up. When using youtube-dl I put these in my ~/.bashrc to simplify the process:

utube-view () {
  mplayer -cache 200 -really-quiet $(youtube-dl -g --max-quality mp4 "$@" )     
}

youtube-viewer

youtube-viewer directly invokes mplayer:

youtube-viewer "http://youtube.com/video-url"

And it works great! The quotes are necessary to avoid bash interpretation of specialcharacters. youtube-viewer can also download the video too:

youtube-viewer -d "http://youtube.com/video-url"

MPlayer Keyboard Shortcuts

Got this from here:

Managing Scripts and Configurations on Github with a script

This post is a follow-up to Michael Smalley’s excellent post on how to manage your dotfiles.

Use Git and Github to Manage Your Dotfiles. I wanted a way to regularly have my configurations and scripts updated on Github that didn’t require me remembering how to do it :). So I created a script that would do it for me:

Works pretty good. Then I put these in my crontab to have them updated every week.

Sudoers Permissions as a File

I learned that for granting root permissions to certain programs that it is easer and more constructive to use a separate file. sudo must be told to look in a separate directory in its configuration to be able to do so.

sudo visudo

Likely all distributions have this available and it will be listed at the end:

#includedir /etc/sudoers.d/  

The # is necessary. Also the trailing forward slash is likely necessary (I had to add it); when it wasn’t added, files in /etc/sudoers.d/ would not always get recognized.

Here’s my configuration built from an excellent tutorial in the Ubuntu forums. I usually build these per user naming them user_<USER>.

# Allowed root permissions of programs for user USER

# Aliases
Host_Alias HOST    = aspire
Cmnd_Alias G9LED   = /usr/bin/g9led
Cmnd_Alias IOTOP   = /usr/bin/iotop
Cmnd_Alias PACKER  = /usr/bin/packer
Cmnd_Alias PACMAN  = /usr/bin/pacman
Cmnd_Alias SANDFOX = /usr/bin/sandfox
Cmnd_Alias MYPROGS = G9LED, IOTOP, PACKER, PACMAN, SANDFOX

# Programs allowed for user or computer
todd HOST=(root) NOPASSWD:MYPROGS

The configuration will need to proper-permissions:

sudo chown root:root /etc/sudoers.d/user_<USER>
sudo chmod 0440      /etc/sudoers.d/user_<USER>

Allotting iotop

Recently, iotop has been moved to being only allowable to be viewed as root. iotop is a great program for measuring disk throughput and I am unable to figure the logic of why it has been moved to root-only. To be able to run iotop as regular user again root permission must be given to the regular user for the program. A good way is to create a sudoers file per-user of allowable programs.

Then iotop can be invoked by (without need of a password):

sudo iotop

Resources

Follow

Get every new post delivered to your Inbox.

Join 52 other followers