How to find files that have a colon in the file name?
Solution 1:
Based on comments on my other "answer" (which was just too complex for a regular comment), user wants to find and rename files with unusual/troublesome characters in it. Below is an old Perl script which can find files at any level below any dir, including with special chars.
You run it like this if you're trying to remove a char that needs to be escaped in a regex: findfile.pl "\*"
You can optionally add a dir at the end too, like findfile.pl "\*" /opt
If you're trying to rename files with a ":" (which I can't reproduce), I don't think you need to escape the colon, but I can't test this without files with colons.
Since the user wants to rename the files, just modify the print in the final line to rename according to requirements.
use strict;
use warnings;
use File::Find;
my $substring;
my $dir;
if ($#ARGV < 0) {
print "\nUSAGE: $0 <substring> [dir]\n\n";
print "The substring is compared against the names of files located\nin (or under) dir (which is \".\" if not specified).\n";
print "\nPlease enter the substring: ";
$substring = <>;
chomp $substring;
print "Please enter the directory: [.] ";
$dir = <>;
chomp $dir;
if ($dir eq "") { $dir = "." }
}
else {
$substring = $ARGV[0];
if ($#ARGV > 0) {
$dir = $ARGV[1];
}
else {
$dir = ".";
}
}
find(sub {if (m/$substring/i) {print "$File::Find::name\n"}}, $dir);
Solution 2:
Ultimately I wanted to remove these special characters from the folder and file names. I ended up installing xcode and homebrew along with the homebrew 'rename' component. This ultimately allowed me to use a one liner for each character.
- Open terminal and run
xcode-select –install
- Follow the prompts to install it.
- Download HomeBrew (https://brew.sh/) by putting the below in terminal (password required):
curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh
- Install using
/bin/bash install.sh
- Install 'rename' using
brew install rename
- Use cd and ls and locate the folder you want to do the renaming on
- This command will look for the word 'foo' and rename it with ‘bar’.
find . -exec rename -s 'foo' 'bar' {} +
Once this is done, I used the following to rename the special characters (which are not supported by OneDrive).
find . -exec rename -s '"' '2' {} +
find . -exec rename -s '*' '8' {} +
find . -exec rename -s ':' '-' {} +
find . -exec rename -s '<' '(' {} +
find . -exec rename -s '>' ')' {} +
find . -exec rename -s '?' '' {} +
find . -exec rename -s '/' '_' {} +
find . -exec rename -s '\' '-' {} +
find . -exec rename -s '|' '-' {} +
find . -exec rename 's/^ *//' * {} + #remove leading blank spaces
find . -exec rename 's/ *$//' * {} + #remove trailing blank spaces