*nix

Commands and Utilities

  • OSD_CAT: X-Onscreen file displayer
  • hexdump: converts binary to hex
  • xlsfonts: shows all the fonts installed
  • df -h: disk usage
  • cut, grep, awk, sed : string manipulations.
  • notify-send: Shows popup.
  • lynx – command line browser
  • ` (back-tick): executes the command inside it and gives the output of the command.
  • apropos, which , locate, whereis, whatis – locate commands.
  • Expect Scripting – Automate interactive processes. Link: http://www.thegeekstuff.com/2010/10/expect-examples/
  • watch – watch logs or output from netstat
  • mutt -s “subject” -a “attachment1” -a “attachment2” “email” < file.txt

Unix Environment

A working environment is setup at below locations.

  • /etc/profile – defaults for all the users – Unix first looks here. This is not edited generally.
  • ~/.bash_profile, ~/.bash_login, ~/.profile, ~/.login – On Login Shell, if it finds one of the files then executes that file and ignores the rest.
  • ~/.bashrc  –  executes when a new sub shell is started. rc indicates resource.
  • ~/.bash_logout – executes when logging out

In order to have the same login shell and bash shell, put everything in the .bashrc and source it inside the .bash_profile like below:

if  [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

Inside .bashrc:

Put aliases like alias ll='ls -laGh'
Put environment variables like JAVA_HOME='/opt/jdk/java'. This will only make it available for current session. To make it available for all the programs and sessions, this environment variable must be exported like: export JAVA_HOME.

PATH Variable

Unix looks for commands in the following order and if it finds, it ignores the looking up rest of the locations:
/usr/bin:/bin:/usr/sbin:usr/local/bin:/usr/X11/bin.
This can be changed if we edit or add to the PATH environment variable.
export PATH="new_path:$PATH" – Ensure to put double quotes so that $PATH gets expanded. Don’t put single quotes because $PATH will be interpreted as directory.
Generally, export is used to set environment variable for history for either formatting or some settings.

export HISTSIZE=10000
export HISTCONTROL=ignoreboth --> ignores duplicates
export HISTTIMEFORMAT='%b %d %I:%M %p '
export HISTIGNORE="history:pwd:exit:df:ls -la" --> doesn't records these commands in history

UNIX Power Tools

grep: Global Regular Expression Print

grep [options] 'regular_expression' file.txt returns each line matching the characters regular_expression
Useful Options:

-i : ignore case
-w: matches only whole word and not the sequence of characters. It finds word breaks by looking at spaces, punctuations and line breaks.
-v: displays every line except which contains the sequence we are looking for. In this case it matches every line that doesn't contain the sequence regular_expression.
-n: prints line numbers as well
-c: prints only the number of matches
-R: check all the files inside the directories and display the results with file names.
-Rh: it suppresses the file names and displays only the results.
-Rl: displays only the file names and suppresses the results.
-L: displays only the file names which doesn't match the sequence we are looking for. Similar to -v option. -v option returns lines that don't match, -L option returns files that don't match.
--color=auto : colors the matches
-E: use extended regular expressions

Instead of using the -E option, you can use egrep.

tr: Translate

Searches and replaces characters in a string at their positions.
echo 'a,b,c' | tr ',' '-' outputs a-b-c
tr 'A-Z' 'a-z' < file.txt makes the characters lower case.
tr ',' '\t' tabbedfile.txt
tr -s ' ' file2 – squeezes all the spaces to single space.
Options:
-d: delete the characters instead of translating
-s: squeeze the repeats in the set
-c: complements the set


Sed: Stream editor

sed 's/pattern/replacement_string' file.txt – Searches a pattern and substitutes it with the replacement string on each line only once
sed 's/pattern/replacement_string/g' – Searches a pattern and substitutes it with the replacement string on each line globally


Cut

cut option positions [ -d option ] filename.txt: Cut selected portions of files. You can cut characters, bytes and fields specifying it in the options. positions can be comma separated and contain a range delimiter.

diff

diff file1 file2 shows deletes (d), changes (c) and appends(a) among two files.
: occurred in the right file.
3d1 : it detected deletion on line 3 in first file and line 1 on second file.
7c4 : it detected a change on line 6 in first file and line 4 in second file.
Options:
-i : case insensitive
-b : ignores blank characters
-w : ignores all white spaces
-B : ignore blank files
-r: recursively compares files in the directories.
-s : tells if the files are identical.
-c : output format of diff
-u : shows the changes in an intuitive way
-q : tells if the files are different

diffstat: shows the summary of diff.

xargs

pass the arguments to the command. The default command is echo

Options:
-t displays the command with arguments.
-n [1-9] loops through 1 to 9 arguments where arguments are words
-L [1-9] loops through 1 to 9 arguments where arguments are lines
-I {}: place holder for arguments to this option.
-0 : don’t ignore spaces. Used mainly when working with spaces in file names. Especially used when using find.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s