I have hundreds of directories, some nested in other directories, with tens of thousands of files. The files need to have a date/time stamp removed from them.
An example filename is Letter to Client 27May2016~20160531-162719.pdf
and I would like for it to go back to being Letter to Client 27May2016.pdf
Another example filename is ABCDEF~20160531-162719
and I would like for it to go back to being ABCDEF
. Note that this file has no extension, unlike the example above.
I need a command that I can run at the root of the affected folders that will recursively go through and find/fix the filenames.
( I use Syncthing to sync files, and restored deleted files by copying them from the .stversions
directory back to where they were, but found that Syncthing appends that date/time stamp…)
Best Answer
Meet the Perl
rename
tool:(online man page, also see this Q)
That regex says to match a tilde, as many characters that are not dots, but at least one; and to replace whatever matched with an empty string. Remove the
-n
to actually do the replace. We could change the pattern to~[-0-9]+
to just replace digits and dashes.Sorry, you said "recursively", so lets use
find
:Or just with Bash or ksh, though directories with
~
followed by digits will break this:Again, remove the
echo
to actually do the rename.