Posts Tagged ‘.bashrc’


Sunday, July 31st, 2011

Hacking the shell

I’ve scratched some itches this week, some by pure luck, others by seeking these itches out and scratching them with a vengeance ;)

Added to my ~/.bash_aliases is now alias pwd='pwd -P' which tells pwd to resolve the real path when you have cd’d in to a symlinked directory.

Not really a hack, but I discovered Meta-BackSpc / ESC-BackSpc which will, unlike Ctrl-w, delete parts of a path, instead of the entire path.

This thread was of great help in finding that key combination.

Also, thanks to Rikard I got hold of this document which outlines how I would go about getting a word, after the cursor, deleted. Before the cursor is eash (Ctrl-w), but after has always eluded me. Turns out it is Meta-d (or, as in my case, if that is overridden by your window manager, “ESC d”). VERY nice!

I did update my PS1 variable, both for root and my ordinary user. I don’t really know why I hadn’t thought of adding a time indicator before.

There have been times when I have started a long running process, and half an hour or so into it realized that it would have been nice to time the process. Now, this wouldn’t exactly be able to fully replace time, but could do in a pinch so to speak.

root: PS1='\n\[\e[0;33m\][\t] \[\e[0;31;5m\]\u@\h\e[25m\] \[\e[01;34m\]$(pwd)\n\[\e[0m\]# '

which will create the prompt:

[HH:MM:SS] root@host /present/working/directory

(yes, root@host IS blinking. root can haz be seriuz bizness!)

user: PS1='\n\[\e[0;33m\][\t] \[\e[0;37m\]\u@\h \[\e[01;34m\]$(pwd)\[\e[0m\]\n\$ '

and this produces

[HH:MM:SS] patrik@host /present/working/directory

ImageMagick and Animations

I kindof just assumed imagemagick would have some sort of way of creating an animated gif, and of course imagemagick didn’t let me down.

What surprised me was how incredibly easy it was.

Duck Duck Go (as I am attempting a google-free week) gave me this result, and once I had tweaked the parameters somewhat I ended up with this: $ convert -delay 50 -loop 0 root*.gif rootshell.gif

Zenity and passtore

I also managed to convince (nagged him until his ears risked falling off ;)) Rikard to try out my password management system passtore.

He did have one counter-demand however: he’d need a GUI for day-to-day operations. Me, being a wmii acolyte, can do without it, but I can see how he would feel different, and seeing as it would be a fun exercise, and I’d get him to give it a shot, I sat down and had at it.

Zenity was my first thought, and as it worked out well, it was my only thought ;) The working name for that code is “Heaven ZENt”. Yes I know, I am a stupid wanker, I can live with that ;D

I am however pondering whether I should release this code as a separate project (it depends on passtore, and is useless without it, which should indicate “no”) or not (it does introduce a dependency on zenity, which isn’t strictly needed to operate passtore).

At present time it is only a wrapper around the getpass script from passtore. In essence, “heaven ZENt” is “wyrd” to “passtore’s” “remind”. I am unsure as how to proceed. Input would, as always, be appreciated.


I’ve given myself the mission to move one of my (spam-trap) email accounts from Thunderbird to mutt. I really want to be able to use mutt as that would be one less thing I’d need to start X for.

So I am looking at other peoples .muttrc files, and came across the index_format setting. This controls how your list of emails in an “inbox” is presented. In Thunderbird this view (simplified) is “some flags, subject, some more flags, author name, even more flags, date”, and what I realize from the .muttrc files I’m viewing, is that at least some people place the date before author, before subject.

That got me thinking about whether this is some sort of brain hack, to force the brain into working in a different way. Coming from Thunderbird, when a new message arrives in a conversation my reaction is to either open it, or let it be for now, depending on whether it is a discussion I am interested in.

At that point I don’t bother to look at the author field, if the conversation is uninteresting (to me) I will simply defer it to later, and if it is interesting I will simply open the new mail and read it, thereby getting to know who wrote it.

Putting the author in front of the subject could let you filter on people rather than on subject, as we all have some people we listen more intently to than others, so whatever discussion they’re in, it might be worth while to read it.

But that still didn’t explain why they’d put date and time before author. The only idea I have about this is that some mails will be urgent and thus it is good to see when they arrived. But I don’t know. I think I will try out “flags, author, subject, datetime” and see how that goes.


If you mess up the datetime settings in BIOS (because you have powered down and unplugged your system during a thunderstorm, AND your motherboard is so old that the CMOS battery is dead) you may experience some problems when booting.

In my particular case, FSCK (or whatever binary it is that is performing a quick check on the file system during boot) reported that the datetime was now 2011-07-11 (because I fucked that up), but the last check had been performed in 2011-07-22.

This made it confused and prompted me to manually repair the file system, by entering the root password (which I have unset, -1 for sudo / me) and doing some operations.

That was actually not such a fun night trying to get to the bottom of that.


Blinging out your terminal, adding more colors

Thursday, August 5th, 2010

I recently installed Arch on my desktop, forsaking Ubuntu.

I still like Ubuntu and all, but it was an old version (Jaunty), too many packages were getting too badly outdated, and the one flaw with Ubuntu is that through bitter experiences I no longer trust dist-upgrades at all.

Arch promised a solution to this, in rolling upgrades. It hasn’t convinced me yet, but having given Ubuntu a chance for little over three years, Arch should get the benefit of a doubt until it does something pants-on-head-crazy at least ;)

One thing (among a whole host) I missed in Arch, which Ubuntu had done for me, was setting up dircolors so that when running the ls command some types of files (determined by their extension) would be colorized differently.

For instance, I quite liked that in Ubuntu, whenever I executed an ls in a directory containing tarballs, they’d all show up with a bright red color instead of the usual bland green.

It made it easier to locate them, which, in theory, would save me time.

So I fired up Firefox and asked Google for

archlinux colorize images and archives in ls

What Google returned was (among others) these two forum-posts:

The first didn’t seem all that useful to me, which was why I went to the other post and found almost all I was looking for. The ~/.dir_colors in the second post didn’t have any TERM line for rxvt, but the first post did, so with a little copying and pasting voilà! Colors! Colors everwhere!

So what you want to be doing is:
a. edit your ~/.bashrc (or whatever shell you’re using) adding:

if [ -f ~/.dir_colors ]; then
    eval `dircolors ~/.dir_colors`

b. read the dir_color manpage to understand how the ~/.dir_colors file should be formatted and what the available color codes are. I.e.:

Attribute codes:
00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
Text color codes:
30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
Background color codes:
40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

For instance, would you want files of a certain extension to show up as blinking red text on a black background use: 05;31;40
(you probably don’t want do use that specific combination for anything ;D)

c. go crazy with colors in your ~/.dir_colors, e.g.:

# archives
.tar 01;31
.tgz 01;31
.tar.gz 01;31

# images
.jpg 01;35
.jpeg 01;35
.gif 01;35
.png 01;35


That’s all there is to it really. Changes won’t appear in any existing terminal until you source the .bashrc file but all new terminals will behave “correctly” from the start.

And in closing, a completely unrelated little Arch tip:

If you are trying to get k3b or k9copy to work and it just frakking won’t, you are most likely not part of the group “optical”.

# usermod -a -G optical YourUsernameHere

fixes that (the user added to the optical group would need to log out and in again for changes to take effect).


Easy permission sanitizing using chmod

Thursday, June 3rd, 2010

Let’s say you have a web app, such as WordPress, and you have installed it on your own server. You are of course security conscious, so you wish to have the permissions set up correctly, no exceptions. this usually means 755 (rwxr-xr-x) for directories and 644 (rw-r–r–) for files.

The way I used to solve this, on every server I worked, I set up a small shell script ( along the lines of:

find $TARGET -type f | xargs chmod 0644
find $TARGET -type d | xargs chmod 0755

This worked well, with one huge caveat: What if you, somewhere in that directory structure had a file which needed to be executable?

I don’t know if such a case exists in WordPress, I’ve used that script on a couple of WP installations without any noticeable side-effects, but it’s obviously a flawed approach.

I’ll side-track this post a bit, since it is relevant to the overall post, that I, through, stumbled upon this blog post (which is awesome by the way, go read it!) about why LaTeX is so cool, and why it can be useful writing your résumé using it.

Just by chance I continued into Dan’s code section, and long story short, I found some cool stuff in his .bashrc file. Most notably this little beauty:

# sanitize - set file/directory owner and permissions to normal values (644/755)
# Usage: sanitize <file>
sanitize() {
	chmod -R u=rwX,go=rX "$@"
	chown -R ${USER}.users "$@"

I personally, for some reason, have always tended more to the octal representation than the [ugo][+-=][rwx] syntax, but that single chmod line is so outstandingly brilliant that I am almost forced to switch.

In one fell swoop Dan’s command does what I need two commands (really, with the xargs and I suppose one new process per found file/directory to execute chmod, my script needs a lot of processes) to accomplish.

The magic happens in that capital X, which is defined in the chmod man-file as: “execute/search only  if  the file is a directory or already has execute permission for some user”.

Directories automatically receives the executable flag, and any file which already has it, maintains it. Bloody brilliant!

Many thanks to Dan for sharing his configuration files, one of these days I’ll have to follow his good example.

Galactifying my desktop

Saturday, May 16th, 2009

I stumbled across a blog post today which described how to “insert” a terminal onto your Compiz desktop. Naturally I became all giddy over the idea since all these tv-shows, which present computers where the desktop is alive with various kinds of fictional data which flow by, has totally corrupted me, so I got to work setting it up per the instructions.

My previous wallpaper (white and black, and various shades of gray) didn’t make font-color selection easy, which ended up with me replacing the old wallpaper, with the beautiful Tricia Helfer (wallpaper).

Which got me thinking further, that I could do something funny with this terminal, and since my desktop was already taking on a strong feel of Battlestar Galactica (well, number six at least) I could have the terminal reinforce that feeling.

So, terminal font-color: red. Then I had another thought. Inside the .bashrc configuration file there is a small snippet of code which sets the PS1 variable to various things. Pontus has modified his, and helped me modify my, PS1 variable so that each terminal prompt now reads:

[username] [pwd]

Therefore it shouldn’t be that hard for me to modify it to look for this particular terminal-instance and output an altogether different prompt, say along the lines of:

By your command

This turned out to be a rather hard nut to crack actually. I figured I could write a shell script which would launch the terminal, with a correct set of parameters, and one of these parameters, in turn, would be another shell script to execute upon creation of the terminal window. The first shell script could then be called upon by the session manager at login.

That was the theory anyway. Actually getting it to perform all these things was less than easy. The shell script to be executed by the terminal wouldn’t conform (although this might have been my own ignorance putting a stop to it).

I won’t drag on about all the various ways I tried to fix this, in the end Pontus arrived and saved the day ;D

His solution: create a clone of the .bashrc file, change the PS1 variable modification in it to whatever I wanted (“By your command”), then have the session manager execute the following command:

gnome-terminal --window-with-profile=trans -x bash --rcfile ~/.nrsixrc

(where .nrsixrc is the cloned .bashrc file, containing the modified PS1 variable)

For interested parties, the interesting line in .nrsixrc is this one:

PS1='\nBy your command\n> '

And here’s the result:

by_your_command1 Oi! Stop ogling the b00bies and focus your attention at the upper left corner! I don’t know about you, but I find it pretty neat.

The one thing I have a hard time figuring out however, is why the terminal won’t either die, or recover, if I by accident forget myself and try to close that terminal. It will just “hang” and my two options at that time is to either use xkill (which coincidentally will also kill the terminal with which I initialize xkill) or do a killall gnome-terminal. None of these alternatives are especially attractive, and I can for the live of me not figure out why that is. But for the time being it will do just fine.