LinuxForum.Fans
Тема

групповое переименование файлов

Автор: v0id 7 сообщений 3099 просмотров
#393426

Доброго времени суток!
Подскажите как решить задачку.
Есть много файлов file[X] где X какое-то число.
Нужно переименовать file[X] в new[X], число должно остаться неизменным.

#393436

как то так
[console]find /some/dir -name "*file*"|xargs rename file new[/console]

#393437

v0id,

DESCRIPTION
rename will rename the specified files by replacing the first occurrence of expression in
their name by replacement.

OPTIONS
-v, --verbose
Give visual feedback which files where renamed, if any.

-V, --version
Display version information and exit.

-h, --help
Display help text and exit.

EXAMPLES
Given the files foo1, ..., foo9, foo10, ..., foo278, the commands

rename foo foo0 foo?
rename foo foo0 foo??

will turn them into foo001, ..., foo009, foo010, ..., foo278. And

rename .htm .html *.htm

will fix the extension of your html files.
#393443

Так не работает

text
Bareword "file" not allowed while "strict subs" in use at (eval 1) line 1.
xargs: rename: exited with status 255; aborting
#393445

Немного грубо но для вашей задачи подойдет:

[console]#!/bin/bash

_SOURCEDIR="/home/user/test" # в какой папке ищем
_FROMNAME="file" # что нужно заменить в файле
_TONAME="new" # на что нужно заменить

find $_SOURCEDIR/$_FROMNAME* -type f | while read F; do
mv -vf "$F" "$(dirname "$F")/$(basename "$F" | sed 's|'$_FROMNAME'|'$_TONAME'|')";
done[/console]

#393498

"v0id" написал:
Так не работает
man rename читали? Ну вот и не работает.
#393516

Всем спасибо!
Решил задачу, с помощью команды:

text
rename 's/file/new/' *file*
Войдите или зарегистрируйтесь, чтобы ответить.