Last updated: Feb 26, 2024
Reading time·3 min
To list all of the files of an S3 bucket with the AWS CLI, use the s3 ls
command, passing in the --recursive
parameter.
aws s3 ls s3://YOUR_BUCKET --recursive --human-readable --summarize
The output of the command shows the date the objects were created, their file size and their path.
1,000
list requests is $0.005
in the us-east-1
region.The parameters we passed to the s3 ls command are the following:
Name | Description |
---|---|
recursive | performs the command on all files under the set prefix. |
human-readable | displays the file sizes in human-readable format. |
summarize | displays the total number of objects and their total size. |
--recursive
parameter from the command.To list all files, located in a folder of an S3 bucket, use the s3 ls
command, passing in the entire path to the folder and setting the --recursive
parameter.
aws s3 ls s3://YOUR_BUCKET/YOUR_FOLDER/ --recursive --human-readable --summarize
The output of the command only shows the files in the /my-folder-1
directory.
To only list the filenames of an S3 bucket, we have to:
s3api list-objects
command.--output
parameter to text
.--query
parameter to filter the output of the command.aws s3api list-objects --bucket YOUR_BUCKET --output text --query "Contents[].{Key: Key}"
The output of the command shows all the filenames, or in other words, path names in the S3 Bucket.
To list only the filenames in a specific folder, add the --prefix
parameter
to the command.
aws s3api list-objects --bucket YOUR_BUCKET --prefix "my-folder/" --output text --query "Contents[].{Key: Key}"
We can also redirect the output to a file on the local file system.
aws s3api list-objects --bucket YOUR_BUCKET --prefix "my-folder-1/" --output text --query "Contents[].{Key: Key}" > file-names.txt
You can learn more about using AWS S3 by checking out the following tutorials: