Overview

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

Working with directories

You can always discover what directory you are currently in using the pwd command. The following shows this user is in the /home/username/example.com directory:

[server]$ pwd

/home/username/example.com

[server]$

Creating directories

To create a new directory, use the mkdir command. The follow example creates a new directory named ‘directory_name’:

[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 rmdir command:

[server]$ rmdir directory_name
[server]$

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
[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 cd command. The following changes the directory used to /target/directory:

Delete files

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 as you can inadvertently delete files this way. As a safeguard, you can use the -i flag; you will then be asked to confirm all deletions. Type y or n as prompted to confirm whether or not you wish to delete the each file:

Locating files

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. Another 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 to include the -empty flag within the above command.

This command searches 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

To show the total disk usage for your VPS or dedicated server, use df. This will show you total, used, and available disk space. Adding the -h flag displays the numbers in a more readable format (KB, MB, GB, and so on).

Check if your files are correctly uploaded (check for file integrity)

If you need to upload some large files to the server, you might want to check that they are correctly uploaded with no errors. To do that, just create an MD5 file and upload it to the server, in the same folder as the files you want to check.

An MD5 file is a plain text file that contains checksums of your files so their integrity can be verified afterwards; to create it you can use any MD5 tool, like wxChecksums or the command line utility win a UNIX like system with openssl md5.

Once you upload the MD5 file, go to the directory that contains the files you want to verify (and where you the uploaded the MD5 file) and enter the following command: