Unix genius - remove "." from a bunch of filenames?
I have an amount of files, somewhat inexplicably, but due to extenuating circumstances, receive a ". " (period AND space) appended to the front of the filename.
I would like to write a bash script to remove this, but I don't really know where to start.
I ca开发者_开发问答n retrieve a list of all the affected files by
find dir -name ".*"
But that's about all I know. Can anyone help me out?
find dir -type f -regex ".*/[.] .*" -exec rename ". " "" {} \;
Find everything within dir where the name matches the regular expression ".*/[.] .*"
- "Anything, slash, dot, space, anything"
For each found file, execute: rename ". " "" filename
which changes ". " to "" in the file called filename
If you have the Perl script version of rename
:
find dir -name '. *' -exec rename 's/^. //' {} \;
for fn in \.\ *; do mv -T "$fn" "${fn:2}"; done
The -T
is for safety, i.e., treat "${fn:2}" as file instead of directory.
(Tested in bash)
精彩评论