Shell Tips

From WikiDLXTV
Revision as of 09:51, 2 September 2010 by Odoggy (Talk | contribs)

Jump to: navigation, search

Common Shell Commands

I've put together some of the useful shell commands that are available under WDLXTV (using SSH or Telnet). They're organized by order of usability (most used first), so you can easily find a command, their description and how to use it. This guide will continue to be updated and if you would like to add to this guide, please edit after checking the command yourself.

Remember you can usually auto complete filenames my pressing [TAB] (if there are multiple options, pressing [TAB] again will show them all).

More useful

  • 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.
  • cd : change directory (ex: cd /usr/local/apache will go to /usr/local/apache/)
cd ~ : go to your home directory
cd - : go to the last directory you were in
cd .. : go up a directory
  • cat : print file contents to the screen (ex: cat filename.txt : cat the contents of filename.txt to your screen)
more : like cat, but opens the file one screen at a time rather than all at once (browse through the file, hit Space to go to the next page, q to quit)
tail : like cat, but only reads the end of the file. ex:
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
  • chmod : changes file access permissions. (usage: chmod permissions filename)
Permission Table
# permission meaning
0 --- No permission
1 --X Execute only
2 -W- Write only
3 -WX Write and execute
4 R-- Read only
5 R-X Read and execute
6 RW- Read and write
7 RWX Read, write and execute
  • permission is a set of 3 numbers in this order from left to right: USER - GROUP - EVERONE. The three digits can be 0-7 as shown in the table on the right:
ex: chmod 000 file1 means No one can access file1, chmod 777 file2 means Anyone can access /write to/execute file2
  • chown : changes file ownership permissions. The permission set is of 2 parameters going from left to right: USER.GROUP (ex: 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)
  • nano : friendly, easy to use file editor (ex: pico /conf/S00user-script : edit the custom options user file)
  • vi : another editor, tons of features, harder to use at first than nano, read more info here.
  • grep : looks for patterns in files, ex:
grep root /etc/passwd : shows all lines with the string root in file /etc/passwd
grep -v root /etc/passwd : shows all lines that do not match string root. Remember strings are case sensitive.
  • cp : copy a file
cp filename filename.backup : copies filename to filename.backup
cp -a /root/.config* /home/.config : copies all files, retaining permissions form one directory to another.
cp -a * newdir : Copies all files and directories recurrsively in the current directory INTO newdir
  • mv : Move a file command
mv oldfilename newfilename : Move a file or directory from oldfilename to newfilename
  • rm : delete a file
rm filename.txt : deletes filename.txt, will more than likely ask if you really want to delete it
rm -f filename.txt : deletes filename.txt, will not ask for confirmation before deleting.
rm -rf mydir/ : recursively deletes the directory tmp, and all files in it, including subdirectories. BE VERY CAREFULL WITH THIS COMMAND!!!
  • kill : terminate a system process, usage: kill PID. Use top or ps to get system PIDs (Process IDs)
kill 10550 : will terminate process with ID 10550
  • ARCHIVES : Creating and Extracting .tar.gz, .tar and .zip files
tar -zxvf file.tar.gz : Extracts the file
tar -xvf file.tar : Extracts the file
tar -cf archive.tar contents/ : Takes everything from contents/ and puts it into archive.tar
gzip -d filename.gz : Extracts the file
unzip file.zip : Extract .zip files
  • 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 box isn't bogged down. Type top then press Shift + M to sort by memory usage or Shift + P to sort by CPU usage
  • 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 it you can kill (or terminate) a running program on your server (see kill command).
  • pwd: short for print working directory. It's used to output the path of the current working directory.

Less useful

  • du : shows disk usage.
du -sh : shows a summary, in human-readble form, of total disk space used in the current directory, including subdirectories.
du -sh * : same thing, but for each file and directory. helpful when finding large files taking up space.
  • touch : create an empty file. ex: touch /config/S00user-script : create an empty file called S00user-script in the directory /config
  • ln : create's "links" between files and directories, ex: 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 original, however you can delete the link and it will not delete the original.
  • wc : short for word count. Counts how many lines, words and characters are in a file, ex: wc -l filename.txt : tells just how many lines are in filename.txt
  • last : shows who logged in and when, ex:
who : This also shows who is on the server in an shell.
  • netstat : shows all current network connections.
netstat -an : shows all connections to the server, the source and destination ips and ports.
netstat -rn : shows routing table for all ips bound to the server.
  • file : attempts to guess what type of file a file is by looking at it's content.
file * : prints out a list of all files/directories in a directory


Putting commands together

Often you will find you need to use different commands on the same line. Here are some examples. Note that the | character is called a pipe, it takes date from one program and pipes it to another. > means create a new file, overwriting any content already there. >> means tp append data to a file, creating a newone if it doesn not already exist. < send input from a file back into a command. Here are some examples:


grep http /etc/lighttpd/lighttpd.conf |more

This will dump all lines that match 'http' from the lighthttpd.conf, then print the results to your screen one page at a time.


last > /tmp/lastlogins.tmp 

This will print all the current login history to a file called lastlogins.tmp in /root/


tail -10000 /conf/S00user-script |grep config_tool |more

This will grab the last 10,000 lines from /conf/S00user-script, find all occurances of config_tool, then send it to your screen page by page.


netstat -an |grep :80 |wc -l

Show how many active connections there are to a webserver (lighthttpd runs on port 80)