UNIX commands

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


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'''

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

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

[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 ../. If you are logged into your webserver via SSH, running the following command changes your directory to your user’s directory:

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) in vertical format, add the -la flags:

[server]$ ls -la
-rw-------  1 username pg######   10541 Mar 12 18:46 .bash_history
-rwxr-xr-x  1 username pg######   430 Dec 18 14:45 .bash_profile
-rw-r--r--  1 username pg######   237 Sep 16  2014 .bashrc
drwxr-xr-x  5 username pg######   4096 Sep  6  2011 Maildir

Directory sizes

To determine the size of a directory, use the du command with the -sh flags:

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

Add the -sh flag to see the size in a more readable format (KB, MB, GB, and so on). You can also list multiple directories and files separated 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 in the current working directory.

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

mv can also be used to move a file to a new directory while renaming it:

[server]$ mv oldfilename ../directory/newfilename

You can also rename a file while using the cp command:

[server]$ cp /existing/location/filename /new/location/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 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:

[server]$ rm -i *.jpg
rm: remove 'example1.jpg'? y
rm: remove 'example2.jpg'? y

To permanently remove a directory and all its contents, use the -rf flags:

[server]$rm -rf directoryname
Dh-kb-important-icon.fw.png Important: The rm -rf command completely deletes everything in that folder and there will be no way to recover the data. Be careful when using this command.

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. 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).

[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
                      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 /home 
Filesystem        	Size  Used Avail Use% Mounted on
/dev/hdv1         	30G  7.8G  22G  26% /


To get a history of recent login activity:

last <username>

Other common commands

Decompress a ZIP Archive

If you have uploaded a ZIP archive file to your user space, you can use the unzip command to decompress it:

[server]$ unzip filename.zip
Archive:  filename.zip
  inflating: list
  inflating: of
  inflating: files
  inflating: being
  inflating: uncompressed
  inflating: from
  inflating: the
  inflating: zip
  inflating: archive

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 in 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:

[server]$ md5sum -c yourfile.md5
list: OK
of: OK
uploaded: OK
files: OK
being: OK
verified: OK

If all of the files report OK, the upload was completely successful. If any files report FAILED, it was not.

Compress all files In a directory into a ZIP Archive

Let's imagine that you have a directory named ‘images’ and that you want all the files and subfolders (and files within subfolders etc…) to be zipped up in a single file.

zip -r images_backup images

This creates a file named ‘images_backup.zip’ in the current folder containing everything that was in the "images" folder.

You can use the maximum compression setting in tandem with this by using:

zip -9 -r images_backup images

See also