Monday, October 26, 2009

Find Hard Drive Space / Memory Usage in Linux Quick Example

$ df -H
____the H puts it in human readable format

Find memory usage in Linux:

$ free -m
____the m puts it in megabytes

Find our how much processor and memory processes are using:

$ top
____like ps, but continuously refreshes, and shows most active processes at top.
____hit capital 'O' inside top to change which column things are sorted by.
$ ps aux
___ display all current processes, user names, process ids, memory use, etc.
$ ps aux | grep searchd
___ display any processes matching searchd

Copying files to / from / between linux servers, quick examples

using scp (like normal cp, but for servers):

$ scp file.txt user@remoteserver.com:mydir/file.txt
____copy file.txt from current directory to remote server under mydir

$ scp user@server1.com:file.txt user@server2.com:file.txt
____copy file.txt from server1 to server2

using sftp:

$ sftp user@server.com
____open secure ftp sessions
sftp> put file.txt
____copy file.txt from local directory to remote
sftp> get file.txt
____copy file.txt from remote to local directory where sftp was run
sftp> lls lpwd lcd
____putting an 'L' in front of the command lets you navigate local while logged into remote.

Linux File Permissions Quick Examples (chmod howto)

$ ls -l
___t all files and their permissions.

[properties] [links]__[owner] _[group]__ [size]_ [date modified] __[filename]
drwxr-xr-x____2____ scott____staff____68____Sep 7 07:00__ __temp
-rw-r--rwx____ 1___ _scott ___ staff __ _ 23 ____Sep 4 16:59 ____ temp.txt
-rw-rwxr--____ 1 ____scott _ __staff ____23 ____Sep 4 17:03 ____temp2.txt
-rwxr--r--___ _ 1 _ __scott _ __staff ____23____ Sep 4 17:05 ____ test3.txt

properties:__ [d]______[rwx]___[rwx]__[rwx]
___________[filetype][owner][group][other]
r: read
w: write
x: execute (add if you want to execute a script file, for instance)

$ chmod gu+wx file.txt
___add write and execute privilege to the group and owner
$ chmod o-rw file.txt
___take away read and write privileges for others
$ chmod g=rw,o=r temp.txt
___set group to read & write, others to just read
$ chmod 755 temp.txt
___number codes: (r = 4,w = 2,x = 1).
___7 => rwx for owner (4+2+1)
___5 => r-x for group (4+1)
___5 => r-x for others
$ chmod -R a=rw mydir
___change all (user, group and others) to rw, recursively including all files in mydir

Linux Command Line Navigation Quick Examples, Tips

$ cd -
____switch to previous directory

typing commands:
  • tab : auto-complete
  • ctrl-a : move cursor to beginning of line
  • ctrl-e : move cursor to end of line
  • ctrl-k : delete whole line from cur
  • ctrl-w : delete previous word
  • Esc b : move backwards one word
  • Esc f : move forwards one word
  • ctrl-c : cancel command
$ crtl-r
____search command history for matching commands
$ alias ls='ls -lG'
____make ls an alias for ls -lG. The G adds color in OS X panther+. Have to put in your ~/.bash_profile to retain.
$ history
____view command history, $ !47 to execute that command, !! executes last command
$ pushd
____like cd, but stores the directory in history so you can go back to it with popd, which is like the 'back' button.

$ pushd
____like cd, but stores the directory in history so you can go back to it with popd, which is like the 'back' butt

shortcuts / symlinks / symbolic links in linux:

$ ln -s target link_name
$ ln -s /usr/local/long_directory/ ~/short_cut

after following a sym link, type pwd -P to get the absolute path

and to change to it, type cd `pwd -P` (the backwards apostrophe on the ~ key)

Howto Unzip .gz, tar.gz, .zip Files in Linux, Quick Examples

$ gzip myfile.txt
____zip (creates myfile.gz, deletes myfile.txt)
$ gzip -r mydir
___ zip several files--doesn't package! (creates .gz versions of each file in mydir. Not gzip and gunzip don't group several files together into one file, for that you need tar)
$ gunzip myfile.gz
___ unzip (creates myfile.txt, deletes myfile.gz)

$ tar -xzfv foo.tar.gz
___ unzip and extract (note: tar by itself just groups / archives files, it doesn't compress. Using the z option causes it to use gzip/gunzip to also compress / uncompress files.)
___ x/c: extract, use c to create an archive
___ z: use gzip/gunzip to also compress (you end up with .gz files)
___ f: operate on a file and not typed input
___ v: print out files it's operating on verbosely
$ tar -cf foo.tar mydir.tar mydir/
___ archive folder (add option z to also compress)

$ zip -r test.zip mydir
____zip (replace mydir with * to zip up everything in the cur dir)
$ unzip test.zip
____unzip

Text Editing / Viewing Files in Linux (quick examples)

$ nano myfilename
____basic navigation commands are displayed (page up + down), no need to remember!
$ cat myfilename
____prints contents out, no editor
$ less myfilename
____view file text, no editing
____G : end of file,
____ctrl-v : page down, (works in man listings)
____ctrl-b : page up, (works in man listings)
____/text : forward search
____?text : backward search:
____F : go to end, display new data, (good for watching logs)
$ tail -n 200 development.log
____prints last 200 lines, -f keeps the file open and prints more as the file grows

Find Files / Search in Linux: Quick Examples

$ find / -name myfilename
____look in the root directory + mounted drives

$ find / -mtime -2
____find all files modified in the last two days

$ find . -name '?foo*'
____find all files in current and sub directories that start with foo (wildcard) (have to use ' ' with *)

$ find . -iname AbCd
____case insensitive

$ find / -user scott
____find files owned by scott (can do groups and permissions, too)

$ find / -type f
____find files, not directories. d is for directories, l is for sym links.

$ find / -name foo 2>/dev/null
____hide "permission denied errors". You could also just do $ sudo find ...

$ find . -maxdepth 2
____only go 2 directories deep

$ find . -size +20k -size -1M
____size is greater than 20k, less than 1M. note lower case k.

$ find . -exec grep -iH dog {} \;
____find files containing text (dog), i means case insensitive, sub l for H to get only file names
$ locate
____an indexed version of find, may not have recent files