learning the command line
DESCRIPTION
An introduction to the Unix command line with a focus on uses for web developers given at Code for Miami MeetUp in April 2014.TRANSCRIPT
![Page 1: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/1.jpg)
Learning the Command Line
...the first thing that Apple's hackers had done when they'd got the MacOS up and running--probably even before they'd gotten it up and running--was to re-create the Unix interface, so that they would be able to get some useful work done...
-- In the Beginning was the Command LineNeal Stephenson
http://www.slideshare.net/aramonc/learning-the-command-line-33146569
![Page 2: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/2.jpg)
Follow along...
$>mkdir -p ~/www/wp_site/news
&& wget http://wordpress.org/latest.zip
&& unzip latest.zip -d
~/www/wp_site/news
Chaining commands with &
Make news directory under /var/www/wp_site, then download latest version of WordPress, then extract the contents of the archive to /var/www/wp_site/news
![Page 3: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/3.jpg)
● Developer at ServerGrove● All around nerd● Systems Administrator for
7 years● @aramonc in all the places
About Me
![Page 4: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/4.jpg)
More than 40% of all web servers use *nix
Why bother?
DebianUbuntu
CentOS/RHELSmartOS*
Servers do not use graphical interfaces*Not really Linux, still Unix based
![Page 5: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/5.jpg)
30,000 Web Sites Hacked A Day
Why bother?
![Page 6: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/6.jpg)
● Increased speed & flexibility● Muscle memory● Large pool of freely available utilities● *nix Command Line utilities are mostly
standard
Why bother?
![Page 7: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/7.jpg)
What if I mess up?
![Page 8: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/8.jpg)
![Page 9: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/9.jpg)
Getting to the Command Line
Windows
Localhost:Cygwin
Webhost:SSH via PuTTY
OSX, *nix
Localhost:Terminal, xterm, etc
Webhost:SSH via the above
![Page 10: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/10.jpg)
Navigating Folders = Directory Tree
![Page 11: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/11.jpg)
Where are you right now?
$>pwd/Users/adrian_sg/www/wp_site/news
Print Working Directory
● /Users/adrian_sg/www/wp_site/news = a path● / not \● Starts with / (root) = absolute
![Page 12: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/12.jpg)
What’s in here?
$>lsindex.php wp-includeslicense.txt wp-links-opml.phpreadme.html wp-load.phpwp-activate.php wp-login.phpwp-admin wp-mail.phpwp-blog-header.php wp-settings.phpwp-comments-post.php wp-signup.phpwp-config-sample.php wp-trackback.php
LiSt
![Page 13: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/13.jpg)
What’s in here?
$>ls -latotal 320drwxr-xr-x 21 adrian_sg wheel 714 Feb 28 22:07 .drwxr-xr-x 3 root wheel 102 Feb 28 21:57 ..-rw-r--r--@ 1 adrian_sg wheel 640 Mar 05 20:14 .htaccess-rw-r--r--@ 1 adrian_sg wheel 418 Sep 24 20:18 index.php-rw-r--r--@ 1 adrian_sg wheel 19929 Jan 18 2013 license.txt-rw-r--r--@ 1 adrian_sg wheel 7185 Jan 13 13:16 readme.htmldrwxr-xr-x@ 88 adrian_sg wheel 2992 Jan 23 15:17 wp-admindrwxr-xr-x@ 5 adrian_sg wheel 170 Jan 23 15:17 wp-content-rw-r--r--@ 1 adrian_sg wheel 2932 Sep 24 20:18 wp-cron.php
LiSt
![Page 14: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/14.jpg)
Moving around...
$>cd wp-admin$>pwd~/www/wp_site/wp-admin$>cd /var/www/wp_site$>pwd~/www/wp_site$>cd wp-content/themes/twentyfourteen$>pwd~/www/wp_site/wp-content/themes/twentyfourteen
Change Directory
![Page 15: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/15.jpg)
Moving around...
$>cd ..$>pwd~/www/wp_site$>cd ~/$>pwd/Users/adrian_sg$>cd -$>pwd~/www/wp_site
Change Directory
![Page 16: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/16.jpg)
Where is this thing?
$>find . -iname xml\*
~/www/wp_site/xmlrpc.php
$>find . -mtime 3
Find
. (dot) means current directory-mtime in 24 hour increments of modified date
![Page 17: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/17.jpg)
Where is this thing?
$>grep theme ./*
./index.php: * wp-blog-header.php...the theme.grep: ./wp-admin: Is a directory./wp-settings.php:require(ABSPATH.WPINC.'/theme.php');./wp-settings.php:do_action( 'setup_theme' );
Globally search a Regular Expression and Print
![Page 18: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/18.jpg)
Where is this thing?
$>grep -R theme ./*
./wp-admin/update.php: $parent_file='themes.php';
./wp-admin/update.php: $submenu_file='themes.php';
./wp-admin/update.php: $nonce='install-theme_'.$theme;
Globally search a Regular Expression and Print
![Page 19: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/19.jpg)
Can I change things?
$>ls -ltotal 320-rw-r--r--@ 1 adrian_sg wheel 418 Sep 24 20:18 index.php-rw-r--r--@ 1 adrian_sg wheel 19929 Jan 18 2013 license.txt-rw-r--r--@ 1 adrian_sg wheel 7185 Jan 13 13:16 readme.htmldrwxr-xr-x@ 88 adrian_sg wheel 2992 Jan 23 15:17 wp-admindrwxr-xr-x@ 5 adrian_sg wheel 170 Jan 23 15:17 wp-content-rw-r--r--@ 1 adrian_sg wheel 2932 Sep 24 20:18 wp-cron.phpdrwxr-xr-x@ 122 adrian_sg wheel 4148 Jan 23 15:17 wp-includes-rw-r--r--@ 1 adrian_sg wheel 2359 Oct 24 18:58 wp-load.php
Owners, Groups, & Permissions
![Page 20: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/20.jpg)
Can I change things?
Permissions
r = Readw = Writex = Execute (run a script or open a directory)
Three levels of permissions: Owner, Group, Everyone else
![Page 21: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/21.jpg)
Can I change things?
Owner = User● Sometimes your user● Sometimes root● Sometimes www-data (Apache user)
![Page 22: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/22.jpg)
Can I change things?
Group● Permissions for a set of users● Users have own group● Most users only belong to own group
![Page 23: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/23.jpg)
Can I change things?
Permissions
Owner Group EveryoneIs Dir.?
rwx r-x r-xd
$>ls -ldrwxr-xr-x@ 88 adrian_sg wheel 2992 Jan 23 15:17 wp-admin
adrian_sg wheel
![Page 24: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/24.jpg)
How do I change this thing?
$>mkdir -p useless/stuff
$>ls -l-rw-r--r--@ 1 adrian_sg wheel 418 Sep 24 20:18 index.php-rw-r--r--@ 1 adrian_sg wheel 19929 Jan 18 2013 license.txt-rw-r--r--@ 1 adrian_sg wheel 7185 Jan 13 13:16 readme.htmldrwxr-xr-x 2 adrian_sg wheel 68 Mar 3 22:04 useless-rw-r--r--@ 1 adrian_sg wheel 4892 Oct 4 10:12 wp-activate.php
MaKe DIRectory
![Page 25: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/25.jpg)
How do I change this thing?
$>mv readme.html useless/stuff/readme
$>ls -l
-rw-r--r--@ 1 adrian_sg wheel 418 Sep 24 20:18 index.php-rw-r--r--@ 1 adrian_sg wheel 19929 Jan 18 2013 license.txtdrwxr-xr-x 2 adrian_sg wheel 68 Mar 3 22:04 useless-rw-r--r--@ 1 adrian_sg wheel 4892 Oct 4 10:12 wp-activate.php
MoVe
![Page 26: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/26.jpg)
How do I change this thing?
$>cp wp-config-sample.php wp-config.php
$>ls -l
-rw-r--r--@ 1 adrian_sg wheel 4795 Sep 5 21:38 wp-comments-post.php-rw-r--r--@ 1 adrian_sg wheel 3087 Oct 24 18:58 wp-config-sample.php-rw-r--r--@ 1 adrian_sg wheel 3087 Mar 3 21:59 wp-config.phpdrwxr-xr-x@ 5 adrian_sg wheel 170 Jan 23 15:17 wp-content
CoPy
● cp -a keeps same permissions as original
![Page 27: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/27.jpg)
![Page 28: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/28.jpg)
When everything goes wrong...
$>less
/var/log/apache2/error_log
Read the log file
![Page 29: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/29.jpg)
![Page 30: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/30.jpg)
When everything goes wrong...
Read the log file
● (up arrow) to scroll towards the top● (down arrow) to scroll towards the bottom● (space) next page● p previous page● q(uit) to exit● / <expression> to search for <expression>● n to search again
![Page 31: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/31.jpg)
But I only care about the last error...
$>tail -n 5 /var/log/apache2/error_log[Mon Mar 03 10:28:01 2014] [notice] Digest: generating secret for digest authentication ...[Mon Mar 03 10:28:01 2014] [notice] Digest: done[Mon Mar 03 10:28:01 2014] [notice] Apache/2.2.26 (Unix) DAV/2 PHP/5.5.4 mod_ssl/2.2.26 OpenSSL/0.9.8y configured -- resuming normal operations[Mon Mar 03 13:40:08 2014] [notice] child pid 8870 exit signal Bus error (10)[Mon Mar 03 18:02:20 2014] [notice] caught SIGTERM, shutting down
Read the last few lines of the log file
![Page 32: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/32.jpg)
I only care about the current error...
$>tail -f /var/log/apache2/error_log[Mon Mar 03 10:28:01 2014] [notice] Digest: generating secret for digest authentication ...[Mon Mar 03 10:28:01 2014] [notice] Digest: done[Mon Mar 03 10:28:01 2014] [notice] Apache/2.2.26 (Unix) DAV/2 PHP/5.5.4 mod_ssl/2.2.26 OpenSSL/0.9.8y configured -- resuming normal operations[Mon Mar 03 13:40:08 2014] [notice] child pid 8870 exit signal Bus error (10)[Mon Mar 03 18:02:20 2014] [notice] caught SIGTERM, shutting down
[Mon Mar 03 20:18:01 2014] [notice] Apache/2.2.26 (Unix) DAV/2 PHP/5.5.4 mod_ssl/2.2.26 OpenSSL/0.9.8y configured -- resuming normal operations
Read the log file as it happens
● -f is interactive● Ctrl+C to exit interactive
![Page 33: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/33.jpg)
Who is currently on my site?
$>tail -f /var/log/apache2/*access_log::1 - - [26/Dec/2013:09:35:23 -0500] "OPTIONS * HTTP/1.0" 200 -127.0.0.1 - - [26/Dec/2013:09:35:34 -0500] "GET / HTTP/1.1" 200 44127.0.0.1 - - [26/Dec/2013:09:35:34 -0500] "GET /favicon.ico HTTP/1.1" 404 209127.0.0.1 - - [26/Dec/2013:09:36:40 -0500] "GET / HTTP/1.1" 304 -127.0.0.1 - - [26/Dec/2013:09:36:42 -0500] "GET / HTTP/1.1" 304 -
Read the access log as it happens
![Page 34: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/34.jpg)
How do I change this thing
$>nano wp-config.php
Nano text editor
![Page 35: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/35.jpg)
![Page 36: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/36.jpg)
How do I change this thing
$>nano wp-config.php
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell
Nano text editor
![Page 37: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/37.jpg)
Putting it all together
$>tail -n 10000
/var/log/apache2/error_log | grep “Mar
01” | less
Deep filtering with the pipe statement
Look for any log entry that occurred on March 1st within the last ten thousand lines of the error log and display it in a way I can easily navigate
![Page 38: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/38.jpg)
Putting it all together
$>mkdir -p wp-content/uploads && echo -
e "RemoveHandler .php .phtml .php3
php4\nRemoveType .php .phtml .php3
php4" > wp-content/uploads/.htaccess
Creating files without a text editor
Make a new uploads directory in wp-content, then create a .htaccess file in uploads with the contents of “RemoveHandler .php .phtml .php3 php4RemoveType .php .phtml .php3 php4”
![Page 39: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/39.jpg)
Resources
● Usage Share of Server OSes
● In The Beginning Was the Command Line
● linuxcommand.org
● 5-Minute Essential Shell Tutorial
● PuTTY
● Cygwin
![Page 40: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/40.jpg)
Questions?
![Page 41: Learning the command line](https://reader034.vdocuments.mx/reader034/viewer/2022052619/5551a795b4c905013a8b54c8/html5/thumbnails/41.jpg)
Thank you!http://www.slideshare.net/aramonc/learning-the-command-line-33146569