UNIX commands

The following are some basic commands which may be useful in the UNIX/Linux Shell

Working with directories
Note that you can always discover what directory you are currently in using the  command: [server]$ pwd /home/current/directory/ [server]$

Creating directories
To create a new directory, use the  command: [server]$ mkdir directory_name  [server]$

Deleting directories
There are actually a few ways to delete directories in the shell. To delete an empty directory, use the  command: [server]$ rmdir directory_name  [server]$

To delete a non-empty directory (one that still contains files or other directories in it)  will not work. Here you have two choices. You can either remove all the contained files and directories by hand using the  and   commands, or you can use  's   flag. [server]$ rm -r directory_name  [server]$ Be careful using this flag, as you will delete everything contained in the directory specified. There is no "Recycle Bin" or "Trash Can" in the shell. What you delete is gone forever, so use caution.

Changing directories
To change to another directory, use the  command: [server]$ cd /target/directory  [server]$ A successful change will not return any messages.

To change to your previous directory, use the  command:

[server]$ pwd /current/directory/ [server]$ cd /new/directory/  [server]$ pwd /new/directory/ [server]$ cd - [server]$ pwd /current/directory/ [server]$

The user above has changed from one directory to another, then used the  command to return to their previous directory.

You can go to the parent directory quickly by using the.

cd ../

You can go up multiple directories by stringing the  together.

cd ../../

List contents of current directory
To list the contents of a directory, use the  command: [server]$ ls Maildir   example.com    logs [server]$

Add the  flag to list the contents with full details, including permissions, file size and last modified date: [server]$ ls -l drwx--S---   12 username  groupname    4096 Mar 15 17:28 Maildir drwxr-xr-x    5 username  groupname    4096 Mar  7 12:35 example.com drwxr-sr-x    8 root      root         4096 Apr 17 06:33 logs [server]$

To list all files within the directory including hidden files add the  flag: [server]$ ls -a .               .bash_profile    example.com ..              .bashrc          logs .bash_history   Maildir [server]$

ls sorting
Some of you might notice that the  settings show up in a semi-alphabetical order with dot files (files starting with dots) and cased files intermixed. Like so: [server]$ ls -a .               .bash_profile    example.com     TEMP ..              .bashrc          .gnome          .vimrc .bash_history   Maildir          logs [server]$ This has to do with the  setting allowing   to sort based on your local language. To fix this and put it back to "normal"  listing, unset your   environment variable. [server]$ unset LANG [server]$ ls -a .               .bash_profile   .vimrc           example.com ..              .bashrc         Maildir          logs .bash_history   .gnome          TEMP [server]$ You can add the  command into your   file (at the end) so that it unsets everytime you log into the server.

Directory sizes
To determine the size of a directory, use the  command: [server]$ du example.com  1532   example.com [server]$ Add the  flag for more human readable format. You can also list multiple directories and files seperated by spaces.

Creating files
To create a new file, you can either save it from within a text editor or other program, or you can use the  command: [server]$ touch filename  [server]$ This creates an empty file named.

Moving files
To move a file to another place: [server]$ mv /old/location/filename /new/location/filename  [server]$

Note that you can also use relative paths: [server]$ mv filename ../directory/filename  [server]$

Copying files
To copy a file to another place: [server]$ cp /existing/location/filename /new/location/filename  [server]$

Renaming files
is also used to rename a file to something else: [server]$ mv oldfilename newfilename  [server]$

Delete files
To delete a file: [server]$ rm filename  [server]$

You can use wildcards to delete multiple files with similar names. To delete all files beginning with "pic" (eg, pic01.jpg, pic02.jpg, etc): [server]$ rm pic* [server]$

The wildcard can appear anywhere in the string. To delete all .jpg files: [server]$ rm *.jpg [server]$

Be careful when using wildcards - you can inadvertently delete files this way. Using the -i flag, you will be asked to confirm deleting files. Type y or n as prompted: [server]$ rm -i *.jpg rm: remove 'example1.jpg'? y rm: remove 'example2.jpg'? y [server]$

Locating files
To locate all of the files in a directory tree that contain some pattern in their name: [server]$ find directory -name  -print list of results [server]$ Note that this command uses a regular expression ( above) to describe the filename. You can also type in the exact filename.

For example, to find all files ending with  in the current directory and any subdirectories: [server]$ '''find. -name *.htm -print''' Note that searches containing wildcards ("*", ".","?") should be bounded by quotes so that the shell does not try to interpret them as regular expressions: [server]$ '''find. -name "*.htm" -print''' Further,  is the default on most Unix/Linux systems so it can be omitted in most instances: [server]$ '''find. -name "*.htm"''' The find command is extremely powerful. One other handy use is to delete all of the empty subfolders in a particular tree. For example: [server]$ '''find. -depth -type d -empty -exec rmdir {} '; Just make sure you don't leave off the -empty!

This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output: [server]$ '''find. -exec grep " some_string " '{}' \; -print'''

To find all the files that have been modified in the last 7 days and output them to a file: [server]$ '''find. -mtime -7 > mod.txt'''

Disk usage
The panel doesn't show disk usage for dedicated servers. You'll just need to SSH/telnet to your server and use. This will show you total, used, and available disk space. Adding the  flag displays the numbers in an easier to read format. [server]$ df -h Filesystem	      Size  Used Avail Use% Mounted on /dev/hda1             2.0G  1.1G  876M  55% / /dev/hda3            4.9G  2.7G  1.9G  58% /usr/local 10.3.19.71:/vol/pulpy/postal/babricus 239G 220G   19G  92% /home/.babricus 10.3.19.114:/vol/chippy/postal/abubu 239G 211G   28G  89% /home/.abubu 10.3.19.112:/vol/wetty/postal/cabernet 191G 169G   22G  89% /home/.cabernet... [server]$ Note that the output above was truncated. The actual output on DreamHost's servers can be quite a bit longer.

Your home directory and all your files are in  (not listed above).

In the example below, the home directory is listed, and you can see from this that 26% percent of a 30GB partition is being used. [server] df -h Filesystem	    1k-blocks	    Used Available Use% Mounted on /dev/hda1	       2071416	 1017516    948676  52% / /dev/hda3	      5162828	 1036076   3864492  22% /usr/local /dev/hda4	     30273148	 7413324  21322004  26% /home /dev/hdd1	     78786704	 6505004  68279540   9% /mnt/backup

If you're just interested in knowing how much space a directory and its contents are taking up, you can use this command instead: [server]$ du -sh directory_name 52M    directory_name

Users
To get login details: last

Unix tutorials

 * Learn UNIX in 10 Minutes Note: Not everything mentioned here can be used at Dreamhost.
 * UNIX Tools - Power Point Presentation Note: CPT247 College Course.
 * Unix Administration - Power Point Presentation Note: CPT248 College Course.