파일 이름에서 숫자 제거

파일 이름에서 숫자 제거

디렉토리의 파일 이름을 수정하는 데 문제가 있습니다 Music/ .

다음과 같은 이름 목록이 있습니다.

$ ls
01 American Idiot.mp3
01 Articolo 31 - Domani Smetto.mp3
01 Bohemian rapsody.mp3
01 Eye of the Tiger.mp3
04 Halo.mp3
04 Indietro.mp3
04 You Can't Hurry Love.mp3
05 Beautiful girls.mp3
16 Apologize.mp3
16 Christmas Is All Around.mp3
Adam's song.mp3
A far l'amore comincia tu.mp3
All By My Self.MP3
Always.mp3
Angel.mp3

마찬가지로 파일 이름 앞의 모든 숫자를 제거하고 싶습니다(확장자의 3은 제외).

처음에는 또는 숫자가 있는 파일만 사용해 보았지만 grep첫 번째 단계에서도 성공하지 못했습니다. 가능해지면 실제 이름을 변경하고 싶습니다.find -execxargsgrep

내가 지금 시도한 것은 다음과 같습니다.

ls > try-expression
grep -E '^[0-9]+' try-expression

위와 같이 올바른 결과를 얻고 있습니다. 그런 다음 다음 단계를 시도했습니다.

ls | xargs -0 grep -E '^[0-9]+'
ls | xargs -d '\n' grep -E '^[0-9]+'
find . -name '[0-9]+' -exec grep -E '^[0-9]+' {} \;
ls | parallel bash -c "grep -E '^[0-9]+'" - {}

비슷하지만 "파일 이름이 너무 김"과 같은 오류가 발생하거나 전혀 출력되지 않습니다. 문제는 별도의 명령에서 or 를 표현식으로 사용하는 방식이 xargs잘 작동한다는 것입니다.find

당신의 도움을 주셔서 감사합니다

답변1

숫자로 시작하는 디렉토리의 모든 파일을 나열하려면,

find . -maxdepth 1 -regextype "posix-egrep" -regex '.*/[0-9]+.*\.mp3' -type f

접근 방식의 문제점은 find파일에 대한 상대 경로를 반환하는 반면 파일 이름 자체만 예상한다는 것입니다.

답변2

Debian, Ubuntu 및 그 파생 제품에서는 renamePerl 스크립트를 사용합니다(Perl이 아닌 스크립트도 있으므로 rename작동하지 않습니다).

이름 바꾸기 작업을 시뮬레이션합니다.

rename 's/^\d+ //' * -n

삭제 -n(작업 없음)는 다음 작업을 수행합니다.

rename 's/^\d+ //' *

다행스럽게도 Perl rename도 /usr/bin/rename배포판에 설치되어 있습니다(Fedora도 Perl rename을 사용한다는 소문이 있습니다).

보다Perl 이름 바꾸기 매뉴얼 페이지기타 기능에 대한 자세한 내용.

답변3

bashjust 와 정규식을 사용하여 다음을 수행할 수 있습니다.가정 어구:

#! /bin/bash

# get all files that start with a number
for file in [0-9]* ; do
    # only process start start with a number
    # followed by one or more space characters
    if [[ $file =~ ^[0-9]+[[:blank:]]+(.+) ]] ; then
        # display original file name
        echo "< $file"
        # grab the rest of the filename from
        # the regex capture group
        newname="${BASH_REMATCH[1]}"
        echo "> $newname"
        # uncomment to move
        # mv "$file" "$newname"
    fi
done

예제 파일 이름에 대해 실행하면 출력은 다음과 같습니다.

< 01 American Idiot.mp3
> American Idiot.mp3
< 01 Articolo 31 - Domani Smetto.mp3
> Articolo 31 - Domani Smetto.mp3
< 01 Bohemian rapsody.mp3
> Bohemian rapsody.mp3
< 01 Eye of the Tiger.mp3
> Eye of the Tiger.mp3
< 04 Halo.mp3
> Halo.mp3
< 04 Indietro.mp3
> Indietro.mp3
< 04 You Can't Hurry Love.mp3
> You Can't Hurry Love.mp3
< 05 Beautiful girls.mp3
> Beautiful girls.mp3
< 16 Apologize.mp3
> Apologize.mp3
< 16 Christmas Is All Around.mp3
> Christmas Is All Around.mp3

답변4

방금 Rexx 개체에 몇 줄을 썼고 파일 시작 부분의 숫자를 제거했습니다. 내 파일은 다음과 같습니다.

데이터:

003. Atomic Rooster.mp3
087. Crowded House.mp3

암호:

#!/bin/rexx
'rxqueue /clear'
'ls | rxqueue'
do while queued()>0
 parse pull fn
 parse var fn .'.'rest
 rest = strip(rest)
 if pos('.',rest)=0 then iterate
 "mv '"fn"' '"rest"'"
end

관련 정보