Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

UNIX tips and tricks for a new user, Part 1: File maintenance tools

Tim McIntire, Consultant, Freelance Writer
Photo of Tim McIntire
Tim McIntire works as a consultant and co-founder of Cluster Corporation, a market leader in HPCC software, support, and consulting. He also contributes periodically to IBM developerWorks and Apple Developer Connection. Tim's research, conducted while leading the computer science effort at Scripps Institution of Oceanography's Digital Image Analysis Lab, has been published in a variety of journals, including Concurrency and Computation and IEEE Transactions on Geoscience and Remote Sensing. You can visit TimMcIntire.net to learn more.

Summary:  Systems administrators can use a number of programs to maintain files in a UNIX® system from the command line. In this tutorial, you'll experiment with commands, such as cd, cp, and tar, to navigate a UNIX file system from the command line and work with files and directories. The cd command changes directories, cp duplicates files or directories, and tar quickly groups files into an archive. You'll also learn how to deal with file permissions and perform simple input/output.

View more content in this series

Date:  26 Sep 2006
Level:  Intermediate PDF:  A4 and Letter (72 KB | 22 pages)Get Adobe® Reader®

Activity:  30588 views
Comments:  

The file system and file sizes

It's easy to learn how to deal with individual files and examine their size and content. You can use the same techniques to examine the contents of entire directories and the file system. Many new versions of UNIX can display this information in simple numbering formats, using letters to denote units.

df

df stands for Display Free disk space. Using it is as simple as typing df; you get information about the amount of disk space in each file system on your machine, the amount of space used, and the amount of space available. Most systems default to display the values in 512KB blocks, which is hard to read. Use -g to display information in gigabytes or -m to display information in megabytes. Some systems have a -h option, which stands for human-readable. This makes df use suffixes like G, M, and K, and it displays each number in three or fewer digits. Type this command:

$ df -h 
				

This is an example of output you might see on a simple server:

$ df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             7.9G  3.7G  3.9G  50% /
none                  3.9G     0  3.9G   0% /dev/shm
/dev/sda3              24G   20G  1.9G  92% /export
				


ls -lh

If your system has -h for df, you can also use it with ls. Type this command to see a detailed listing with easy to read file sizes:

 
$ ls -lh 
				


du

du is a third way to check file sizes, but it has the added advantage of summing up directory sizes. It can also be used with -h on some systems; otherwise, try using -k, which gives results in 1024-byte blocks. You can also use -s and a filename or wildcard to specify which directories and files you want to examine. Try this:

 
$ cd ~ 
$ du -sk * 
$ du -sh * 
				

This is an example of output you might see in a home directory:

 
$ du -sk * 
316468  OLD
637940  MyData1
571788  Code
12356364        Build
3224480 Hardened
$ du -sh * 
310M    OLD
623M    MyData1
559M    Code
12G     Build
3.1G    Hardened
				


/dev

The /dev directory holds special files called device files, which, among other things, are used to access disk drives on your system. To learn about the /dev directory, take another look at the output of df. This is different on every machine, but it's important to note that df shows results for each file system on your computer. Unlike Windows-based computers, each mounted file system is addressed from the system's root, which is denoted by a forward slash: /. This is different from systems that separate disks with drive letters, such as C, D, E, and so on.

In UNIX, it's common for SCSI (and SATA) disks to use device names, such as /dev/sda, /dev/sdb, /dev/sdc, and so on. A common device name for a CD-ROM drive is /dev/cdrom. These devices are mounted to directories so that they can be accessed without using the device name. Please consult the documentation for your flavor of UNIX to find out how devices on your system are labeled.


mount

Any device can be mounted at any location (any directory). For instance, it's common to mount a CD-ROM at /mnt/cdrom. Some UNIX-like operating systems (such as many versions of Linux and Mac OS) mount CD-ROMs automatically, but it's good to learn how to use the mount command either way. Insert a CD-ROM, and type the following commands:

 
$ mount -t iso9660 /dev/cdrom /mnt/cdrom
$ df
$ ls /mnt/cdrom
				

Note: This will work only if /dev/cdrom and /mnt/cdrom exist on your system. If so, you see in the output of the df command that the CD-ROM is now part of the file system. The ls command should show the contents of the CD-ROM drive you just mounted.


umount

To unmount a device, most UNIX-like operating systems use umount. The syntax is umount followed by the mount point. If your previous mount command succeeded, type the following:

 
$ umount /mnt/cdrom
$ df
$ ls /mnt/cdrom
				

Note: You must not be in the mounted file system for the device to unmount properly; otherwise, the system complains that the file system is busy. After a proper execution of umount, the df command no longer shows the CD-ROM drive in the file system, and the ls command shows that /mnt/cdrom is now empty (because nothing is mounted there -- it's a normal directory).

8 of 12 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Linux
ArticleID=162888
TutorialTitle=UNIX tips and tricks for a new user, Part 1: File maintenance tools
publish-date=09262006
author1-email=Tim.McIntire@gmail.com
author1-email-cc=