What is a file?
There is a lot you can learn about files on IBM® AIX® , but first things first. What is a file anyway?
A file is a collection of data that can be read from or written to. A file can be a program you create, text you write, data you acquire, or a device you use. That definition comes from the AIX 7.1 Information Center (see Resources). A file can be anything from a directory to a shell script, a compiled program, an operating system executable, a device, or a database index file (just to name a few).
This article looks at some of the commands that tell you about the content of files and, just as importantly, their context.
List files using the
The first thing you usually know about a file is what it is called. The
ls command lists the files in your current directory by name. Listing 1 shows the output of the
ls command from the /home/ibmdw directory.
Listing 1. ls command example
# ls backup.data image.info readme sample.txt bin myscript.sh reports
The files listed all use the allowable characters for AIX file names. (For information on file-naming conventions, see Resources.) Knowing the names of files is a start, but most people prefer a listing with more details. You can get a long listing with the
ls -l command. This gives you more information, including:
- When the file's contents were last changed
- File permissions that show who owns it, what group it belongs to, and who is allowed to read it, update it, or run it
- The size of the file
You can see an example of a long listing using
ls -l in Listing 2. I added a header line for this example, but it will not appear as part of your normal command.
ls -l command
ls -l command $ ls -l total 608 mode links owner group size modified file name -rw-r--r-- 1 root system 187972 Nov 24 05:34 backup.data drwxr-xr-x 2 ibmdw staff 256 Nov 24 05:29 bin -rw-r--r-- 1 root system 11 Nov 24 05:34 image.info -r-xr--r-- 1 ibmdw staff 109499 Nov 24 05:29 myscript.sh -rw-r--r-- 1ibmdw staff 49 Nov 24 05:32 readme drwxr-xr-x 2 ibmdw staff 256 Nov 24 05:30 reports -rw-r--r-- 1 root system 2550 Nov 24 05:33 sample.txt
In this listing, there is one line for each file. The first character in the left column shows the type of file. The two most common file types are
d, to indicate a directory, and
-, to show an ordinary file. You then see the file mode (an essential part of file permissions), the number of links, and the owner and group that the file belongs to. After that, you see the file size, in bytes, followed by the date and time when the file was last modified. Finally, there is the file name.
The file-permissions information is beyond the scope of this article, but the Resources section shows where you can go for a good explanation.
Many more options are available when you run the
ls command. Table 1 describes some of the common ones.
Table 1. Common flags you can use with the
|Allows you to see the hidden files that start with a dot. That includes files such as .profile, which sets some user variables at login time.|
Note: You can also see this information if you use the
|Sorts the files in order of their modification dates.|
|Reverses the sort order.|
ls command is helpful for providing basic information, but there is a lot more you can learn about a file than what the
ls command reports.
A file's name can indicate what type of file it is. For instance, shell scripts often end in .sh, and configuration files might end in .cfg or .ini. You can often tell what the purpose of a file is by looking at its name and extension (if it has an extension). Table 2 shows some examples.
Table 2. Examples of how file names relate to file type
|startnode.sh||Shell script to start WebSphere node|
|/etc/filesystems||List of file systems|
|Java14.sdk.tar.gz||Java 1.4.2 Software Developer Kit tar file (compressed to gzip)|
As you can see, the name of the file sometimes gives you an indication of what the file might be used for, but it is not an infallible rule. Because AIX is flexible with the type of file names you can use, there is no guarantee that you can tell the file type just from its name.
There are other pointers to what a file is used for, such as the directory name or the file system that the file belongs to. If you have a file system called /db_dat and another called /db_idx, there is a good chance that they are for database data files and index files, respectively.
Some commands help you see the type of file you are dealing with. The
file command tries to classify the file into one of the following categories:
- ar archive library
- block special
- C-language source
- character special
- cpio archive
- FORTRAN source
- first in, first out (FIFO)
- shell script (commands text)
- symbolic link
- tar archive
file command is especially important if you plan to view or edit a file. When you know what kind of file it is, you can determine the tools you need to see its contents.
File systems, directories, and filesets
Knowing a file's name and type is helpful, but a file does not stand on its own. It has relationships with other files. A file sits in a file system and belongs somewhere in a directory tree. Some files also belong to AIX filesets. This section provides some commands that help you see where a file sits in relation to other files.
Which file system?
If you want to find out which file system a file belongs to, looking down the list of file systems produced by the
df command can sometimes be misleading. You might think that the /usr/tmp/hostmibd.log file belongs in /usr, but in fact it belongs to /var. As it turns out, the /usr/tmp parent directory is a symbolic link to /var/tmp, as Listing 3 shows.
Listing 3. List directory with symbolic link
# ls -ld tmp lrwxrwxrwx 1 bin bin 8 Sep 13 2010 tmp -> /var/tmp
So, if that file starts growing, it uses free space in /var, not /usr. Fortunately, you can let AIX figure this out for you by using the
df command and adding the file name as an argument, as shown in Listing 4.
Listing 4. Using the
df command with file name as argument
# cd /usr/tmp # df aixmibd.log Filesystem 512-blocks Free %Used I used %Iused Mounted on /dev/hd9var 2097152 1237000 42% 17545 11% /var
This is especially useful if you have symbolic links or a lot of nested file systems. It is also helpful if you are using a script and need to identify the file system that a file belongs to.
Files belong to a directory path that you can trace back to the root directory (/). For example, the file called myfile might belong to /a/very/long/directory/path.
Sometimes it is helpful to be able to display the file name without the full path. To do that, you can use the
basename command shown in Listing 5.
Listing 5. A
basename command example
# basename /a/very/long/directory/path/myfile myfile
If you want to remove the basename and just keep the rest up to the final slash (/), use the
dirname command shown in Listing 6.
Listing 6. A
dirname command example
# dirname /a/very/long/directory/path/myfile /a/very/long/directory/path
These commands are generally more helpful in a script when you want to split up a string. You can store the result in a variable and then run
basename on that string.
Many files are part of an AIX fileset. You can determine which fileset a file belongs to by using the
lslpp -Jw command followed by the file name with its full path. Listing 7 shows that the
mksysb command (the /usr/bin/mksysb file) belongs to the bos.sysmgt.sysbr fileset.
Listing 7. Display the fileset that a file belongs to
File Fileset Type ----------------------------------------------------------------- /usr/sbin/installp bos.rte.install File /usr/clvm/sbin/linstallpv prpq.clvm File /usr/lpp/bos.sysmgt/nim/methods/c_installp bos.sysmgt.nim.client File
You can also use the fileset as the starting point and list the files that belong to it. You do this using
lslpp -f followed by the fileset name. Listing 8 is an example.
Listing 8. List files included in a fileset
lslpp -f bos.sysmgt.sysbr Fileset File Path: /usr/lib/objrepos bos.sysmgt.sysbr 220.127.116.11 /usr/lpp/bosinst/iscsi /objrepos/sm_name_hdr /usr/lib/bootpkg/18.104.22.168-spotpkg.tar /usr/lpp/bosinst/iscsi /usr/lib/bootpkg/22.214.171.124-spotpkg.tar /usr/lib/bootpkg/126.96.36.199-spotpkg.tar /usr/lpp/bosinst/iscsi/objrepos/sm_cmd_hdr /usr/lpp/bos.sysmgt/mkcd.README.txt /usr/sbin/bootpkg /usr/samples/oem_cdwriters/burn_cd_jodian /usr/lpp/bosinst/cdfs.optional.list /usr/sbin/mkinsttape /usr/bin/mkszfile /usr/lib/bootpkg/188.8.131.52-spotpkg.tar /usr/lpp/bosinst/tape /usr/lpp/bosinst/image.template /usr/lpp/bosinst/CheckSize /usr/lpp/bosinst/BosMenus /usr/lpp/bosinst/bi_main /usr/lpp/bosinst/bosinst.template.README /usr/lpp/bosinst/bosinst.template /usr/lpp/bosinst/Get_RVG_Disks /usr/lpp/bos.sysmgt/mkcd.README.html /usr/lpp/bosinst/iscsi/iscsi_cfg /usr/lib/bootpkg/184.108.40.206-spotpkg.tar /usr/lib/bootpkg/220.127.116.11-spotpkg.tar /usr/lib/bootpkg/bootpkg_list /usr/lpp/bosinst/iscsi/objrepos/sm_menu_opt /usr/lpp/bosinst/post_vgsync /usr/samples/oem_cdwriters / usr/sbin/mkdvd -> /usr/sbin/mkcd /usr/lpp/bosinst/cdeject /usr/lpp/bosinst/iscsi/objrepos /usr/lpp/bosinst/iscsi/objrepos/sm_cmd_hdr.vc /usr/lib/bootpkg /usr/sbin/mkrr_fs -> /usr/samples/oem_cdwriters/mkrr_fs_gnu /usr/bin/restwparfiles -> /usr/bin/listvgbackup /usr/bin/restvg /usr/bin/listvgbackup /usr/lib/bootpkg/18.104.22.168-spotpkg.tar /usr/samples/oem_cdwriters/burn_cd_gnu_dvdram /usr/samples/oem_cdwriters/burn_cd_youngminds /usr/lpp/bos.sysmgt/README.oem_cdwriters -> /usr/lpp/bos.sysmgt/mkcd.README.txt /usr/lpp/bosinst/iscsi/objrepos/sm_cmd_opt /usr/lpp/bosinst/iscsi/iscsi_smit /usr/bin/lsmksysb -> /usr/bin/listvgbackup /usr/lib/bootpkg/22.214.171.124-spotpkg.tar /usr/bin/lssavevg -> /usr/bin/listvgbackup /usr/lib/bootpkg/126.96.36.199-spotpkg.tar /usr/samples/oem_cdwriters/mkrr_fs_gnu /usr/lpp/bosinst/cdfs.required.list /usr/bin/savewpar -> /usr/bin/mksysb /usr/bin/mksysb /usr/lpp/bos.sysmgt/README /usr/sbin/burn_cd -> /usr/samples/oem_cdwriters/burn_cd_gnu /usr/bin/mkvgdata -> /usr/bin/mkszfile /usr/bin/savevg -> /usr/bin/mksysb /usr/samples/oem_cdwriters/burn_cd_gnu /usr/lpp/bosinst/cmdbsys_lib /usr/lpp/bosinst/iscsi/objrepos/sm_name_hdr.vc /usr/lpp/bosinst/adjfs /usr/lib/bootpkg/188.8.131.52-spotpkg.tar /usr/lpp/bosinst/iscsi/objrepos/sm_cmd_opt.vc /usr/samples/oem_cdwriters/mkrr_fs_youngminds /usr/lpp/bosinst/tape/tapefiles1 /usr/bin/mkwpardata -> /usr/bin/mkszfile /usr/lpp/bosinst/iscsi/objrepos/sm_menu_opt.vc /usr/bin/restorevgfiles -> /usr/bin/listvgbackup /usr/lpp/bosinst/cdboot /usr/lpp/bosinst/bicheck /usr/bin/lssavewpar -> /usr/bin/listvgbackup /usr/sbin/restwpar /usr/lib/bootpkg/184.108.40.206-spotpkg.tar /usr/sbin/mkcd /usr/samples/oem_cdwriters/mkrr_fs_jodian /usr/lpp/bosinst/bicfgsup /usr/lpp/bosinst/cdfs.optional.B.list /usr/lib/bootpkg/220.127.116.11-spotpkg.tar Path: /etc/objrepos bos.sysmgt.sysbr 18.104.22.168 /etc/preserve.list
Every file on your system is unique. Even if you make a copy of a file, that copy is a different file from the original. The contents might be the same, but the file is still unique. How is that so?
A file is made up of more than its contents. Even the file name does not make it unique because you can have two files with the same name, as long as they are in different directories. So what is it that makes one file different from another? To answer that question, you can look at the file's inode. The inode is a kind of table of contents. Each file has an inode number, and that number is unique to that file within the file system.
You can examine the file's inode information with the
istat command, which shows you the file's inode number and the device where the file resides. The inode also shows the file's last access time, permissions, and extended permissions. (For more information about extended permissions and access control lists, or ACLs, see Resources.) Listing 9 shows an example of the output of the
Listing 9. An
istat command example
# istat lspartition Inode 321985 on device 10/9 File Protection: rwxr-xr-x Owner: 2(bin) Group: 2(bin) Link count: 1 Length 19884 bytes Last updated: Thu Nov 17 10:19:59 EST 2011 Last modified: Wed Jul 20 01:06:07 EST 2011 Last accessed: Wed Jul 20 06:02:35 EST 2011
You can see three date and timestamp fields in the output of the
istat command. Table 3 lists and describes these fields.
Table 3. Date and timestamp fields in the
istat command output
|Last updated||Shows the date and time of the last inode update. This field is updated when a file is renamed, its permissions are changed, or other changes are made to the file table of contents.|
|Last modified||Shows the date and time of the last change to the file itself, rather than just to the inode.|
|Last accessed||Shows the date and time the file was last opened, even if no updates were made to the file.|
You can use any of these timestamps to search for files using the
find command. The
find command uses flags to allow you to narrow your search to files that have been accessed, modified, or had their inodes updated within a certain time period. For example, by calling
-mtime (modification time), you can locate files that have been changed within a specific number of 24-hour periods. Listing 10 shows how to list all files that have been changed in the last three days, starting from the current directory.
Listing 10. Find files by their modification time in days
find . -mtime -3
Similarly, you can search for files that have been modified, updated, or accessed within a specific number of minutes. Listing 11 shows how to find files that have been accessed within the last 10 minutes.
Listing 11. Find files by their modification time in minutes
find . -amin -10
The AIX command documentation for the
find command explains these options in more detail (see Resources).
To display the size of a file, use the
du command. This is easier than converting the output of the
ls -l command. For more details on how to display the size of a file, see Resources.
If the file is a text file, you can view its contents (provided you have read permissions on the file). There are many commands that help you see part or all of an American Standard Code for Information Interchange (ASCII) file. Table 4 lists and describes some of those commands.
Table 4. Commands for viewing ASCII file contents
|Shows the file one page at a time|
|Shows the first few lines of the file|
|Shows the last few lines of the file. If the file is still being updated, you can see a continuous view by using |
|Shows the file contents in a vi editor session in read-only mode|
Even if a file is not in ASCII format, you can view the printable strings in the file by using the
To see if a file is in use by a process, use the
fuser command. You can list the name of the user who owns the process by using
fuser -u. If you are confident that the process can be terminated, you can stop it by using
More file information
Many other commands can inform you about files and help you manage them. There are commands to delete, copy, move, or rename files. You can also use commands to display file contents or find text strings within files. You can count the characters, lines, or words in a file. You can compare files and display the differences. Table 5 lists a few of the most helpful commands for managing files. Documentation on all of these commands is available in the AIX Information Center (see Resources).
Table 5. Other commands for managing files
|cat||Concatenates or displays files|
|cmp||Compares two files|
|colrm||Extracts columns from a file|
|cut||Writes out selected bytes, characters, or fields from each line of a file|
|devnm||Shows the device a file resides on|
|diff, bdiff||Compares text files|
|diff3||Compares three files|
|dircmp||Compares two directories|
|ff||Lists the paths of all files in a file system|
|grep||Searches a file for a pattern|
|nl||Numbers the lines in a file|
|rm||Removes (unlinks) files or directories|
|paste||Merges the lines of several files or subsequent lines in one file|
|sdiff||Provides a side-by-side comparison of files|
|sort||Sorts files, merges files that are already sorted, and checks files to determine if they have been sorted|
|sum||Gets a checksum and block count of a file|
|uniq||Reports or deletes repeated lines in a file|
|wc||Counts the number of lines, words, and bytes in a file|
|which||Locates a program file, including aliases and paths|
This article showed you some of the AIX commands that you can use for working with files, but there are many more. In fact, every command works with files in one way or another. As you can see, it is beneficial to revisit the command documentation from time to time because there might be new flags or commands available. When you make good use of the available commands, your system administration work becomes a lot easier.
- You can find documentation about AIX commands in the AIX 7.1 Information Center.
- The name of each file must be unique within the directory where it is stored. To find out about file names, see File-Naming Conventions in the AIX 7.1 Information Center.
- For information about file permissions, see the AIX 7.1 Information Center page about File ownership and user groups.
- For a more in-depth approach to file permissions, including lots of practical examples, see the article Speaking UNIX, Part 4: UNIX ownership and permissions provide for privacy and participation (developerWorks, October 2006).
- Using file access control lists (developerWorks, May 2011) explains how to use extended permissions on files and directories beyond the normal standard permissions as found with the
- The How big is that file? blog entry on Power IT Pro explains several different ways to check the size of a file.
- Follow developerWorks on Twitter.
Get products and technologies
- Evaluate IBM products in the way that suits you best: Download a product trial, try a product online, use a product in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement Service Oriented Architecture efficiently.
- Get involved in the developerWorks Community. Connect with other developerWorks users when exploring the developer-driven blogs, forums, groups, and wikis.