Changing directories (
cd /var/www/my_website (go to /var/www/my_website directory)
cd / (go to root directory)
cd .. (go up a directory)
pwd will tell you the present working directory.
ls (list current directory)
ls -l (list current directory with more details, e.g. last updated date, permissions, size)
ls -al (as above, but including hidden files, e.g. .htaccess)
ls -lt (list files by last modified date, newest first;
ls -ltr for the reverse)
chmod (change mode, e.g. permissions), e.g.
chmod ugo+rwx foo to grant read, write and execute permissions for user, group and other for directory foo
chmod o-w foo (remove write permissions for other)
You can also use the octal representation, e.g.
chmod 777 foo is the equivalent of
chmod ugo+rwx foo
du -h folder_name
grep foo my_document.txt (look for occurences of the word foo in the file my_document.txt)
grep string directory (look for a string within all files in a directory)
grep -r -i string directory (as above but does the search recursively and case insensitively)
find . -name foo.php (look for file with name foo.php in the current directory and sub-directories)
find . -maxdepth 1 -name foo.php (as above, but limit the search to the current directory)
find . -maxdepth 1 -name "foo.*" (example of using a wildcard; here look for files named foo with any file extension)
Use the pipe symbol to chain together and refine searches, e.g.:
grep foo error_log | grep bar
This looks for the string foo within error_log, then looks for the string bar within the results of the first search.
more foo.txt (view contents of foo.txt)
less access.log (useful for looking up large log files. Allows you to view the content of a file in reverse order. Type > to go to the end of the file, then b to go up a page and Space to go down a page. Q to quit.)
The results of actions can also be piped into
more for paginated viewing. E.g.
grep foo error_log | more will let you page through results of what you've just grepped for. Use Space to go forward a page, or Return to go forward a line.
Editing and creating files and directories
sudo nano foo.txt (use built in text editor "nano" to edit file foo.txt)
If you run the above command and foo.txt doesn't exist, nano will create it for you (but you need to do a Ctrl + O within nano to write the file).
You can also write results, e.g. of a search, out to a file, e.g.:
grep foo error_log > /home/username/search_result.log
mkdir foo (create directory foo)
rm foo.txt (delete file foo.txt)
rm -r bar (delete directory bar)
Copying and moving
cp source_file destination_file (make a copy of source_file and name it destination_file. Also works for copying directories)
mv source_file destination_file (move a file or directory)
mv source_file .. (move source_file up a directory)
scp to copy files between two machines. E.g.:
From this machine to another:
scp /path/to/local/file username@hostname:/path/to/remote/file
From another machine to this one:
scp username@hostname:/path/to/remote/file /path/to/local/file
Connecting via SSH to server
ssh email@example.com (connect to server; alternatively use domain name in place of IP address)
exit (close connection)
Useful keyboard shortcuts
Ctrl + C usually quits the current process, e.g. if you're paging through search results or a large file.
⌘ + K clears the screen.
Ctrl + K deletes characters from the cursor to the end of the line.
Ctrl + A moves cursor to the beginning of the current line; Ctrl + E moves it to the end.
Ctrl + _ to undo typing.
In Terminal.app, hold down ⌥ and the pointer will change to a crosshair. You can then click to place the cursor anywhere within the currently editable area. Useful in nano. Can be quicker than using the arrow keys.