
나는 이것을 할 수 있기를 원합니다: 이름을 바꾸고 string1
처음 에 string3
추가합니다 . string2
문자열 Fish가 발견되고 Great_ = string2인 경우 Fish와 같은 Sth는 Great_Bear로 이름이 변경되었습니다.
지금까지 나는 이것을 가지고 있습니다 :
ls | sed s'/\(.*\)\(string1\)\(.*\)/mv \"&\" \"\1string3\" /' | bash
이것은 현재 디렉토리에 대한 작업을 수행합니다.
ls -d $PWD/**/* | sed s'/\(.*\)\(string1\)\(.*\)/mv \"&\" \"\1string3\" /' | bash
이는 하위 디렉터리에서만 작동하고 스크립트가 있는 디렉터리에서는 작동하지 않습니다.
string2
또한 파일 이름의 시작 부분에 추가하는 방법을 알고 싶습니다 .
답변1
나는 globstar 패턴이 아닌 rename
간단한 일치와 함께 사용합니다 ..*/*
rename 's|([^/]+)/(.+)|$1/$1_$2|' */* -vn
우리는 디렉터리와 그 안에 포함된 내용을 일치시킵니다. 너무 멀리 재귀하는 것을 원하지 않기 때문에 이것은 globstar보다 다소 안전합니다.
결국 -n
에는 실제로 중지됩니다.행위아무것. 그것은 단지 당신에게 보여줄 것입니다. 그것이 정확하다고 확신하면 제거하십시오. 다음은 약간의 테스트 도구입니다.
$ mkdir -p test/test{1..3} && touch test/test{1..3}/file{1..3}
$ cd test
$ rename 's|([^/]+)/(.+)|$1/$1_$2|' */* -vn
test1/file1 renamed as test1/test1_file1
test1/file2 renamed as test1/test1_file2
test1/file3 renamed as test1/test1_file3
test2/file1 renamed as test2/test2_file1
test2/file2 renamed as test2/test2_file2
test2/file3 renamed as test2/test2_file3
test3/file1 renamed as test3/test3_file1
test3/file2 renamed as test3/test3_file2
test3/file3 renamed as test3/test3_file3
답변2
이렇게 하면 트릭을 수행할 수 있습니다.
find . -type f -exec rename -n 's/(.*)\/(.*)string1(.*)/$1\/string3$2string2$3/' {} +
find . -type f -exec
{} +
: 현재 작업 디렉터리의 계층 구조에서 파일을 반복적으로 검색하고 결과 목록으로 확장되는 명령줄의 나머지 부분을 실행합니다 .rename -n 's/(.*)\/(.*)string1(.*)/$1\/string3$2string2$3/' {} +
: 가 마지막으로 나타날 때까지 임의의 문자와 일치 및 그룹화/
, 와 일치, 가/
마지막으로 나타날 때까지 임의의string1
문자string1
와 일치 및 그룹화, 임의의 수의 문자와 일치 및 일치 및 그룹화; 일치 항목을 첫 번째 캡처된 그룹, 문자/
,string3
두 번째 캡처된 그룹,string2
세 번째 캡처된 그룹으로 바꿉니다( 드라이런을 수행하고 이를 제거하여 실제로 파일 이름을 바꿉니다)-n
.rename
% tree
.
└── dir
├── string1_bar.jpg
├── string1_foobar.jpg
└── string1_foo.jpg
1 directory, 3 files
% find . -type f -exec rename -n 's/(.*)\/(.*)string1(.*)/$1\/string3$2string2$3/' {} +
rename(./dir/string1_foo.jpg, ./dir/string3string2_foo.jpg)
rename(./dir/string1_foobar.jpg, ./dir/string3string2_foobar.jpg)
rename(./dir/string1_bar.jpg, ./dir/string3string2_bar.jpg)