unix linux bash

chown

SOFTWARE: unix linux bash

chown joe myfile.txt

change ownership of files and directories.

MORE INFO:

untar and tar files

SOFTWARE: unix linux bash

tar -zxvf myfile.tar.gz

To Untar (decompress)

Use to uncompress a tar file on the command line. Should also work for tar files ending in slightly different suffixes: .tgz, .tar.gz etc.

tar -zxvf myfile.tar.gz

To Untar files with .bz2 suffix

Modify the flags to be like:

tar -xvjf myfile.tar.bz2

Note the difference between the two commands above is that the first uses the -z flag for gzip, whereas the second uses the -j flag for bzip2.

Get more detail about the available flags to use with the tar command with:

tar --help

To Tar (compress)

There are a number of options. Here's an example to tar up two files in your current directory into an archive called "myarchive.tar"

tar -cf myarchive.tar one_file another_file

You can also use the "z" flag to use zip/gzip, and the "v" flag to get verbose output. To zip a directory, you just target the directory like you would for a file and it'll go recursive by default:

tar -cvzf myarchive.tgz directory_to_tar
MORE INFO:

scp

SOFTWARE: unix linux bash

scp -r servername:/remotedir /localdir/localsubdir

Copy directory across boxes using scp. You can use . to signify "here" (this directory) when designating where to copy files to in the second argument if you're already in the location you want to copy your files to.

scp -r servername:/remotedir /localdir/localsubdir

The example above assumes your copying from a remote machine to your local machine. If you want to do it the other way around, just flip the first and second arguments like:

scp /path_to_file/filename servername:/path_to_destination_dir/

Also, to add the remote user that you want to connect as on the remote server, use username@servername:/path/filename:

scp /path_to_file/filename username@servername:/path_to_destination_dir/

RSYNC

Syntax is:

rsync options from to

so for example to get a folder from another server and update your local folder:

rsync -zvr another_server:/folder/folder/folder_to_get/ /folder/folder/local_folder_to_overwrite/

Note trailing slashes on folders. 

Options are:

-z compress during transmission
-v verbose output
-r recursive

For simplicity just use the -a flag. Works great.

ssh agent

SOFTWARE: unix linux bash

ssh-agent bash (hit enter) ssh-add (hit enter)

use to add auth to your shell so you can automatically access a number of boxes. It's two part, so run "ssh-agent bash" first, then "ssh-add".

MORE INFO:

chmod

SOFTWARE: unix linux bash

chmod 755 myfile.txt

To change permissions in Unix, use "chmod".

Number Method

It's tricky to remember which numbers to use, so find out quickly with the online calculator. Then, the format to use is:

chmod permissions target

Example:

chmod 755 myfile.txt

Or for a directory, use recursive flag (-R) to catch all contents:

chmod -R 755 myfiles

Regular Method

For "who" will get the permissions you've got (u)ser, (g)roup, and (o)thers. And the available permissions are: (r)ead, (w)rite, and e(x)ecute.

The format is

chmod user operator permissions target

Example to add write permission for user:

chmod u+w myfile.txt

Or to set all users to no permissions:

chmod a-rwx myfile.txt

Which could also be set with:

chmod a= myfile.txt

Comma separate different user operator permission sets:

chmod u+w,go-w myfile.txt
MORE INFO: http://en.wikipedia.org/wiki/Chmod

tail

SOFTWARE: unix linux bash

tail -f production.log

View just the end of a file. Useful for watching the results of actions scroll by in logs.

Get n number of last lines (ie 40):

tail -40 logname.log
MORE INFO: http://en.wikipedia.org/wiki/Tail_(Unix)

reverse search command in shell

SOFTWARE: unix linux bash

ctrl_key + r + [start typing command to recall ...]

Use this command sequence to search through recently used commands in the shell. Faster than using the up and down arrows to scroll through recent commands if you know what your command you want to recall started with.

-source: A. Limi.

MORE INFO:

logs

SOFTWARE: unix linux bash

Where

Access logs by checking out the /var/log directory on many nix systems.

Scanning

  • Nifty to vi them, then use the slash "/" search operator in vi to find something quickly.
  • Use tail -f to watch the end of the log file interactively while working.

Type this (replace with whatever your log is called) in the shell and leave shell open while you hit pages to see what's goin' on:

tail -f development.log
MORE INFO:

rsync at site5 hosting

SOFTWARE: unix linux bash

rsync -avz -e ssh username@www.yourdomain.com:/home/username/remotedir /path/to/localdir

An example of using rsync (through ssh tunnel) to download an archive (-avz settings) of remote_dir which lives in your /home/username directory at site5:

rsync -avz -e ssh username@www.yourdomain.com:/home/username/remote_dir /path/to/local_dir

Could be good in conjunction with this cool backup shell script:

backup script

Reference URL for this cheat is a good tutorial for using rsync.

Here's an entire workflow for gettting live stuff not only backed up, but also mirrored onto your dev box automatically. Kind of a reverse staging->production thing ...

migration to site5

MORE INFO: http://everythinglinux.org/rsync/

handy vi commands

SOFTWARE: unix linux bash

:$ end of file, :0 top of file

A few handy things in the vi text editor:

  • colon + 0 - top of file
  • colon + $ - end of file
  • pg dwn key - page down etc.
  • / + term - search for term
  • esc + : + q - quit w/out writing changes
  • esc + : + w + q - quit and write changes
  • a - alter
  • x - delete character
  • dd - delete a line
  • u - undo
  • yy - yank (copy) a line
  • 17yy - yank 17 lines
  • p - paste the yanked lines
MORE INFO: http://www.worldtimzone.com/res/vi.html

search directory with grep

SOFTWARE: unix linux bash

grep -r -i termtofind pathtodirectory

When grepping through an entire directory, use the -r (recursive) flag to search the whole thing.

grep -r -i term_to_find path_to_directory

As always, the . for "here" can be nice, as in:

grep -r -i term_to_find .
MORE INFO:

clear terminal screen

SOFTWARE: unix linux bash

clear

Just type clear and hit return to clear the terminal window. Quick way to get a nice clean window in which to work if it gets cluttered.

MORE INFO:

SSH RSA Keys

SOFTWARE: unix linux bash

generate keypair > trxfr public key (idrsa.pub) to remote "authorizedkeys" file

This is a note on the "big picture" with keys. IE visualizing what's actually going on.

Details on the actual commands to use are available at:

Using rsync and SSH

The Big Picture

There are three main files that live under .ssh/ They are:

  • The private key (id_rsa) is for your local machine
  • The public key (id_rsa.pub) is information for your remote server (placed in .ssh/authorized_keys on remote)
  • The known_hosts file (known_hosts) is for your local machine. Don't worry about that one when setting up keypairs. It gets populated automatically as you actually use this stuff.

So the basic steps are:

  1. Create a keypair on your local machine. At this point, you pick your passphrase.
  2. This gives you a local "secret" key (".ssh/id_rsa" by default).
  3. Also gives you a public key (".ssh/id_rsa.pub" by default)
  4. Copy the contents of the public key to the file .ssh/authorized_keys on the remote server
  5. Now, when you connect via SSH, you'll be prompted for your passphrase (Cool, huh?).

Code

cd ~/.ssh
ssh-keygen -t rsa
>> enter name for key (IE id_rsa_myuse, or just take default: id_rsa)
>> enter passphrase

Other Notes

You can also authenticate via bash once with "ssh-agent bash", "ssh-add". This will load your auth into cache so you can ssh around without having to re-enter the passphrase each time.

I believe the "known_hosts" file under your local .ssh directory forms a sort of secure handshake between the two keys so that once you've been prompted to accept the connection once, it will not have to prompt you again to accept. If you removed an entry from this file for a given connection, it would give you the original prompt again asking if you want to trust.

If you already have a keypair on your local machine, you can simply add the id_rsa.pub file up to the remote .ssh/authorized_keys file. This way you'll be prompted for the same passphrase you normally use.

Non-password or non-default key path

If you want to have no passphrase required, you can create a new keypair. Give it a unique name when creating so you don't overwrite your main keys. Simply hit Enter when prompted for the passphrase for the keys when you make them. Less secure obviously. So use with caution only when you need to (IE with a Cron script that requires it.) In a case like this where you have multiple keys, you need to manually designate which key to use when connecting to a server via ssh, so do something like:

ssh -i /path/to/keyname -lusername servername

Keys with Site5

MORE INFO: http://troy.jdmz.net/rsync/index.html

Mail Commands

SOFTWARE: unix linux bash

type "mail" to access email in terminal

A quick cheatsheet of commands for unix mail:

  • mail access mail
  • & the prompt in mail
  • number of message IE (1 + enter, 3 + enter etc.) read selected message when in list view
  • space bar read more of message
  • h return to message list (at prompt)
  • r reply
  • R reply to all
  • d + message number(s) delete messages when in list view IE d 1 3 to delete message 1 and 3
  • q quit
  • ? inline help
MORE INFO: http://www.helpdesk.umd.edu/documentation/unix/mail.shtml#7

Mac OS X Tiger Cron

SOFTWARE: unix linux bash

sudo vi /etc/crontab

crontab -e to generate crontab for specific user wasn't getting kicked off by the daemon, so I just used the main crontab in /etc/crontab instead.

sudo vi /etc/crontab

Note that the format for cron jobs in the main crontab includes WHO as well like so:

minute hour mday month wday WHO command

So be sure to designate which user you want to run the command as well. IE to have a user kirk run the command 'doublekick.sh' at 9am:

0 9 * * * kirk /Users/kirk/scripts/doublkick.sh
MORE INFO:

disk space report

SOFTWARE: unix linux bash

df -h OR du -sh /path/to/dir (for just a directory)

The whole box

Check disk space on linux box.

df -h

the h flag prints the space in Megs and Gigs instead of K.

A specific directory

Use du to check a specific directory. The -s flag makes it a summary for the whole directory, and the -h flag makes it Megs and Gigs instead of K.

du -sh /path/to/directory

Get help and additional flag options with the usual:

du --help

df --help
MORE INFO:

shutdown or restart linux

SOFTWARE: unix linux bash

shutdown -r now

To restart (-r flag) right away (now), the basic command is:

shutdown -r now

Or, you can pass another time instead of now. For example, restart in two minutes:

shutdown -r 2

And, you can add a comment to flash to users:

shutdown -r 2 hey I'm going to restart  in two minutes! Save your work!

Actual shutdown instead of restart is the -h flag (halt)

shutdown -h now I'm gonna shutdown right now!
MORE INFO:

Encrypt dmg (Mac OSX)

SOFTWARE: unix linux bash

hdiutil create -encryption -stdinpass -srcfolder private encrypted.dmg

Lifehacker has a link to how to create an encrypted dmg. Also includes info on how to set up with disk utility.

At command line

To create an encrypted disk image with the name "encrypted.dmg" with contents from the source folder "private" in your current location:

hdiutil create -encryption -stdinpass -srcfolder private encrypted.dmg

Enter password at prompt.

Decrypt:

hdiutil attach -stdinpass encrypted.dmg

http://lifehacker.com/software/mac-tip/create-an-encrypted-disk-image-at-the-command-line-279005.php

With disk utility app

  1. Go > Utilities > Disk Utility
  2. New Image
  3. Pick where you want to save it
  4. Set the size
  5. Enable encryption
  6. Make format: read/write disk image
  7. Create

Once it's made, you can double click to open it, add files, modify etc. Just eject it to unmount. Subsequent times you click it to open, you'll be prompted for password.

MORE INFO: http://lifehacker.com/software/mac-tip/create-an-encrypted-disk-image-at-the-command-line-279005.php

telnet to test address and port

SOFTWARE: unix linux bash

telnet ip_address port

The ping command is cool for testing a connection to an ip address. If you need to test a specific port, tho, you can open a shell and type:

telnet ip_address_to_test port_to_test

IE

telnet 10.1.1.1 443

If you get a connection, it works. Escape with:

CTRL_KEY + ]

Quit with "quit"

Works in nix and PC shells.

MORE INFO:

install from source

SOFTWARE: unix linux bash

untar > cd to dir > ./configure > make > make install

Install from source

On mac os x, make sure you have developer tools installed. Using MacPorts or Fink is a lot easier since then you're downloading a package and patches and things you might need for mac os x are already taken care of. But when you need to go straight to source, here's the deal.

Locate the file on the net via a google search or whatever. Download the file with your browser. Or, if you want to be fancy pants and download via the shell, you can use software like curl to get it:

curl -o myfile.txt http://www.some_url.com/myfile.txt

"myfile.txt" is what the download will be called on your machine. You can just enter the actual name of the file you're grabbing to keep it simple.

Extract the file. On mac, you can just double click many compressed files to open. Or you can do it by hand depending on the compression type. For tar files use:

tar -zxvf myfile.tar.gz

Now, change directory into the location of the extracted software:

cd path-to-software

If you look in the directory with ls, you should see a file called "configure". So run:

./configure

Or, some builds will want you to supply arguments such as:

./configure --prefix=/usr/local

So add those if you need them. I'm a bit vague on this, but it would appear that if you want your software installed in /usr/local/bin most of the time, you should include the --prefix argument as above when you run ./configure. Get help about configure with:

./configure --help

If you're lucky you can find some documentation for the software you're installing. Anyway, once the build is configured we can move on to generating the make with "make"

make

And now we need to actually install with :

sudo make install

Precompiled

Some software downloads are available as "precompiled" for a given os. Installation methods may vary. You might be able to simply drop into the appropriate folder on your system and be done, or the software may have had "configure" run on it, but still need to have "sudo make install" run to actually install.

MORE INFO:

find

SOFTWARE: unix linux bash

find location -name "file_name"

To search in the terminal for files and things: use the find command and designate where you want to look, followed by -name and the filename in quotes. To search for a file called .bash_profile in your home directory (~):

find ~ -name ".bash_profile"

Or, if you want to find a name that contains certain characters, but you're not sure exactly what it's called, use the * operator:

find ~ -name ".bash*"

You can use this same idea to find a filetype based on its extension. For example to find all pdf files:

find ~ -name "*.pdf"

A bunch of other cool tricks with find are in the tutorial listed as the reference URL for this cheat.

Find xml files containing mystring:

$ find . -name '*.xml' -exec grep 'mystring' {} \;
MORE INFO: http://dmiessler.com/study/find/

suse yast rpm cheatnotes

SOFTWARE: unix linux bash

type "yast" in shell to access setup

A few notes on sysadmin of suse 10.x. Yast provides a bunch of admin options for your suse box. Alternatively, you can manage packages manually via the rpm command in the shell. Below are a few notes on both.

YAST

Type "yast" in the terminal to open up the "yet another setup tool" in suse linux. Good stuff because it provides a bunch of options via an ncurses interface and can be easier to use than direct rpm commands etc.

yast

When accessing via a mac and ssh, some of the commands for navigating the ncurses interface weren't working right (IE alt + H doesn't take.) So I just started using TAB which zips you around thru all available options on the screen.

TAB key to move around
ARROW KEYS also to move around in some interfaces
ENTER to select

Http Server info is in Network Services > HTTP Server. Lots of fun stuff in there. That's where you install apache2 mods. Tab over to "Server Modules" to configure them. If you don't see one listed that you want to enable for apache2, back out to the root of yast interface and use Software Management to add the required mod package first.

To install, uninstall, upgrade software packages, go to Software Management in yast.

RPM Commands

rpm commmands are a bit cryptic. Here are a few:

Help:

rpm --help

Manual:

man rpm

List all installed packages (query all):

rpm -qa

Only list installed packages with "mod_" in the name:

rpm -qa | grep mod_

To get info about a package:

rpm -qi package_name

To get package file list:

rpm -ql package_name

Package Docs

/usr/share/doc/packages

MORE INFO:

suse yast zope config

SOFTWARE: unix linux bash

yast > System > /etc/sysconfig Editor

For zope on SUSE Linux:

Normally, you edit your conf for zope in:

/var/opt/zope/default/etc/zope.conf

But yast can manage your configuration settings for zope. This option is toggled on/off in yast under:

yast > System > etc/sysconfig Editor > Network > WWW > Zope

Tab thru the options here to toggle config options on/off. If the option to manage has been toggled on in yast, then changes you make directly to zope.conf on the filesystem can cause mismatch errors.

You can also modify the config settings directly on the command line via:

/etc/sysconfig/zope

Note that if you toggle debug mode to on, and your server fails to start, it's because any errors cause startup to fail, rather than simply logging them. You can inspect the event log (/var/opt/zope/default/log/event.log) to find out what might be killing your server. I had a bad product sitting in Products that I didn't notice in production mode since it just failed to show up, but didn't hurt anthing ... until I switched the zope server to debug.

MORE INFO:

symlink (symbolic link)

SOFTWARE: unix linux bash

ln -s target link_name

To create a symbolic link from link_name that points to the actual stuff at target:

ln -s target link_name

In other words it's like:

ln -s go_there if_i_go_here

Here's an example that makes a link called "DIYPloneStyle" in the Products directory and points to the actual stuff in the Plone-3rd-Party directory:

cd /var/opt/zope/default/Products

ln -s /var/opt/zope/default/Plone-3rd-Party/DIYPloneStyle DIYPloneStyle
MORE INFO: http://en.wikipedia.org/wiki/Symbolic_link

quick colorize terminal when ssh to another box

SOFTWARE: unix linux bash

alias ls='ls --color'

Mac OS X Terminal

A quick and dirty way to get mac terminal to colorize files when you're ssh'd to another (remote linux) box. ssh to the remote server, then do:

alias ls='ls --color'

this will set the --color flag for the ls command to always (default), which makes the results of listing a directory colorized by file type.

On your own local machine, you can modify your .bash_profile to include something like this:

export LSCOLORS=fxfxcxdxbxegedabagacad

A bit cryptic to figure out the colors to designate, but nice because it always works on your local machine's shell.

More info about how this stuff works on the linux side via:

dircolors --help
ls --help
echo $TERM

To colorize your terminal in mac: just alias ls -G to ls.

alias ls='ls -G'
MORE INFO:

copy common files (ie plone upgrade)

SOFTWARE: unix linux bash

cp -R mydir /path/to/PARENTdir

Copy only same contents

You can use this trick when upgrading plone for example. You get your new distro from plone.org and you want to cp the Products and lib folders over your old install. But, you've added some stuff (additional products etc) and you want to keep those during the upgrade.

If you copy (cp -R) a directory and give it a target of the parent of a subdirectory with the same name, you'll only overwrite the files that are common to both.

So an example is that I have a "Products" directory with some new versions of stuff, and I want to copy it over my old Products directory which lives in a directory called "default". I do:

cp -R Products /var/opt/zope/default

This way, I get to keep my old stuff that I may have added to the old Products directory and not worry about clobbering all that stuff.

MORE INFO:

regexp match NOT string

SOFTWARE: unix linux bash

^(?:(?!mystring).)*$

To match anything but the string "mystring":

^(?:(?!mystring).)*$
MORE INFO: http://wiki.tcl.tk/986

Add a User

Add a user called "webadmin" to a linux server. This adds them to the "www" group. Gives them the bash shell. Lets them sftp in and work on stuff.
useradd -c "add a comment here" -m -g www -s /bin/bash webadmin

Help is available for "useradd":
useradd --help

Set the password for the new "webadmin" user with:
passwd webadmin

Because they're part of the www group, change user/group on the directories and files if you need to so apache owns them:
chown -R wwwrun:www [directory name]

Now give the "www" group permissions to the stuff the user will work on:
chmod g+x [directory]
chmod g+w [files]


Diff and Patch

diff

A simple example.
Create a patch file by comparing two source documents that are different. First place them in directories like OLD, and NEW. So you might have OLD/DA.py and NEW/DA.py. Then run the following command passing in the two directory names like:

diff -Naur OLD NEW > patch.txt
Because we are using the -r (recursive) flag, diff will look inside the directories and make a patch file that represents the changes between the files it find in those directories. The flags we use are:
N: number of lines = L-F+1
a: Treat all files as text
u: Output NUM (default 3) lines of unified content
r: recursive
Now you have a patch.txt file that you can use to apply these changes to another file:


patch

A simple example.

To apply the patch we created above, we can run patch on a specific target file and use the patch file we created. This will modify the target file directly:

patch some_directory_path/target_file.txt -i some_other_path_to/patch.txt

If you want to create a new file instead of overwriting the target, you can do:

patch some_directory_path/target_file.txt -i some_other_path_to/patch.txt -o output_file.txt


Shell Scripting

http://steve-parker.org/sh/philosophy.shtml

Netstat

To view ports: netstat -pnat

chkconfig

To stop a service (rc{service_name} start | stop | status) from starting on boot:

chkconfig {service_name} off

This effects /etc/init.d/{service_name}

so you can do:

find /etc/init.d/ -name *{service_name}* to find out what's in there

Comments