UNIX commands

From DreamHost
(Redirected from Unix commands)
Jump to: navigation, search

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 pwd command:

[server]$ pwd

Creating directories

To create a new directory, use the mkdir command:

[server]$ mkdir directory_name

Deleting directories

There are actually a few ways to delete directories in the shell. To delete an empty directory, use the rmdir command:

[server]$ rmdir directory_name

To delete a non-empty directory (one that still contains files or other directories in it) rmdir will not work. Here you have two choices. You can either remove all the contained files and directories by hand using the rmdir and rm commands, or you can use rm's -r flag.

[server]$ rm -r directory_name

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 cd command:

[server]$ cd /target/directory

A successful change will not return any messages.

To change to your previous directory, use the cd - command:

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

The user above has changed from one directory to another, then used the cd - 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 ls command:

[server]$ ls
Maildir    example.com    logs

Add the -l 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

To list all files within the directory including hidden files add the -a flag:

[server]$ ls -a
.                .bash_profile    example.com
..               .bashrc          logs
.bash_history    Maildir

ls sorting

Some of you might notice that the ls 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

This has to do with the $LANG setting allowing ls to sort based on your local language. To fix this and put it back to "normal" ls listing, unset your $LANG environment variable.

[server]$ unset LANG
[server]$ ls -a
.                .bash_profile   .vimrc           example.com
..               .bashrc         Maildir          logs
.bash_history    .gnome          TEMP

You can add the unset command into your .bashrc 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 du command:

[server]$ du example.com
1532    example.com

Add the -sh flag for more human readable format. You can also list multiple directories and files seperated by spaces.

Working with files

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 touch command:

[server]$ touch filename

This creates an empty file named filename.

Moving files

To move a file to another place:

[server]$ mv /old/location/filename /new/location/filename

Note that you can also use relative paths:

[server]$ mv filename ../directory/filename

Copying files

To copy a file to another place:

[server]$ cp /existing/location/filename /new/location/filename

Renaming files

mv is also used to rename a file to something else:

[server]$ mv oldfilename newfilename

Delete files

To delete a file:

[server]$ rm filename

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*

The wildcard can appear anywhere in the string. To delete all .jpg files:

[server]$ rm *.jpg

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

Locating files

To locate all of the files in a directory tree that contain some pattern in their name:

[server]$ find directory -name <regexp> -print

Note that this command uses a regular expression (<regexp> above) to describe the filename. You can also type in the exact filename.

For example, to find all files ending with htm 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, -print 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 df. This will show you total, used, and available disk space. Adding the -h 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
                      239G  220G   19G  92% /home/.babricus
                      239G  211G   28G  89% /home/.abubu
                      191G  169G   22G  89% /home/.cabernet...

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 /home (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


To get login details:

last <username>

External links

Unix tutorials