Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, November 29, 2008

Funny UNIX tricks from Slashdot

There was a recent story on slashdot about useless (or useful) things one can do in UNIX. Being a command line junkie, I read through virtually every comment (all 2300+ of them) to learn some new tricks. Here are some of the better ones:

  • Bash History
    • history -c # clear history (good for preserving privacy/passwords, or check out the more precise -d option)
    • In vi command mode, type /query and hit Enter to search history, n to keep searching backwards, N to search forwards
  • vimdiff
    • vimdiff original_file patched_file
    • unified format: open original file, then :vertical diffpatch path/to/diff
  • Encryption
    • openssl aes-256-cbc -a -e -salt -in INPUT_FILENAME -out OUTPUT_FILENAME # encrypt
    • openssl aes-256-cbc -a -d -salt -in INPUT_FILENAME -out OUTPUT_FILENAME # decrypt
    • echo Oe lbh pna vzcyrzrag UK tbireazrag fgnaqneq rapelcgvba jvgu ge | tr a-z n-za-m # Rot 13 encrypt/decrypt
    Others:
    • sleep 8h; cat /dev/urandom > /dev/dsp # alarm clock
    • eject -T # close cd tray if open, open if closed (useful to find out which physical machine you are logged into)
    • sl # punish users who accidentally type 'sl' instead of 'ls'
    • eposd && say 'hello' # make the computer talk
    • :(){ :|:& };: # forkbomb (space required between { and :) (protect against this with ulimit -u)
    • for I in $(seq 1 100) ; do echo $I; sleep .25; done | dialog --gauge "PIZZA" 6 50 100 # Pizza timer via dialog

    Friday, April 06, 2007

    My .bashrc file

    export TERM="xterm-color" # xterm terminal emulation (with color -- be sure to put at top of file on Ubuntu)
    export PYTHONSTARTUP="$HOME/.pythonrc.py" # python startup file
    set -o vi # sets vi command-line editing mode
    bind -m vi-command -r 'v' # so that every time you hit v in command mode, an editor doesn't launch
    ulimit -c unlimited # dump core files, no matter how big they are
    export EDITOR="vi" #use vi as the default editor for some commands, like "fc"

    alias screen='TERM=screen screen' # workaround for screen backspace bug

    alias d="date '+%r -- %A, %D'" # human-readable date output

    alias ll="ls -lh" # long listing (with human-readable file sizes)
    alias lr="ls -ltrh" # list by reverse modification time
    alias la="ls -Ah" # list hidden files (except implied . and ..)

    alias <name>="cd <important>" # get to important directories fast
    alias <name>="ssh <login>@<important>" # log into oft-used remote machines fast

    # prevent myself from doing stupid things with vi
    function vi {
    if [ ! -e "$1" ]; then
    if [ "$1" == "" ]; then
    vim
    else
    vim "$1"
    fi
    elif [ -d "$1" ]; then
    cd "$1"
    elif [[ -c "$1" || -b "$1" || -p "$1" || -S "$1" || ! -r "$1" ]]; then
    file "$1"
    else
    vim "$1"
    fi
    }

    The following I only enable on Solaris systems:
    export PAGER="less" #uses less to view manpages instead of more

    Links:

    Wednesday, April 04, 2007

    Common Bash Tasks

    I don't use Bash for much scripting. Usually, if I have to open up a file to write a script, it will be done in Python. However, the less I have to do that for mundane tasks that can be accomplished on the command line with Bash, the better. This entry may not have much in it now, but every time I do something cool/useful/time-saving in Bash from now on, it'll go on here.

    Common Tasks

    • Rename *.foo files to *.bar files: for i in *.foo; do mv $i ${i%.foo}.bar; done (BashFAQ)
    • Rename foo.* files to bar.* files: for i in foo.*; do mv $i bar.${i#foo.}; done (also check out the perl regex-based rename Linux utility)
    • Do something to multiple arguments: for i in arg1 arg2 arg3; do echo $i; done
    • Print 0 through 9 on separate lines: for i in {0..9}; do echo $i; done
    • Flatten output onto one line: <Multi-line output> | xargs (10 habits)
    Links

    Tuesday, December 05, 2006

    Bash prompt customization

    Using old UNIX machines is a pain sometimes. The 10-year-old features just don't mesh with the current ones, and it just doesn't feel right. Also, the prompt might not display any useful information. Heresy, I say! Follow these steps to make yourself feel more at home. Some information taken from this article.

    To get an Ubuntu-like prompt (assuming bash is installed):
    1. Edit the .bashrc file with an editor like vi
    2. Add this as the last line: export PS1='\u@\h:\w$ '
    Other prompt configuration options:
    • \! History number of current command
    • \# Command number of current command
    • \d Current date
    • \h Host name
    • \n Newline
    • \s Shell name
    • \t Current time
    • \u User name
    • \W Current working directory
    • \w Current working directory (full path)
    To get easy access to some obscure directory: alias [name]="cd [absolute directory path]"

    You can also edit the .login file, which executes immediately when you log in. (It might go by a different name depending on the shell... see here for details)

    Use set -o emacs (the default) or set -o vi to set your command line editing mode of choice.

    And remember kids, don't forget to write the other users if on a public machine! (Or maybe talk, or wall if you're the admin).

    More links

    Sunday, September 24, 2006

    Linux Command Line Odds and Ends

    Here are some useful Odds and Ends... most are related to command line stuff, some not; whatever, enjoy. Most of these came from either scouring the web, or Learning the Bash Shell or Learning Red Hat Enterprise Linux and Fedora.

    Job Control
    • kill %<PID> kill a process
    • kill -QUIT %<PID> kill a process, a bit stronger
    • kill -KILL %<PID> unconditionally kill a process
    • fg bring a background job into the foreground
    • jobs list jobs running
    • ps process information

    File permissions (owner, group, others)
    • 0 ---
    • 1 --x
    • 2 -w-
    • 3 -wx
    • 4 r--
    • 5 r-x
    • 6 rw-
    • 7 rwx

    Globbing
    • * matches zero or more characters
    • ? matches any one character
    • [abc...] matches any of the characters specified
    • [a-z] matches any character in the specified range
    • [!abc...] matches any character other than those specified
    • [!a-z] matches any character not in the specified range
    • ~ home directory of current user
    • ~userid home directory of a user
    • ~+ current working directory
    • ~- previous working directory

    Quotes
    • 'xxx' interprereted literally, variables not substituted
    • "xxx" interprereted literally, variables ARE substituted
    • `xxx` output of xxx command replaces it


    Command line special characters
    • # comment
    • ; command seperator
    • & run in background
    • \ command continued on next line
    • | pipe

    Input/Output Redirectors
    • prog > file stdout to file
    • prog 2> file stderr to file
    • prog >> file concatenates stdout to file
    • prog 2>> file concatenates stderr to file
    • prog > file 2>&1 stdout and stderr to file
    • prog >> file 2>&1 concatenates stdout and stderr to file
    • prog < file stdin from file
    • prog << text reads stdin until a line matching text is found, then EOF posted ("here document")
    • prog | prog2 pipe stdout
    • prog 2>&1 | prog2 pipe stdout and stderr

    Command Line Movement
    • Ctrl+Shift+N open new console window
    • Crtl+Alt+F[1-7] go to virtual console 1-6 or X(7)
    • Ctrl+Alt+Backspace stop X and go to console
    • Alt+B Back one word
    • Alt+F Forward one word
    • Ctrl+A Beginning of line
    • Ctrl+E End of line
    • Alt+D Delete word |------------->X
    • Ctrl+D Delete char
    • Ctrl+K Delete |------------->X
    • Ctrl+U Delete X<-----------|
    • Ctrl+L Clear screen
    • Ctrl+Y UNDO
    • ESC+. Insert last word of previous command
    • TAB Possible completions

    IRC
    • /server join a server
    • /join join a channel
    • /quit quit the server
    • /close close the current screen
    • /part leave the current channel
    • /partall leave all channels
    • /msg msg a user with a new window
    • /notice msg a user without a new window
    • /query force a window open to msg a user
    • /chat DCC with a user
    • /dns dns lookup for a user
    • /ping ping a user
    • /me *** does something
    • /whois query whois for a user

    Ctrl Keys
    • Ctrl+C intr: stop current command
    • Ctrl+D eof: end of input
    • Ctrl+\ quit: stop current command (if Ctrl+C doesn't work)
    • Ctrl+S stop: halt output to screen
    • Ctrl+Q resume output to screen
    • Ctrl+Z suspend current command (works well with bg, fg and jobs)

    Escape Sequences
    • \a alert (bell)
    • \b backspace
    • \c omit final newline
    • \E escape character
    • \f formfeed
    • \n newline
    • \r return
    • \t tab
    • \v vertical tab
    • \xxx ASCII in octal
    • \\ backslash
    Tar: tarball options
    • -c create
    • -r append
    • -t list contents
    • -x extract
    • -a append files
    • -v verbose
    • -z zip/unzip
    • -f use filename
    • Oft-used:
      • tar -cf foo.tar foo; gzip foo.tar
      • gunzip bar.tar.gz; tar -xvf bar.tar