I having a program that successfully uploads all of the files that I need. I have new files everyday that I need to upload. After I have uploaded the files I no longer need them and thus am not looking to sync them.
I am curious if there is a way to check if given a path and file name if that exists within S3 using the s3cmd.
We can use s3cmd ls , Take one flag flag_exists true if file is there and false if file is not there.
Explanation - Since ls can return many values like if u search for s3cmd ls abc.txt , then it can return values like abc.txt abcd.txt and so on , so looping and checking using if condition if file exists.
You can use the ls command in s3cmd to know if a file is present or not in S3.
Bash code
Usage: ./s3_exist.sh s3://foo/bar.txt
Edit:
As cocoatomo pointed out in comments,
s3cmd ls $path
lists all file that begins with$path
. A safer approach would be to uses3cmd info $path
and check the exit code.New Bash code
In the newer version of AWS CLI, you can use the following code to detect the existence of a file or directory
Assuming that bar.txt and bar.txt.bak exist in a bucket s3://foo, "s3cmd ls s3://foo/bar.txt" shows a following output.
Since we should remove 2nd line from the command result, we use "awk" command to filter unnecessary lines.
Finally, we build up all commands.