The following is a resource for shell commands (including ssh) that I’ve accumulated. It is growing so if you can’t find something, drop me a line.
You can find miscellaneous shell commands, Apache, MySQL and nano commands.
More can be found here
| Command | Example | Notes |
|---|---|---|
| /bin/pwd | /bin/pwd | Prints the absolute working directory when you’re in a symlinked directory |
| cat | cat filename.txt | Prints contents of a file to the screen (shell) |
| cat /dev/null > myfilename | Deletes the contents of myfilename without having to delete and recreate the actual file | |
| cd | cd /usr/local/apache | change directory, go to /usr/local/apache/ directory |
| cd ~ (or just cd) | go to your home directory | |
| cd - | go to the last directory you were in | |
| cd .. | go up a directory | |
| chown |
Changes file ownership permissions. The set of 2 go in this order from left to right: USER – GROUP |
|
| chown root myfile.txt | Changes the owner of the file to root | |
| chown root.root myfile.txt | Changes the owner and group of the file to root | |
| chmod |
Changes file access permissions. The set of 3 go in this order from left to right:
0 = — No permission |
|
| chmod 000 index.html | No one can access | |
| chmod g+w index.html | Adding write access for the “group” | |
| clear | Clears the terminal screen | |
| cmp | cmp file1.html file2.html | Compares 2 files and writes the result to the standard output. 0 = identical 1 = different |
| cp | cp filename filename.backup | copies filename to filename.backup |
| cp -a /home/burst/new_design/* /home/burst/public_html/ | copies all files, retaining permissions form one directory to another. | |
| cp -av * ../newdir | Copies all files and directories recurrsively in the current directory INTO newdir | |
| find | find -mtime 7 | “Finds” files modified in the last 7 days |
| find . -mtime -1 -ls | Finds file modified in the last day | |
| echo | echo “My text here” > mynewfile | Creates a file called ‘mynewfile’ with the contents of ‘My text here’ |
| grep | looks for patterns in files | |
| grep root /etc/passwd | shows all matches of root in /etc/passwd | |
| grep -c mystring myfile.txt | shows how many matches of “mystring” were found in the file | |
| grep -v root /etc/passwd | shows all lines that do not match root | |
| history | looks up previously entered commands | |
| !n | executes line ‘n’ of the history record | |
| java | java -version | Provides information on your current java version |
| kill | Terminate a system process | |
| kill -9 PID | ||
| locate | sudo /usr/libexec/locate.updatedb | Manually updates the database |
| ln | Creates links between files and directories | |
| ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf | Now you can edit /etc/httpd.conf rather than the original. changes will affect the orginal, however you can delete the link and it will not delete the original. | |
| ls | ls | list files/directories in a directory, comparable to dir in windows/dos. |
| ls -al | shows all files (including ones that start with a period), directories, and details attributes for each file. | |
| ls -ld */ | shows all directories (in long format) from the current position | |
| mkdir | Creates new directories | |
| more | like cat, but opens the file one screen at a time rather than all at once | |
| more /etc/userdomains | browse through the userdomains file. hit Spaceto go to the next page, q to quit | |
| mv | mv oldfilename newfilename | Move a file or directory from oldfilename to newfilename |
| ps | short for process status, which is similar to the top command. It’s used to show currently running processes and their PID.A process ID is a unique number that identifies a process, with that you can kill or terminate a running program on your server (see kill command). | |
| ps U username | ||
| ps aux | shows all system processes | |
| ps aux –forest | shows all system processes like the above but organizes in a hierarchy that’s very useful! | |
| pwd | pwd | “Print Working Directory”, outputs the path to the current folder location. See “/bin/pwd” |
| rm | rm filename.txt | deletes filename.txt, will more than likely ask if you really want to delete it. rm -rf `find . -name .svn` |
| rm -f filename.txt | deletes filename.txt, will not ask for confirmation before deleting. | |
| rm -rf tmp/ | recursively deletes the directory tmp, and all files in it, including subdirectories. BE VERY CAREFULL WITH THIS COMMAND!!! | |
| sudo -l | sudo -l | What commands can the current user run on this host? |
| sudo !! | sudo !! | Executes the last command with “sudo” added |
| tail | like cat, but only reads the end of the file | |
| tail /var/log/messages | see the last 20 (by default) lines of /var/log/messages | |
| tail -f /var/log/messages | watch the file continuously, while it’s being updated | |
| tail -200 /var/log/messages | print the last 200 lines of the file to the screen | |
| tar | Creating and Extracting .tar.gz and .tar files | |
| tar -zxvf file.tar.gz | Extracts the file | |
| tar -cf archive.tar contents/ | Takes everything from contents/ and puts it into archive.tar | |
| gzip -d filename.gz | Decompress the file, extract it | |
| top | shows live system processes in a nice table, memory information, uptime and other useful info. This is excellent for managing your system processes, resources and ensure everything is working fine and your server isn’t bogged down. top then type Shift + M to sort by memory usage or Shift + P to sort by CPU usage |
|
| touch | creates an empty file | |
| touch index.html | create an empty file called index.html in the current directory | |
| unzip | unzip file.zip | Extracting .zip files shell command |
| wc | wc -l filename.txt | tells how many lines are in filename.txt |
Apache commands |
||
| httpd | httpd -v | Outputs the build date and version of the Apache server. |
| httpd -l | Lists compiled in Apache modules | |
| httpd status | Only works if mod_status is enabled and shows a page of active connections | |
MySQL commands |
||
| mysql/mysqladmin | mysqladmin processlist | Shows active mysql connections and queries |
| mysqladmin create databasenamehere | Creates a mysql database | |
| mysql -u root -p | Connects to mysql | |
| show databases; | Shows all databases on the server (once connected) | |
| use [database]; | Connects you to a particular database | |
| show tables; | Displays all tables once you have connected to a database | |
| show table status; | Displays table metadata including sizes | |
| mysqldump -h db.example.com -p mydbname | gzip > mydbname.sql.gz | Back’s up a database | |
| gunzip < [mydbname.sql.gz] | mysql -u [uname] -p[pass] mydbname | Restores a database | |
| mysql -u root dbname < <(cat *sql) | Restores a database if you have an sql file per table | |
Nano commands |
||
| ctrl + shift + space | or ctrl + shift + alt | Moves forward 1 word |
| ctrl + j | justifies the current paragraph | |
| ctrl + a | moves to start of line | |
| ctrl + o | saves file to disk | |
| ctrl + e | moves to end of line | |
| ctrl + v | moves down a screen | |
| ctrl + y | moves up a screen | |
| ctrl + c | shows where cursor is (handy if you’ve moved down pages etc) | |
