Bash Completion

Bash completion is a very good feature in Bash. We are able to complete filenames, just by pressing TAB. I had downloaded a YouTube video related to Tsar Bomba. Since YouTube videos are Flash Video files (.flv), I was not able to complete their filenames by pressing TAB in the terminal when trying to invoke mplayer.

A quick inspection of /etc/bash_completion solved the problem.

In mplayer(1) completion section,

_filedir '@(mp?(e)g|MP?(E)G|wm[av]|WM[AV]|avi|AVI|asf|ASF|vob|VOB|bin|BIN|dat|DAT|vcd|VCD|ps|PS|pes|PES|fli|FLI|viv|VIV|rm?(j)|RM?(J)|ra?(m)|RA?(M)|yuv|YUV|mov|MOV|qt|QT|mp[34]|MP[34]|og[gm]|OG[GM]|wav|WAV|dump|DUMP|mkv|MKV|m4a|M4A|aac|AAC|m2v|M2V|dv|DV|rmvb|RMVB|mid|MID|ts|TS|3gp|mpc|MPC|flac|FLAC|flv)'

flv was appended at the end. That was it. Problem solved! Happy viewing!

Redirecting output to a file with sudo

Sometimes, we may need to redirect output to a file owned by root ( or another user ) to which we have permission via sudo.

[alanhaggai@archer ~]$ sudo touch test
[alanhaggai@archer ~]$ stat test
  File: `test'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 281248      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2008-09-27 17:08:29.000000000 +0530
Modify: 2008-09-27 17:24:03.000000000 +0530
Change: 2008-09-27 17:24:03.000000000 +0530
[alanhaggai@archer ~]$ sudo echo 'hello' > test
bash: test: Permission denied

sudo executes echo 'hello' as root. Then bash tries to write the output to the file named test as the user alanhaggai. man sudo states:

sudo [-bEHPS] [-p prompt] [-u username⎪#uid] [VAR=value] {-i ⎪ -s ⎪ command}

To circumvent this problem, we use:

[alanhaggai@archer ~]$ sudo bash -c 'echo "hello" > test'
[alanhaggai@archer ~]$ cat test
hello