Common UNIX Commands - for files and directories

October 31, 2008


 

Here's a concise list of UNIX/Linux commands that most users should know. These commands are the essentials dealing mostly with files and directories.

File and directory names can be up to 256 characters long. They may contain upper and lower case characters (including letters, numbers, punctuation, and other symbols).

Note

Try to avoid using characters like "/", ">", "!", "?" or blank spaces in filenames, as they are sometimes hard to manipulate because of their special designations within the shell.

In UNIX, these are case sensitive. For instance, the name "Myxztplk.txt" refers to a file different file from "myxztplk.txt." The file hierarchy starts from the root (or "/") and makes its way down to the particular file name on the path. The full name for any file is the complete path to it, starting at the root, which means all the directories to it. The path name for the file "myxztplk.txt" in the directory "villains" in the directory "superman" which is under the root directory would be: /superman/villains/myxztplk.txt

Use the pwd command to list the name of your p rint w orking d irectory. You may refer to any files in your present directory without using their full path names. Often, you will not have a UNIX account that begins at the root directory. Most UNIX platforms have many users, and each user has a small slice of the available hard drive space. Thus, instead of starting out at the root of the drive, most user accounts will be somewhere down in the directory tree. [4]

Some common commands to help you interact with the filesystem and your files are:

ls (list): List directory contents

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td>ls</td> <td>lists the contents in a directory in columns</td> </tr> <tr> <td>ls -l</td> <td>gives a longer/fuller listing including file permissions, size, date created (the "l" is for long)</td> </tr> <tr> <td>ls -la</td> <td>similar to the above but includes "dot"/hidden files (the "a" is for all)</td> </tr> </tbody> </table>

cd (change directory): Move to a different directory

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">cd</td> <td align="left">returns you to your home directory</td> </tr> <tr> <td align="left">cd ../</td> <td align="left">moves up one directory level</td> </tr> <tr> <td align="left">cd ../..</td> <td align="left">moves up two directory levels</td> </tr> <tr> <td align="left">cd mysubdirectory</td> <td align="left">moves to the subdirectory mysubdirectory</td> </tr> </tbody> </table>

cp (copy): Copy files and directories

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">cp file1.txt file2.txt</td> <td align="left">copies file1.txt to file2.txt</td> </tr> <tr> <td align="left">cp file1.txt mydirectory/</td> <td align="left">copies file1.txt to named mydirectory</td> </tr> <tr> <td align="left">cp file1 file2.txt mydirectory/</td> <td align="left">copies file1.txt and file2.txt to named mydirectory</td> </tr> </tbody> </table>

mv (move): Move or rename files or directories

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">mv file1.txt newname.txt</td> <td align="left">renames file1.txt to newname.txt</td> </tr> <tr> <td align="left">mv mydirectory newname</td> <td align="left">renames directory to newname</td> </tr> <tr> <td align="left">mv file1.txt directory/</td> <td align="left">moves file1.txt to named directory</td> </tr> <tr> <td align="left">mv file1.txt directory/newname.txt</td> <td align="left">moves file1.txt to named directory and renames it to newname.txt</td> </tr> </tbody> </table>

rm (remove): Delete files or directories

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">rm file1.txt</td> <td align="left">removes (deletes) file1.txt</td> </tr> <tr> <td align="left">rm -i file1.txt</td> <td align="left">deletes named file1.txt after prompting to make sure you wish to remove it (the "i" is for interactive)</td> </tr> </tbody> </table>

Caution

BE EXTREMELY CAREFUL with rm . There is no undelete command!

mkdir (make directory): Create directories

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">mkdir mysubdirectory</td> <td align="left">creates a directory within the current directory called mysubdirectory</td> </tr> </tbody> </table>

rmdir (remove directory): Delete directories

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">rmdir mysubdirectory</td> <td align="left">removes (deletes) directory mysubdirectory within the current directory (if mysubdirectory is empty i.e. has no files in it)</td> </tr> </tbody> </table>

chmod (change mode): Change file or directory access permissions

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">chmod u+w file1.txt</td> <td align="left">adds write permission for the user to file1.txt ("write" is the ability to edit or delete the file)</td> </tr> <tr> <td align="left">chmod g+x file1.txt</td> <td align="left">adds execute permission for the group to file1.txt</td> </tr> <tr> <td align="left">chmod o-r file1.txt</td> <td align="left">removes read permission for the world (other) from file1.txt</td> </tr> </tbody> </table>

The chmod command assists in allowing you to share files with another user or group or to restrict access ("lock down") directories of files to keep things private. The document [ http://help.unc.edu/?id=217 ] How to Use UNIX and Linux File Permissions explains this in detail.

Note

Since UNC users' file space is sometimes in AFS (your home directory or any directory that starts with "/afs/") , [ http://help.unc.edu/?id=215 ] AFS ACLs (Access Control Lists) control directory access permissions as well as basic UNIX file permissions.

more: List/view files one screen at a time

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">more file1.txt</td> <td align="left">scrolls forward through file1.txt one screen at a time. Press spacebar to go forward one screen, b to go back one screen, or q to quit </td> </tr> </tbody> </table>

pwd (print working directory): Display path name of current directory

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">pwd</td> <td align="left">displays your current directory</td> </tr> </tbody> </table>

cat: Concatenate files and list them to the screen.

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">cat file1.txt</td> <td align="left">prints the contents of file1.txt to your screen</td> </tr> <tr> <td align="left">cat file1.txt file2.txt</td> <td align="left">prints the contents of file1.txt and file2.txt to your screen</td> </tr> <tr> <td align="left">cat file1.txt file2.txt > file3.txt</td> <td align="left">concatenates the contents of file1.txt and file2.txt into file3.txt</td> </tr> </tbody> </table>

diff: Compare two files and shows where they differ

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">diff file1.txt file2.txt</td> <td align="left">lists the differences between the files file1.txt and file2.txt</td> </tr> </tbody> </table>

wc: Count characters, words or lines

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">wc -c file1.txt</td> <td align="left">prints the number of bytes in file1.txt</td> </tr> <tr> <td align="left">wc -l file1.txt</td> <td align="left">prints how many lines are in file1.txt</td> </tr> </tbody> </table>

who: List users logged in the machine

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">who</td> <td align="left">lists users who are logged in the machine</td> </tr> <tr> <td align="left">who -m</td> <td align="left">shows you what the ip address of the machine you logged in from</td> </tr> </tbody> </table>

ps: Find out what jobs you are running and what shell you are in

<table border="1" width="95%"> <colgroup> <col /> <col /> </colgroup> <tbody> <tr> <td align="left">ps</td> <td align="left">lists your current running jobs and your shell you are in</td> </tr> <tr> <td align="left">ps -ef</td> <td align="left">lists jobs of others as well as yours</td> </tr> </tbody> </table>

As can be seen, all of these commands have optional flags and many take arguments, all of which can be found in the man pages.

This article is an adaptation of content published at UNIX/Linux: Getting Started and is published here under a Creative Commons Attribution 3.0 License.


No comments

Add comment

* - required field

*

CAPTCHA image for SPAM prevention
If you can't read the word, click here.
*
*
 

Netdip.com is an excellent web site that's powered by TYPO3 and other great open source software. Netdip.com is also a fat free alternative to ice cream.