Checking if a File Exists in iCloud from the Command Line

C

While moving selected local files and folders to iCloud I came across a folder on my Mac containing a large number of documents some of which appeared to be duplicates of those on iCloud. Before deciding which ones to move, I wanted to check each one to see if it already existed on iCloud.

Documents folder on iCloud Drive
Documents folder on iCloud Drive

 

 

Local temp folder
Local temp folder

 

 

A comparison of the two folders shows that of the four entries in the temp folder, the file named file.txt and the folder named folder don’t exist on iCloud.

This example only shows a fraction of the contents of each folder and so having written the Bash script below to loop through the contents of the temp folder checking for the existence of each entry on iCloud, I assumed the results would confirm the visual comparison.

Dummy Content
#!/usr/bin/env bash

shopt -s nullglob
shopt -u dotglob

src="/Users/steve/temp"
trg="/Users/steve/Documents"

for entry in "$src"/{..?,.[!.],}*; do
	name=$(basename -- "$entry")
	if [ ! -e "$trg/$name" ]; then	# file doesn't exist on target
		echo "'$name' DOESN'T EXIST in $trg"
	else
		echo "'$name' already exists in $trg"
	fi
done

shopt -u nullglob
shopt -s dotglob
'file.txt' DOESN'T EXIST in /Users/steve/Documents
'folder' DOESN'T EXIST in /Users/steve/Documents
'Wordpress Plugins.docx' already exists in /Users/steve/Documents
'world_cup_2018.xlsx' DOESN'T EXIST in /Users/steve/Documents

 

 

Not so. Clicking the OUTPUT tab above shows the results are as expected except for the file world_cup_2018.xlsx which the script has determined as not being on iCloud when the Finder clearly shows it is.

The clue to the script’s unexpected result is the Documents folder on iCloud Drive icon next to the world_cup_2018.xlsx file on iCloud which denotes the file has yet to be downloaded to the local drive.

Listing the Document folder’s contents on iCloud from the command line shows that such files are subject to a different naming convention not evident in the Finder where the file name is prefixed with . – traditionally denoting a hidden file – and ends with .icloud.

Dummy Content
ls -a /Users/steve/Documents
.world_cup_2018.xlsx.icloud
Wordpress Plugins.docx
workout.xlsx

 

 

Ammending the script to include an additional test for this other naming convention gives the expected results:

Dummy Content
#!/usr/bin/env bash

shopt -s nullglob
shopt -u dotglob

src="/Users/steve/temp"
trg="/Users/steve/Documents"

for entry in "$src"/{..?,.[!.],}*; do
	name=$(basename -- "$entry")
	if [ ! -e "$trg/$name" ] && [ ! -e "$trg/.$name.icloud" ]; then	# file doesn't exist on target
		echo "'$name' DOESN'T EXIST in $trg"
	else
		echo "'$name' already exists in $trg"
	fi
done

shopt -u nullglob
shopt -s dotglob
'file.txt' DOESN'T EXIST in /Users/steve/Documents
'folder' DOESN'T EXIST in /Users/steve/Documents
'Wordpress Plugins.docx' already exists in /Users/steve/Documents
'world_cup_2018.xlsx' already exists in /Users/steve/Documents

 

 

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of fish & chips and writing the occasional blog post he hopes others will find helpful.

Add comment

Steve

Recent Comments

Recent Posts