I'm trying to use this question to rename all the files in a directory and keep only [a-zA-Z0-9.
] (last character is a space).
The terminal entry I'm using is:
for file in /Users/bob/Downloads/videos/*.mp4; do mv "$file" "$(sed 's/[^0-9A-Za-z. ]//g' <<< "$file")"; done
What's being done?
My current understanding, command by command, is:
[1] for file in /Users/bob/Downloads/videos/*.mp4
Creates a string variable called file
which contains the text where *
is, so if a file was called my (awesome) kickass! video.mp4
the variable file
would be a string containing my (awesome) kickass! video.mp4
[2] do
For each file that matches the pattern (a file with an mp4 extension) do the next commands
[3] mv
"$file" "$(sed)"
Use the move file command to rename the matching file
[4] "$(sed 's/[^0-9A-Za-z. ]//g'
Use the stream editor command to do a regex find/replace.
Find every character which is not in the list and replace it with what is between the two forward slashes (nothing); ie, remove non-matching characters.
[5] <<< "$file")"; done
I have no idea what the <<<
operator(?) does, or what referencing back to the file
variable does.
Expected Result
A file named my (awesome) kickass! video.mp4
is renamed to my awesome kickass video.mp4
and stays in the same directory
Actual Result
The file my (awesome) kickass! video.mp4
is renamed to UsersbobDownloadsvideosmy awesome kickass video.mp4
and is moved to the Users
directory
echo
to print the command with the processed/transformed data into Terminal (...; do echo mv "$file" ...
) and look at the result.