Tuesday 8 October 2013

My UNIX Cheat Sheet

Our SAP BW and also some ERP systems sit on Unix platform. Occasionally I have to work with files directly on these and this is my cheatsheet for using Unix.

Commands:

dir and ls
dir gives detailed directory list. ls just lists the files / folders.

cd (change directory)
folder delimiter in Unix is "/" not "\" as with windows.

cd ../ returns one folder up.
cd /xxx/xxx will go to that folder.  If start with the / thats the root directory (I think)
cd xxx will move down tree to folder xxx from current directory (if exists).

mv (move)
moves files and/or renames them.
mv [src] [target]
src is the source file, or wildcard.  eg. * is all in current directory, *.csv is all csv folders
target

rm (remove)
removes files.
rm [tgt]
removes the tgt file(s).  Will be in current directory unless otherwise specified.

gzip / gunzip
gzip / qunzip [tgt]  will zip or unzip the tgt.  tgt can be wildcard.
Note sure if can put multiple files into same .gz file.  Presume so, but not sure syntax.

chmod (security)
files / folders have various security levels in Unix.  chmod allows you (if can) to modify the settings for file(s).
eg. chmod 666 [tgt] will give read / write access to the file.

head and tail (show top / bottom lines of file)
head [filename]  will show first 10 lines of file.
head -n 20 [filename].  will show first 20 lines of file.
tail will do from bottom.

sed (  (s)tream (ed)itor - special editor for modifying files)
Lots of sub commands: this page looks useful.
http://www.grymoire.com/Unix/Sed.html

deleteing with sed.
to simply delete a particular line use sed 2d where 2 is the line to delete.

od (octal data)
Display's filestring in octal characters.  Useful to see if have non-printable characters in a file.
eg. "od -b xxx.csv | more"
Came in handy when looking at Qlikview generated csv files, prepends non-readable characters which was throwing out reads.