Commands by greggster (7)

  • Use vim's diff mode to edit two or more files in one window. The '+diffoff!' turns off diff highlighting when the session is started. Use ctrl+w + ctrl+w to switch between windows. Show Sample Output


    2
    vim -d '+diffoff!' file1 file2
    greggster · 2012-08-30 07:51:41 6
  • Sort lines within vi editor. In this example sort lines 33-61 and lines 4-9 asciibetically. Show Sample Output


    6
    :33,61 !sort
    greggster · 2011-05-06 06:10:05 7
  • rm -rf .* matches ".." and thus one goes up a level and wipes out more than intended. In bash, .??* safely accomplishes what one intends - remove those .files The ? matches most characters except "/", thus .?? does not match ../ and so one is safe. Show Sample Output


    0
    rm -rf .??*
    greggster · 2011-03-11 07:21:58 8
  • When setting up a new aliases file, or having creating a new file.. About every time after editing an aliases file, I source it. This alias makes editing alias a bit easier and they are useful right away. Note if the source failed, it will not echo "aliases sourced". Sub in vi for your favorite editor, or alter for ksh, sh, etc.


    7
    alias va='vi ~/.aliases; source ~/.aliases && echo "aliases sourced"'
    greggster · 2011-03-10 06:41:37 5
  • This is for bash - make an alias - also a good blueprint for making aliases that take arguments to functions. If for Solaris use "-size +${1}000000c" to replace "-size +${1}M" Show Sample Output


    0
    alias big='BIG () { find . -size +${1}M -ls; }; BIG $1'
    greggster · 2011-03-10 06:33:00 6
  • until (ssh root@10.1.1.39 2> /dev/null); do date; sleep 15; done In this case will execute "date" then "sleep 15" until we are able to ssh into server, such as after a reboot Could also be like: until ( ping 10.1.1.39 1> /dev/null); do echo "server 10.1.1.39 is down"; sleep 15; done Show Sample Output


    8
    until (ssh root@10.1.1.39 2> /dev/null); do date; sleep 15; done
    greggster · 2011-03-08 08:42:12 7
  • This is useful for quickly jumping around branches in a file system, or operating on a parellel file. This is tested in bash. cd to (substitute in PWD, a for b) where PWD is the bash environmental variable for the "working directory" Show Sample Output


    7
    cd ${PWD/a/b}
    greggster · 2011-03-03 06:27:12 10

What's this?

commandlinefu.com 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

quickly change all .html extensions on files in folder to .htm

Block known dirty hosts from reaching your machine
Blacklisted is a compiled list of all known dirty hosts (botnets, spammers, bruteforcers, etc.) which is updated on an hourly basis. This command will get the list and create the rules for you, if you want them automatically blocked, append |sh to the end of the command line. It's a more practical solution to block all and allow in specifics however, there are many who don't or can't do this which is where this script will come in handy. For those using ipfw, a quick fix would be {print "add deny ip from "$1" to any}. Posted in the sample output are the top two entries. Be advised the blacklisted file itself filters out RFC1918 addresses (10.x.x.x, 172.16-31.x.x, 192.168.x.x) however, it is advisable you check/parse the list before you implement the rules

Annotate tail -f with timestamps

Convert CSV to JSON
Replace 'csv_file.csv' with your filename.

Convert files from DOS line endings to UNIX line endings
Here "^M" is NOT "SHIFT+6" and "M". Type CTRL+V+M to get it instead. Its shortest and easy. And its sed!, which is available by default in all linux flavours.. no need to install extra tools like fromdos.

get a desktop notification from the terminal
tired of switching to the console to check if some command has finished yet? if notify-send does not work on your box try this one... e.g. rsync -av -e /usr/bin/lsh $HOME slowconnection.bar:/mnt/backup ; z (now fire up X, do something useful, get notified if this stuff has finished).

prevent large files from being cached in memory (backups!)
We all know... $ nice -n19 for low CPU priority.   $ ionice -c3 for low I/O priority.   nocache can be useful in related scenarios, when we operate on very large files just a single time, e.g. a backup job. It advises the kernel that no caching is required for the involved files, so our current file cache is not erased, potentially decreasing performance on other, more typical file I/O, e.g. on a desktop.   http://askubuntu.com/questions/122857 https://github.com/Feh/nocache http://packages.debian.org/search?keywords=nocache http://packages.ubuntu.com/search?keywords=nocache   To undo caching of a single file in hindsight, you can do $ cachedel   To check the cache status of a file, do $ cachestats

Quick and Temporary Named Commands
* Add comment with # in your command * Later you can search that command on that comment with CTRL+R In the title command, you could search it later by invoking the command search tool by first typing CTRL+R and then typing "revert"

Reverse ssh
Both hosts must be running ssh and also the outside host must have a port forwarded to port 22.

Find the package that installed a command


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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: