On OSX you can have tags which allows you to identify different categories of files. Here are two text files with different tags when viewed in Finder:I have a large number of these files and have written a bash script to make several edits on these files. So, for instance I do:$ sed 's/old text/new text/' file1.txt > /tmp/foo.txt$ cp -p /tmp/foo.txt file1.txtin which case I get the desired file content but loose the tags:Question:How do I copy the tags from the original file to the edited file?
Edit a file via a script yet maintain the osx tags
shell script;files;osx;xattr;tagging
When you run > /tmp/foo.txt, you are overwriting the contents of /tmp/foo.txt with the output of sed 's/old text/new text/' file1.txt. Since /tmp/foo.txt doesn't exist when you run this command, bash will create that file for you and then write it.Then, when you use the -p flag to cp, you are copying the permissions and attributes of /tmp/foo.txt to file1.txt and overwriting it in the process.Instead, what you want to do is overwrite the contents of file1.txt with the contents of /tmp/foo.txt, which should sound familiar. Runsed 's/old text/new text/' file1.txt > /tmp/foo.txtcat /tmp/foo.txt > file1.txtNote that this doesn't copy any permissions or attributes from file1.txt to /tmp/foo.txt or vice versa - you are maintaining the existing metadata for file1.txt, but modifying the content.In this specific instance, you could also just use sed -i as pbm suggested.