UNIX: Viewing the Contents of Directories

UNIX logo

You will frequently need to move up and down the directory structure to work with specific files or other directories. UNIX has many commands that enable you to do so.

Command: pwd

What it does: Shows you the full path to your current location

Use the pwd command when you aren’t sure about the directory in which you are currently located. When you use the command, you will see the full path in which you are working. This can be helpful as you move around the directories if you become confused about where you are.

Command: cd pathname

What it does: Changes your directory location to the one in the path pathname

When a specific command is listed in a step, you should ignore the period at the end of the command. For example, in the following steps, don’t type the period after the command cd music in Step 1.

  1. Type cd Music. The prompt will change to [localhost:~/Music] to indicate that you are in the Music directory in your Home directory.

Remember that the ~ represents your Home directory, so ~/Music means that you are in the Music directory that is within your Home directory. This can help you take some shortcuts when entering paths as you will see in the next step.

Also remember that the forward slash (/) in a path indicates a change in level in the hierarchy. If you are in your Home directory and type cd /Music, you will get a message telling you that no such directory exists. When you enter the forward slash, Unix looks back to the highest level in the structure and there is a directory called Music in that directory. Leaving the / out indicates that Unix should look in the current directory, which is where the directory is actually located.

  1. Type cd /Users/shortusername, where shortusername is the short username for your account. This will move you back into your Home directory. You include the /User/ because you are moving above the Music directory and so need to include the full pathname.

In a pathname, the tilde character (~) indicates that you are in your Home directory. In the previous step, you could have just entered cd ~ to move back into your Home directory.

Command: ls

Options: -F differentiates between files and directories; -l shows full information for all the files in the directory

What it does: Lists the contents of a directory in various formats and with various information

Note that while most commands and options are in lowercase, they aren’t always. For example, the -F option is different from the -f option (both are valid for the ls command).

  1. Use the cd command to move into the directory of which you want to see the contents.
  2. Type ls. You will see a multiple-column view of the directory; files and directories are listed by name.
  3. Type ls -F. You will see the same list as before except that now directories are indicated by a / after their names.
  4. Type ls -l. You will see the contents of the directory listed along with plenty of information about each file and directory within the current directory. If there are many items, the information will scroll so quickly that you might not be able to see all of it. This is a good opportunity to see an example of piping two commands together.
  5. Type ls -l | more. This time, the same list will appear, but the display will stop when the screen is full and you will see the more prompt at the bottom of the window. Press the spacebar to see the next screenful of information. You can also scroll the window using the scrollbars to see all the items in the window.

If you type the command ls –la, you will also see the invisible files in a directory.

The permissions string you see at the start of each item in the full listing indicates how the item can be accessed. The first character indicates whether the item is a file (-) or a directory (d). The next three characters indicate what the owner of the file can do; r is for read, w is for write, and x is for execute. If any of these characters is the hyphen (-), that action can’t be taken. The next three characters indicate the permission that the group has to the file. For example, if these characters are r-x, that would mean that other members of the group can read, not write, and execute the file. The last three characters indicate what everyone else can do.

The execute permission applies to a directory. In order to access a directory, you must have both read and execute permission. If you also have w permission, you can change the contents of the directory as well.

Command: file filename

What it does: Indicates what kind of file filename is

Type file, followed by the filename you would like information about, and press Return. Information about the file will be displayed.

http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/dzone_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/blinklist_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/blogmarks_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/magnolia_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/mixx_48.png http://www.techbuzzing.com/wp-content/plugins/sociofluid/images/twitter_48.png

Related Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)