Common Unix Commands


ls list directory contents 
cd change working directory 
pwd print working directory 
mkdir create directory 
rmdir remove directory 
cp copy files 
mv move files 
rm delete files 
more view file contents 
chmod change file access permissions 
man view manual pages


ls

The ls command lists the contents of a directory. The most common form of the ls command is:
ls <directory_name>
 
Examples
ls Testdir1 list the contents of the sub-directory Testdir1
ls . list the contents of the current directory
ls list the contents of the current directory
ls .. list contents of the parent of the current directory
ls ~ list contents of home directory
ls /etc list contents of the directory /etc (absolute pathname specified)
ls ~houdini list contents of user houdini's home directory.

ls can list the contents of multiple directories, if used in the more general form:
ls <dir_name1> <dir_name2> <dir_name3> ...

In this form, ls prints the name of each directory, followed by the contents of the directory.

Command-line options

ls accepts a number of options that control how the output is presented to the user. Some of the more common options are listed below. Read the man page on ls for further details.

-a
Normally, ls does not list files or directories whose names start with a . (dot). The -a option causes all files and directories to be listed.

-l
The -l option lists the contents of a directory in the long format. Given below is an example of the output produced by ls with the -l option.

% ls -l /class/cis221

drwxrwxr-x   6 weide    cis22X-di   1024 Jan 23  1997 96au
drwxrwxr-x   3 long     cisfact     1024 Mar  4 14:05 97sp
drwxrwxr-x   5 weide    cisfact     1024 Jan 24  1997 97wi
-rwxr-x--x   1 pillai   cisgrad   129004 Nov 19  1996 Life.demo
-rw-rw-r--   1 pillai   cisgrad       76 Nov 19  1996 Life1.data
The output is organized into columns. The columns are (in the order in which they appear) - the file access permissions, number of links to the file, owner of the file, group for the file, file size, time of last modification and the name of the file.

The first field or column contains the file access permissions. The first character of this field is usually a "-" or a "d". A "d" means that the item is a directory; "-" means the item is a regular file. The interpretation of the remaining characters in this column depends, to some extent, on whether the item is a file or directory. The next nine characters indicate the access permissions. Of these, the first 3 denote the permissions for the owner of the file, the next 3 denote the permissions for the group members and the last 3 denote the permissions for everyone else.

"r" denotes read permission, "w" denotes write permission and "x" denotes execute permission. Read permission allows users to view the contents of a file and to copy it. Write permission allows users to modify the file. Execute permission allows users to run the file if it is executable.

In the sample output above, the owner of the file Life.demo has "r", "w" and "x" permissions, group members have "r" and "x" permissions but no "w" permission and others have only "x" permission. For the file Life1.data, the owner and group members have "r" and "w" permissions, while others have only "r" permission.

In the case of directories, permissions are interpreted in a slightly different way. Read permission allows users to list the contents of the directory. Write permission allows users to create files and sub-directories within the directory and execute permission allows users to search the directory. You will be able to access a file or directory using a pathname only if you have search permission on all intermediate directories in the path.

Access permissions can be changed by the owner using the chmod command.


cd

The cd command changes the current working directory to the specified directory. The syntax for the cd command is:

cd <directory_name>
 
Examples
cd Testdir1 change to the sub-directory Testdir1
cd .. change to the parent of the current directory
cd ~ change to the user's home directory
cd change to the user's home directory
cd /etc change to the directory /etc (absolute pathname specified)
cd ~houdini change to the user houdini's home directory.


pwd

pwd prints the absolute pathname of the current working directory.


mkdir

The mkdir command is used to create new directories. The syntax of the command is:
mkdir <new_directory>

Example: mkdir Testdir1 creates a new directory called Testdir1 as a subdirectory in the current working directory.


rmdir

The rmdir command is used to delete existing directories. The syntax of the command is:
rmdir <directory>

The directory being deleted must be empty.

Example: rmdir Testdir1 deletes the subdirectory Testdir1.


cp

The cp command is used to copy files. The most common form of this command is:

cp <src_file> <dest_file>

where <src_file> is the file to be copied and <dest_file> is the name of the copy being produced. Both <src_file> and <dest_file> can be pathnames. If <dest_file> exists, its contents are overwritten by the contents of <src_file>. If <dest_file> does not exist, it is created. <src_file> is not changed.

Example: cp /etc/motd motd_mycopy, creates a copy of the file /etc/motd in the current directory. The copy has the filename motd_mycopy.

Another common form of the cp command is:

cp <src_file> <dest_dir>

where <src_file> is as before and <dest_dir> is the directory into which it will be copied. If a file by the same name exists in <dest_dir>, its contents are overwritten. Otherwise, a new file with the same name as the source is created.

Example: cp /etc/motd ., creates a copy of the file /etc/motd in the current directory. The copy has the same name as the original i.e. motd.

cp can also be used to copy multiple files into a directory. For instance:

cp <src_file1> <src_file2> <src_file3> <dest_dir>

In this case, each of the source files is copied into the destination directory. This form of the command can be used to copy all files in one directory into another directory. For example:

Example: cp ~houdini/* ~, copies each file in user houdini's home directory into the current user's home directory. The copied files retain their names.

Another example: cp lab1.cpp lab2.cpp .., copies the files lab1.cpp and lab2.cpp in the current directory to the parent of the current directory.


mv

The mv command is used to move or rename files and directories. mv is used in the same forms as the cp command. mv does not create a copy of the source, but moves the source itself to the new location.

The common forms of usage are:

mv <src_file> <dest_file>
mv <src_file> <dest_dir>
mv <src_file1> <src_file2> <src_file3> <dest_dir>

mv is frequently used to rename files. For example: mv motd motd_new renames the file motd in the current directory to motd_new.

Actually, the source does not have to be a file; mv can also move directories. Thus the following are correct forms of usage:

mv <src_dir> <dest_dir>
mv <src_dir1> <src_file2> <src_dir3> <dest_dir>

Example: mv Testdir1 .., moves the directory Testdir1 from the current directory to the parent of the current directory.


rm

The rm command is used to delete files. The syntax of the command is:
rm <file>

Example: rm lab1.cpp deletes the file lab1.cpp.


more

more is used to view the contents of files. If the file fits one screen of the display, more displays the whole file. Otherwise, more displays one screen of data at a time and then pauses. At the pause, the user can press enter to advance the data one line at a time, or the space key to advance the data a full screen. To quit more, just press 'q'.

It's worth noting that more does not allow the user to go backwards in the file. For this reason, many users prefer the command less instead. It behaves similarly to more, but allows the user to scroll back up through data that have already been seen. The added functionality of less is classic evidence for the belief that "less is more" :-)


chmod

File access permissions can be manipulated using the chmod command. Unix maintains three sets of access permisssions for each file. It maintains separate permissions for :
  1. the owner
  2. the group
  3. others (everyone else)
File permissions can be set using the chmod command as shown below:

chmod <who>=<mode> <file>

where <who> denotes which class of permissions is being set, <mode> is the new set of permissions, and <file> is the name of the file or directory for which the permissions are being set.

<who> can be any combination of "u", "g" or "o" representing user (owner), group and other, respectively.

<mode> can be any combination of "r", "w" and "x" representing read, write and execute permissions, respectively.

For example: chmod g=rx lab1.cpp, sets the access permissions for the group members for file lab1.cpp to read and execute, but not write.
 
Examples
chmod u=rwx lab1.cpp for file lab1.cpp, set owner's access permissions to read, write and execute 
chmod ug=rw lab1.cpp owner and group members are given read and write permissions
chmod o=rx lab1.cpp read and execute permissions for others

The following two commands are equivalent:
chmod ugo=rw lab1.cpp
chmod a=rw lab1.cpp

In other words, if the desired access modes (permissions) for user, group and other are the same, you can use "a" (for all) instead of "ugo".

Permissions can be added using the chmod command as shown below:
chmod <who>+<mode> <file>

Existing permissions can be removed using the form of the chmod command shown below:
chmod <who>-<mode> <file>
 
Examples
chmod ug+x lab1.cpp for file lab1.cpp, add execute permission to the set of permissions for the owner and group 
chmod o-w lab1.cpp remove write from the set of permissions for others


man

man allows you to read the online manual pages for Unix commands and application programs. The common form of the command is:
man <command_name>

Example: man mkdir lists the manual page for the command mkdir.

This form of the command is useful when the user already knows the command that is needed but wants to get more detailed information on the usage of the command.

man uses more to display the manual page.

Command-line options

-k
The -k option can be used when the exact command that is needed is not known. The syntax is:
man -k <keyword>

Example: man -k directories will result in man printing out a list of commands that deal with directory manipulations.

With the -k option, man lists all commands that are related to the specified keyword and a one line description of each command. 



 
Previous Table of Contents Continue


Last modified: Tue Jan 6 20:47:09 EST 1998