Reading and Writing Physical Files in a Shell using Rfile

Want to read source member data from within an IBM i shell environment, such as SSH, QShell, or QP2TERM? Need to copy records from a text file to a physical file? Want a quick way to upload a save file without needing FTP?

IBM i’s Rfile command can do all these things easily. Rfile is designed to be easy to integrate into scripts, so it’s a great tool to have at hand.

To try the examples below, first launch an IBM i PASE shell, such as by typing the QSH command or using SSH.

Reading and writing source file members

Rfile is a flexible command. It accepts numerous flags that change its behavior.

The -r (read) flag makes Rfile read a file and output its contents. As it runs, Rfile automatically performs CCSID conversion from EBCDIC to ASCII/UTF-8, and converts each record to a separate line.

By default, Rfile expects an IFS-style path to the source member, for example “/QSYS.LIB/QSYSINC.LIB/H.FILE/STDIO.MBR“. To make it take a traditional path specification, add the -Q flag. Note that you may need to wrap the path in quotes to avoid your shell from interpreting the path as syntax.

By default, Rfile outputs data to your terminal (through standard output). To make it write to a file instead, you can use shell syntax (the > operator) to redirect it to an IFS file.

The -w (write) flag makes Rfile write to the source file. By default, Rfile will get data from your terminal (through standard input), but you can use the opposite shell syntax (the < operator) to use an IFS file instead. As with reading, Rfile will perform CCSID conversion and treat each line as a record.

The -a (append) flag, when added to -w,  lets you append to the file, instead of overwriting it.

Reading and writing physical files

Physical files are handled by Rfile in the same manner as source files.

Rfile assumes a fixed format for text files generated from (or used to upload to) the physical file. All columns will be directly next to each other, like so:

Likewise, you can write to the physical file from an IFS text file that has fixed-format columns.

Reading and writing save files

So far, the commands we’ve been using have handled text files and database tables.

What if we needed to work with binary data, like save files? These shouldn’t have any CCSID conversion applied, or their contents will be mangled. Luckily, Rfile has the “-b” flag (binary processing), which will make Rfile read and write without any CCSID or line handling. While you can use binary mode to process physical and source files in their “raw” form, the main purpose is moving save files in and out of the IFS.

For example, you copy a save file object to the IFS with the following command:

You can also go in the other direction and copy a save file from the IFS to an object. (You may need to create the savefile first by using the CRTSAVF command.)

Rfile can handle many file conversions on IBM i

For more options, refer to the Rfile manual page or get in touch.

4 replies
  1. Glenn Gundermann
    Glenn Gundermann says:

    1. If I am writing to a file with input from the terminal, is there a way to signal the end of input other than pressing F3? When using DOS, we would use Ctrl-C or Break.
    e.g.
    Rfile -wQ “glennlib/qclsrc(temp)”

    2. How can I write to an IFS stream file with input from the terminal?
    I’ve tried the following command but get an error message: myIFSfile is not a device file or database member.
    Rfile -w myIFSfile.txt

    Reply
    • Calvin Buckley
      Calvin Buckley says:

      1. Control-D would be the way to signal end of file in a normal terminal, but I’m not sure with 5250 what the equivalent would be, if present. The 5250 based dumb terminal emulation is clumsy and I don’t really recommend using it if you can use SSH instead.

      2. Don’t use Rfile, you can use shell input redirection (i.e. cat > file.txt, ls > file.txt, etc.).

      Reply
  2. Russ Khoury
    Russ Khoury says:

    I use rfile to print text files built in the IFS by referencing a previously built printer file (PRTF). rfile -wQ mylib/myprtf < mytextdoc.txt
    The printed output looks fine.
    Thanks for your article.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.