mlsamuelson.com

man mlsamuelson

...documenting with examples the tools and commands that make mlsamuelson productive...

man mlsamuelson? MAN is a Linux command that displays manual pages documenting commands. It's a wonderful tool, but I often miss having real-world examples, so I record them here.

command line
Title Excerpt
SCP - secure copy protocol

scp -r foo username@remotehost.com:/some/remote/directory/bar
copy local directory 'foo' to remotehost's bar directory. use ~/ as remote directory if you want to copy to the user's home folder

SSH - secure shell

ssh username@hostname.com
log into remote server

TAIL - view the tail end of a file

tail -f phperror.log
in real time watch the end of a log file

SFTP - secure file transfer protocol

sftp username@hostname.com
login to remote server with sftp

ls
list files in remote directory

lls
list files in local directory

cd ..
move up one directory on remote

lcd ..
move up on directory on local

pwd
show current path on remote

lpwd
show current path on local

mkdir / lmkdir and rm / lrm hold to the same pattern

put filename.txt
transfer filename.txt from current local directory to current remote directory

get filename.txt

FIND - locate files and directories

find ./ -name "layout.*"
find all files or directories named layout.[something], recursively under the current directory.

find /home -type f -name "*.inc"
find only files named [something].inc in or below the /home directory

find ~/ -amin -30 -type f
in or below user's home directory, find files accessed less than 30 minutes ago

find www/ -atime +3 -type f -name '*.css'
in or below www directory, find .css files accessed more than 3 days ago

TREE approximation using FIND and SED

find ./ -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
print out a tree of directories from current location

find ./ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
print out a tree of files and directories from current location

hat tip: http://murphymac.com/tree-command-for-mac/

Search last N commands

history 200 | grep 'mysql'

in the last 200 commands entered, match/show any with mysql in them.

!123

if the command you want shows up in the history as line 123, this will run it

(Your up arrow will thank you.)

cp -R and copying contents vs. copying directory

cp -R foo/ faa

copy contents of tmp directory to another_tmp directory
resulting path: /faa/[foo's files]

cp -r foo faa

copy directory foo into directory faa
resulting path: faa/foo/[foo's files]

PUSHD, POPD and DIRS - manage the directory stack

pushd

Add the current directory to the directory stack.

pushd ~/tmp

Add ~/tmp directory to the stack and cd there.

dirs -v

Display the directory stack.

pushd +2

Bring the directory at the 2 position to the front of the stack (0) and cd there.

popd

Pop off (remove from stack) the top/current directory in the stack (position 0).

popd -1

Remove the directory at the 1 position in the stack.

Or you could just use Poor man's recall:

cd -

id, finger and hostname

id

returns the user's identity

hostname --long

print host's name, long form

finger

display info on logged in users

finger joe

lookup information on user joe

Benchmark Apache with ab

ab -n 50 http://my.example.com

make 50 requests in an Apache Benchmark session and display results. gives you great stats like number of requests per second, time taken for tests, etc. a big help if you're tuning an opcode cache such as APC for PHP.

Search SVN logs

svn log example.module | egrep 'example_menu' -B 2 -A 1

display SVN log entries for the file example.module that include the string 'example_menu'. The -B 2 option tells egrep to display two lines of context before and -A 1 one line after the matched line. Things can be thrown off by long or short messages, but generally this works.

cvs
Title Excerpt
CVS messages

Resulting from an Update

U
File copied from the repo.
P
File patched to match the repo.
C
Collision. Manual merging necessary.
G
Merge needed and succeeded.
W
File was deleted - no longer in repo.

Resulting from a Commit

M
File modified.
A
File added.
R
File removed.
Drupal
Title Excerpt
Read log files through Drupal Devel's Execute PHP tool

$out = shell_exec('tail -n 100 /path/to/apache/logs/php_error-log');
dpm($out);

output the last 100 lines from the log file.

Also, using the PHP shell_exec() function you could run "ls" or "less" commands to verify that the proper files are in an environment for which you don't have file level access.

Important note: shell_exec() is a powerful reason to keep PHP filter access restricted to only the most trusted users in your Drupal site - or disabled altogether if not needed.

MySQL
Title Excerpt
IF() - evaulate if conditions in MySQL statements

IF(expression1,expression2,expression3)
if expression1 is true, returns expression2, otherwise, returns expression3

an example:

SELECT IF(a.phone_pref = 'home', a.home_phone, a.work_phone) AS phone
FROM individual a;

RLIKE - use Regex in MySQL SELECT statements

SELECT title FROM periodicals WHERE title RLIKE '^National .* Weekly$';

match all rows where title starts with 'National' and ends with 'Weekly' with anything in between

Matches
National Work Workers Weekly
National Policy Review Weekly
Nationalist Critique Weekly

Not
National Lampoon Magazine
Mountaineering Weekly

REPLACE() - replace text in database fields

UPDATE links SET link_description = REPLACE(link_description, 'string to replace', 'new string');

Replace strings in database fields.

PHP
Title Excerpt
Write to PHP error log

I always forget the second parameter in print_r, so recording this.

error_log(print_r($my_array, TRUE), 0);

does a print_r formatted entry to PHP's error log. Great for testing wne working with service calls...

Vim
Title Excerpt
Search and Replace in Vim

:s/search/replace/
search only the current line and match and replace only the first occurrence of a term

:8,10 s/search/replace/g
search and replace all occurrences from line 8 to 10

:%s/search/replace/g
search and replace in whole document

:%s/search/replace/gc
same as previous but will ask for confirmation on replacements

Bookmarking in Vim

ma
set a mark location named 'a'

`a
jump to mark location 'a'. note: ` is the backtick

User login