Grep mv and pipe chaining

I have some test files returned from:

do shell script "find ~/Desktop/'test Æ’'/ | grep -E " & "\\d{2}"'s quoted form

I want to strip out all numbers, dashes, and trailing spaces to yield new filenames (as compared to the original name):

09-14-2015 Something.txt
123 Not important.pdf
Whatever 00-1-123.pdf
who cares 123.rtf

Not important.pdf
who cares.rtf

I used the following regular expression to affect the above changes in a TEXT document. I’m unsure how to pipe a regex to rename the files with mv and grep (or awk/sed). The man pages are more confusing than helpful, and I’d appreciate any guidance.
Textwrangler version–([ ]?)((\d)±?(\d)±?(\d)+)([ ]?)
AS commented–([ ]?)((\d)±?(\d)±?(\d)+)([ ]?)

I don’t think this can be solved with pipes alone. This can be solved with command substitution but I prefer using subshell scripts (who can be used outside find as well).


set shellScript to "#!/bin/sh
# first argument (given by find) is the file path

# Only change the file name, not the entire path
realFileName=$(basename \"$filePath\")

# Get the path to the parent folder
dirName=$(dirname \"$filePath\")

# get the extension of the file name
# extension will be equal to file name if there is no extension
# extension will fail for nested extensions like tar.bz2, tar is not part of the extension 

# get the file name without extension

# use awk to get rid of some patterns in the file name
newFileName=$(awk '{gsub(/[ ]?[0-9]+[0-9\\-]*[ ]?/, \"\");print}' <<<\"$fileName\")


# only add the extension if there is any
if [ $realFileName != $extension ]; then

#rename the file
mv -f \"$filePath\" \"$newFilePath\""

do shell script "find -d $HOME/Desktop/'test Æ’' -exec sh -c " & quoted form of shellScript & " {} \\;"

When one new file name will be the same as another filename it will be overwritten. Thanks to option -f it will not return into an error.
The script is using the option -d, so the child will be renamed before it’s parent if you want it to work recursive. If you remove it paths can be broken.

Hi, DJ. Thank you for your time and also for commenting your sample code. That said, there appears to be a kink somewhere, as the filenames don’t change. In attempting to troubleshoot, I corrected find’s folder path back to “test Æ’” (option + f), where you used a vanilla “f.” That didn’t fix the issue, but I’ll continue to play with it.

I’m sorry, the verbose code didn’t actually move (to avoid renaming all files on my system) I forgot to change filepath into "$filePath" on the last line.


Your short version doesn’t work here – am I missing something? I also tried your long version with a file called ‘Who cares 123.mv4’ and ended up with ‘Who’.


FYI, here’s an ASObjC approach to the same problem. The main difference is that renaming will silently fail rather than overwrite an existing file of the same name. In a couple of small tests it also seems faster (by about an order of magnitude).

use scripting additions
use framework "Foundation"

set thePath to (POSIX path of (path to desktop)) & "test Æ’"
-- make URL of folder
set anNSURL to current application's class "NSURL"'s fileURLWithPath:thePath
-- get items in folder
set fileManager to current application's NSFileManager's defaultManager()
set theFiles to fileManager's contentsOfDirectoryAtURL:anNSURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
-- loop through
repeat with i from 1 to theFiles's |count|()
	set oldURL to (theFiles's objectAtIndex:(i - 1)) -- zero-based indexes
	-- get old name and change it
	set nameStub to oldURL's lastPathComponent()'s stringByDeletingPathExtension()
	set nameStub to (nameStub's stringByReplacingOccurrencesOfString:"[ ]?[0-9]+[0-9\\-]*[ ]?" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, nameStub's |length|()})
	-- make new URL and move
	set newURL to ((oldURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:nameStub)'s URLByAppendingPathExtension:(oldURL's pathExtension()))
	(fileManager's moveItemAtURL:oldURL toURL:newURL |error|:(missing value)) -- fails silently if file already exists
end repeat

Hi, Shane. Thank you, as well, for your effort. I will revisit your method after I install El Cap.

The short version did work on my machine but have removed it. The newer version is too long and there is no point in such unreadable code anymore.

Thanks Shane. The awk script was applied to the entire file name. Now it’s only applied to the file name without the extension. I’ve updated the code above.