UNIX Commands

ls command

The ls command

To display all information on the entries in the current directory, use:

ls -lF

By including the F option, the character at the end of the name will indicate the type of entry as follows:

(blank)
normal file or hard link
/
directory
*
executable file
@
symbolic link
=
socket
|
FIFO / pipe

To display all of the entries in one or more directories, use:

ls -lF dirpath ...

To display the attributes of a directory itself, use the d option:

ls -ld .
ls -ld dirpath ...

ls -ld . displays the attributes of the current directory.

mkdir command

How to create a directory

mkdir newdir
mkdir -p dirpath/newdir
mkdir -p /u##/dirpath/newdir

mkdir newdir creates the new directory under the current directory. -p will automatically create any intermediate directories that do not already exist in dirpath.

find command

Searching for file names beginning with a pattern

find dirpath -name pattern\* -print
find . -name pattern\* -print
find / -name pattern\* -print

. seaches the current directory and all directories below it. / searches all of the directories in the system. Since the shell expands wildcards, the * character must be escaped.

Searching for file names based on other criteria

find dirpath -mtime -days -size +blocks -print
find . -mtime -7 -size +200 -print
find . -mtime -7 -size +102400c -print

-mtime searches for files based on the last modification time of the file. +days matches files that were modified more than the specified number of days ago while -days matches files that have changed within the given number of days. Likewise, -size can be used to search for files based on their size in 512-byte blocks (default) or characters (c). The combination of these two can be useful for determining what used a lot of space recently, as in the last two examples above, where -mtime -7 -size +200 or -mtime -7 -size +102400 checks for files larger than 100K created within the past week.

Executing a command on each matching file name

find dirpath -name pattern\* -exec command \;

Include {} wherever the matching file names are to be positioned in the command. The ; must be escaped and separated from the command by a space.

For example, to list the attributes of each matched file:

find dirpath -name pattern\* -exec ls -ld {} \;
find dirpath -mtime -days -exec ls -ld {} \;

To list only the files containing a particluar string:

find dirpath -name pattern\* -exec grep -l string {} \;
ln command

Creating symbolic links to existing files

ln -sh filepath newpath
ln -sh pattern dirpath

filepath or pattern is the fully-qualified path to an existing file. newpath is the file name of the new link, or a directory where the link is to be placed. If multiple source files or a pattern is used, the new links will be created in the dirpath directory.

h prevents the ln command from following the target symbolic link if it already exists. From the command line, i can be used to display a prompt if the target file name already exists. In scripts, f can be used to replace the link if it already exists.

Last updated Saturday December 30, 2006


Printer-friendly PDF* format:

UNIX Commands

This Section

You are currently viewing this page in XHTML 1 Style Sheet* format (* see Clicklets for more infomation). This document is also available in XHTML 1*XML*HTML 4*HTML 5 Style Sheet*HTML 5 XML*HTML 5 non-XML* XHTML 2* XHTML Mobile* WML Mobile* and printer-friendly PDF* formats. This is accomplished with Single Source Publishing, a content management system that uses templates in XSLT style sheets provided by XML Styles .com to transform the source content for various content delivery channels. There is also RDF* metadata that describes the content of this document.