Commands by ozymandias (2)

  • In my work environment, we log onto the servers as our user ('user', in the sample ouput), and 'sudo su - root' to other accounts. This trick allows us to return the account name we logged in as -- and not the account name we currently are ('root', in this example). Using this trick, you can build other commands: Set your CVSROOT env variable to your account name: CVSROOT=$(who am i | awk '{print $1}') SCP a file to another server: scp file.txt $(who am i | awk '{print $1}') This works out great in my environment, as we can include this in our documentation and make the comands more easy to copy/paste for different users, and not have to set all sorts of variables, or modify the docs for each user. whoami gives you the name of the user you currently are, not the user you logged on originally as. who gives you a listing of every single person logged onto the server. who am i gives you the name of the user you logged on as, and not who you changed to with su. Look at the following scenario: whoami user su - # whoami root # who am i user pts/51 2009-02-13 10:24 (:0.0) whoami != who am i Show Sample Output

    who am i
    ozymandias · 2009-02-20 16:26:11 9
  • The expansion {,} in bash will repeat the given string once for each item seperated by commas. The given command will result in the following being run: cp /really/long/path/and/file/name /really/long/path/and/file/name-`date -I` These can be embedded as needed, ex: rm file{1,2,3{1,2,3}} would delete the files file1, file2, file31, file32, file32, and no other files.

    cp /really/long/path/and/file/name{,-`date -I`}
    ozymandias · 2009-02-18 20:35:47 1

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

Remove spaces from filenames - through a whole directory tree.
Sometimes, you don't want to just replace the spaces in the current folder, but through the whole folder tree - such as your whole music collection, perhaps. Or maybe you want to do some other renaming operation throughout a tree - this command's useful for that, too. To rename stuff through a whole directory tree, you might expect this to work: for a in `find . -name '* *'`;do mv -i "$a" ${a// /_};done No such luck. The "for" command will split its parameters on spaces unless the spaces are escaped, so given a file "foo bar", the above would not try to move the file "foo bar" to "foo_bar" but rather the file "foo" to "foo", and the file "bar" to "bar". Instead, find's -execdir and -depth arguments need to be used, to set a variable to the filename, and rename files within the directory before we rename the directory. It has to be -execdir and won't work with just -exec - that would try to rename "foo bar/baz quux" to "foo_bar/baz_quux" in one step, rather than going into "foo bar/", changing "baz quux" to "baz_quux", then stepping out and changing "foo bar/" into "foo_bar/". To rename just files, or just directories, you can put "-type f" or "-type d" after the "-depth" param. You could probably safely replace the "mv" part of the line with a "rename" command, like rename 'y/ /_/' *, but I haven't tried, since that's way less portable.

system beep off

back ssh from firewalled hosts
host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.

Find the package that installed a command

Rename .JPG to .jpg recursively
This command is useful for renaming a clipart, pic gallery or your photo collection. It will only change the big caps to small ones (on the extension).

Generate the CPU utilization report
Generated the CPU utilization stats with 5 lines /every 2 seconds. Needs sysstat package to be installed prior to use sar.

display a smiling smiley if the command succeeded and a sad smiley if the command failed
you could save the code between if and fi to a shell script named with the first argument as and then do a to see if the command succeeded. a bit needless but who cares ;)

start a VNC server for another user

What is my ip?

find previously entered commands (requires configuring .inputrc)
[Click the "show sample output" link to see how to use this keystroke.]   Meta-p is one of my all time most used and most loved features of working at the command line. It's also one that surprisingly few people know about. To use it with bash (actually in any readline application), you'll need to add a couple lines to your .inputrc then have bash reread the .inputrc using the bind command:   $ echo '"\en": history-search-forward' >> ~/.inputrc   $ echo '"\ep": history-search-backward' >> ~/.inputrc   $ bind -f ~/.inputrc     I first learned about this feature in tcsh. When I switched over to bash about fifteen years ago, I had assumed I'd prefer ^R to search in reverse. Intuitively ^R seemed better since you could search for an argument instead of a command. I think that, like using a microkernel for the Hurd, it sounded so obviously right fifteen years ago, but that was only because the older way had benefits we hadn't known about.     I think many of you who use the command line as much as I do know that we can just be thinking about what results we want and our fingers will start typing the commands needed. I assume it's some sort of parallel processing going on with the linguistic part of the brain. Unfortunately, that parallelism doesn't seem to work (at least for me) with searching the history. I realize I can save myself typing using the history shortly after my fingers have already started "speaking". But, when I hit ^R in Bash, everything I've already typed gets ignored and I have to stop and think again about what I was doing. It's a small bump in the road but it can be annoying, especially for long-time command line users. Usually M-p is exactly what I need to save myself time and trouble.     If you use the command line a lot, please give Meta-p a try. You may be surprised how it frees your brain to process more smoothly in parallel. (Or maybe it won't. Post here and let me know either way. ☺)

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: