Once in a while a developer has to say goodbye to his or her favourite text editor and go commando-style command-line editing with nothing but basic linux skills and vi. This is the bare minimum you need to know if you want to survive the black screen.

Basic navigation

The tilde ~ refers to the home page
The dot . refers to current directory

Moving one directory up:
cd ../
Move to the home directory:
cd ~
Move to the root folder
cd /root
Move to the folder public in the current directory
cd public

Directory overview

Get a overview:
ls -la
with flags -a for all and -l for long format.
Get a recursive directory view
ls -lar

Finding files

Find all files with .php extension by recursion from current directory
find -name "*.php"
Find all files with .php extension but exclude the Zend folder
find -name "*.php" ! -path "*/Zend/*"
Case-insensitive search:
find -iname "test.php"

Finding text in files

Find the text string userName in any file in the current directory
grep "userName" ./
Find one word but not another word in a file
grep "xxx" | grep -v "yyy"
Find the text string user in any file but not .svn files
find ! -name "*.svn-base" | xargs grep -iln "user" 2>/dev/null
The addition 2>/dev/null will supress any No such file or directory notice. The flag -i makes the query case-insensitive and the the flag -l will make sure only the filenames are printed. The line number is printed with the flag -n.
Find the text string user in any file but exclude the cache folder
find -path ./cache -prune -o | xargs grep -in "user" 2>/dev/null

File / directory manipulation

Create a file
touch file.txt
Delete a file
rm file.txt
Delete a directory by recursion
rm -Rf dirname
Create a directory
mkdir dirname
Delete a directory
rmdir dirname

Vi text editor

Two major text editors are vi and the more extensive vim (Vi IMproved).

Open a file
vi filename.text
Open a file at a specific line number
vi filename.text +200
Switch to edit mode
A
A stands for addition
Save a file
control C followed by :w
Save a file and exit
control C followed by :wq
Find a text string
/xxxx
For the next instance type n, for the previous instance type cntl n
Goto line number 200
+200
Copy and paste:
press 10yy  (y = yanked) to copy 10 lines starting from the current line and then move the cursor to the position for pasting and
press p
Delete a section:
Repeat above but use d (= delete) instead of y.

Notes

* In case of a CTRL s event see: https://raam.org/2007/recovering-from-ctrls-in-putty/
* For multiscreen Putty sessions see SuperPutty.

Advertisements