Saving AWS CLI output to a file keeps API responses, inventory snapshots, and one-off checks available after the terminal session ends. A saved file is easier to attach to a ticket, compare across repeated runs, or hand to another tool than copied terminal text.
Shell redirection writes stdout to disk, while --output, --query, and --no-cli-pager decide the exact format, fields, and terminal behavior before the file is created. That makes it possible to save a full json response, a narrowed text value, or the same command result to both the screen and a file.
The saved file receives only stdout. Current AWS documentation recommends pairing --output text with --query for predictable field ordering, and AWS CLI errors stay on stderr unless they are redirected separately. The commands below use ec2 describe-regions --generate-cli-skeleton output so the save workflow can be demonstrated locally without live credentials, but generated skeleton output is not stable across CLI versions.
Steps to save AWS CLI output to a file:
- Save the command output as json when the file should keep the normal structured response body.
$ aws ec2 describe-regions --generate-cli-skeleton output --output json --no-cli-pager > regions.json
--output json overrides any saved profile formatter for that one command, and --no-cli-pager keeps the response from opening in the default pager before the shell writes the file.
- Open the saved file and confirm the response body was written.
$ cat regions.json { "Regions": [ { "OptInStatus": "OptInStatus", "Geography": [ { "Name": "Name" } ], "RegionName": "RegionName", "Endpoint": "Endpoint" } ] } - Save only the field list that matters when the next step needs a smaller text file instead of the full response.
$ aws ec2 describe-regions --generate-cli-skeleton output --query 'Regions[].RegionName' --output text --no-cli-pager > region-names.txt
Current AWS documentation recommends using --query with --output text so the saved columns and values stay predictable.
- Use tee when the same command result should stay visible in the terminal and be written to a file at the same time.
$ aws ec2 describe-regions --generate-cli-skeleton output --query 'Regions[].RegionName' --output text --no-cli-pager | tee region-names-live.txt RegionName
tee is useful during manual checks because the terminal view and the saved file come from the same command run.
- Redirect stderr to a separate log when the response body and the diagnostic trace must be kept in different files.
$ aws ec2 describe-regions --generate-cli-skeleton output --output json --debug --no-cli-pager > regions-debug.json 2> regions-debug.log
Replace > with >> when the response file should grow across repeated runs instead of being replaced.
- Check that the response file and the debug log were written to separate paths.
$ ls -l regions-debug.json regions-debug.log -rw-r--r-- 1 user staff 277 Apr 19 20:56 regions-debug.json -rw-r--r-- 1 user staff 16619 Apr 19 20:56 regions-debug.log
regions-debug.json contains stdout, while regions-debug.log contains the verbose debug trace from stderr.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
