Как удалить символы из имен файлов в папке?

1261
Midoes

У меня есть список файлов в каталоге, но некоторые из них имеют странные символы в имени. Как я могу переименовать только эти файлы, чтобы удалить эти символы?

Компьютерная ОС Red Hat, кодировка UTF-8.

Список файлов

0
Это действительно странный персонаж или знак вопроса? Paul 8 лет назад 0

2 ответа на вопрос

2
Larssend

Try detox. From the manual page:

Name

detox - clean up filenames

Synopsis

detox [-hnLrv] [-s -sequence] [-f -configfile] [--dry-run] [--special] file ...

Description

The detox utility renames files to make them easier to work with. It removes spaces and other such annoyances. It'll also translate or cleanup Latin-1 (ISO 8859-1) characters encoded in 8-bit ASCII, Unicode characters encoded in UTF-8, and CGI escaped characters.

Sequences

detox is driven by a configurable series of filters, called a sequence. Sequences are covered in more detail in detoxrc(5) and are discoverable with the -L option. Some examples of default sequences are iso8859_1 and utf_8.

It's available in RHEL 6 repositories, last time I checked. I'm not sure about RHEL 7. Before doing the actual cleanup, it's advisable to run detox with the -n (dry-run) switch. For example: detox -rn /somedir.

Не найдено в стандартных репозиториях Centos 7 и epel .... Найдено в RHEL 6. также ... в Fedora 23. mdpc 8 лет назад 0
0
AFH

If you can't use detox, or you want more control over the new file names, you can use standard tools.

ls with output to the terminal shows unprintable characters as ?. If the displayed name (eg ab?cd) is unique (ls ab?cd shows a single file) you can simply use this mask in an mv command:

mv ab?cd ab_cd 

ls -b shows unprintable characters as escape sequences (eg ab\001cd), though you can't use the displayed name in another command, but it does allow you to see all the affected files:

ls -b|grep '\\' ; # includes names with embedded spaces ls -b|grep '\\[^ ]' ; # excludes names with embedded spaces 

ls with output to a file or pipe writes the literal file name: in this case you can get a list of file names with unprintables with:

ls|env LC_ALL=C grep '[^!-~]' ; # includes names with embedded spaces ls|env LC_ALL=C grep '[^ -~]' ; # excludes names with embedded spaces 

You can now pipe this output to a script to perform whatever renaming you require, as in:

ls|env LC_ALL=C grep '[^!-~]'|while l=`line`; do ... ; mv "$l" "$n" ; done 

The omitted code is to formulate whatever new name $n you require. If you don't have line, you can use while read l instead, although this will not work properly with file names with leading or trailing spaces.

Похожие вопросы