find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with tags in Mac OS x.

my current closest command is this (doesn't work):

tag -f "Note" . | xargs -0 {} mv {} .{}

Hi,

in order for xargs to use NUL character as separator, the utility before the pipe needs to produce them.

try:

mdfind -0 -onlyin . "Note" | xargs -0 -I {} echo {}

or

tag -0 -f "Note" . | xargs -0 -I {} echo {}
1 Like

Hi Scrutinizer,

Thanks for the quick response.

for tag command :

tag -0 -f "Note" . | xargs -0 -I {} echo {}

It only echo out a list of files including directories. Something like this /dir/dir/dir/file.ext.

So when I try to do:

tag -0 -f "Note" . | xargs -0 -I {} rename -n 's/^/\./' {}

the out put came out like :

'/dir/dir/dir/file.ext' would be renamed to './dir/dir/dir/file.ext'

but I want the result to be :

'/dir/dir/dir/file.ext' would be renamed to '/dir/dir/dir/.file.ext'

For mdfind, I modified the code to be like this to echo out the list:

mdfind -0 "kMDItemUserTags == 'Note'" | xargs -0 -I {} echo {}

But still no luck of adding '.' to the file only at the beginning.

If you use this:

s/^/\./

Then the . is added at the beginning of the path.
To add it to the last item in the path, try:

s|.*/|$0/.|
1 Like

I tired :

tag -0 -f "Note" . | xargs -0 -I {} rename -n 's|.*/|$0/.|' {}

It starts to add the '.' to the file, however it removed out the subdirectory completely like this:

'/dir/dir/dir/dir/file.txt' would be renamed to '/usr/local/bin/rename/.file.ext'

Try this instead:

s|(.*)/|$1/.|
1 Like

It works! Thank you so much Scrutinizer!

So the solution is as follows:

tag -0 -f "Note" . | xargs -0 -I {} rename 's|(.*)/|$1/.|' {}
3 Likes