Seiden Group
Modern Development & Open Source for IBM i
  • Link to LinkedIn
  • Link to Mail
  • Home
  • Seiden PHP+
    • Seiden PHP+
    • Install & Learn
    • SmartSupport
    • PHP Migrations & Upgrades
    • Success Stories
    • Free PHP Assessment
    • Documentation
    • What’s New (Changelog)
  • IBM i Services
    • Development
    • Training & Mentoring
    • Open Source Setup & Upgrades
    • SSL/TLS Install & Learn
    • Performance
  • Support
    • Open Source & PHP
    • VS Code for i
    • Developer Support
    • Support Success Stories
  • VS Code for i
    • Support
    • Training
    • Code for i Resource Guide
    • Getting Started Videos
    • Code for IBM i Fridays
  • Free Stuff
    • IBM i Strategy & Tips
    • PHP Upgrade Assessment
    • CNX Valence Assessment
    • VS Code for IBM i Resources
    • Code for IBM i Fridays
    • QCachegrind Download
    • PHP Toolkit for IBM i Resources
    • Qshell on i Library
  • Blog
  • About
    • About Our Team
    • About Alan Seiden
    • Speakers & Sessions
    • In the News
  • Contact
  • 201.447.2437
  • Search
  • Menu Menu

Seiden Group Blog

Reading and Writing Physical Files in a Shell using Rfile

February 25, 2021/4 Comments/in IBM i /by Calvin Buckley

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.

1
Rfile -r /QSYS.LIB/QSYSINC.LIB/H.FILE/STDIO.MBR

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.

1
Rfile -rQ "QSYSINC/H(STDIO)"

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.

PHP
1
Rfile -rQ "QSYSINC/H(STDIO)" > stdio.h

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.

PHP
1
Rfile -wQ "MYPROJ/QCSRC(MAIN)" < main.c

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

PHP
1
Rfile -waQ "MYPROJ/QCSRC(MAIN)" < main.c

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ Rfile -r /QSYS.LIB/QIWS.LIB/QCUSTCDT.FILE/CUSTCDT.MBR
938472Henning G K4859 Elm Ave DallasTX7521750003003700000000
839283Jones   B D21B NW 135 StClay  NY1304104001010000000000
392859Vine    S SPO Box 79    BrotonVT0504607001043900000000
938485Johnson J A3 Alpine Way Helen GA3054599992398750003350
397267Tyron   W E13 Myrtle Dr HectorNY1484110001000000000000
389572Stevens K L208 Snow PassDenverCO8022604001005875000150
846283Alison  J S787 Lake Dr  Isle  MN5634250003001000000000
475938Doe     J W59 Archer Rd SutterCA9568507002025000010000
693829Thomas  A N3 Dove CircleCasperWY8260999992000000000000
593029WilliamsE D485 SE 2 Ave DallasTX7521802001002500000000
192837Lee     F L5963 Oak St  HectorNY1484107002048950000050
583990Abraham M T392 Mill St  Isle  MN5634299993050000000000

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

PHP
1
Rfile -w /QSYS.LIB/MYPROG.LIB/PF.FILE/TABLE.MBR < records.txt

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:

PHP
1
Rfile -rbQ MYPROG/BACKUP > backup.savf

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.)

PHP
1
Rfile -wbQ MYPROG/BACKUP < backup.savf

Rfile can handle many file conversions on IBM i

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

Tags: DB2, IBMi, pase, QP2term, QSH, qshell, Rfile, SSH
Share this entry
  • Share on Facebook
  • Share on X
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Reddit
  • Share by Mail
https://www.seidengroup.com/wp-content/uploads/2017/03/SeidenLogo-180.png 0 0 Calvin Buckley https://www.seidengroup.com/wp-content/uploads/2017/03/SeidenLogo-180.png Calvin Buckley2021-02-25 23:32:102022-06-16 23:25:51Reading and Writing Physical Files in a Shell using Rfile
Calvin Buckley

About Calvin Buckley

Recognized as an IBM Champion and a 2020 Fresh Face of IBM i, Calvin exhibits remarkable talent for expanding the use of IBM i through open ...Read More

4 replies
  1. Glenn Gundermann
    Glenn Gundermann says:
    October 17, 2022 at 10:48 pm

    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:
      May 29, 2023 at 2:32 pm

      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:
    February 28, 2021 at 2:07 pm

    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
    • Alan Seiden
      Alan Seiden says:
      March 4, 2021 at 7:21 am

      Thanks for the tip, Russ!

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel 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.

SUBSCRIBE
Open Thinking
Monthly IBM i Strategy & Tips
  • This field is for validation purposes and should be left unchanged.

IBM i Development

  • RPG, COBOL, SQL, Node,
    PHP, Python, Valance, etc.
  • Modernization. Integration.
  • Web and API Solutions
  • Legacy Maintenance
    ...
LET'S GET IT DONE!

Recent Posts

  • Do You Need IBM i Observability?
  • PHP 8.6 is Coming Soon to IBM i
  • Safer npm installation is on the way for Node.js (and all JavaScript)
  • Access Modern Security for IBM i and Connected Web Environments
  • IBM i: A Natural Platform for Agentic AI
  • PHP on IBM i in 2026: The Modernization Engine
  • Your “AS/400” Is a Modern IBM i Platform
  • MCP + AI for IBM i Teams (with a MongoDB example)
  • GnuPG PHP Extension for IBM i: Now Included with Seiden Support
  • Getting Started with Code for IBM i: A Lunch & Learn Video

SEIDEN GROUP: Modern Development & Open Source for IBM i

Home   |   Seiden PHP+   |   IBM i Services  |   Support   |   VS Code for i   |   Free Stuff   |   Blog  |   Privacy Policy  |   Contact         201.447.2437

© 2026 Seiden Group, LLC
  • Link to LinkedIn
  • Link to Mail
Link to: Node.js and IBM i – Getting Started Guide Link to: Node.js and IBM i – Getting Started Guide Node.js and IBM i – Getting Started Guide Link to: How to Measure Python Speed with QCachegrind Link to: How to Measure Python Speed with QCachegrind How to Measure Python Speed with QCachegrind
Scroll to top Scroll to top Scroll to top