sed命令修复文件名中的一个目录 [英] sed command to fix filenames in a directory
问题描述
我运行的生成目录中的10K左右的文件的脚本。我只是发现有处于引起一些文件名有一个回车(presumably一个'\\ n字符)的脚本中的错误。
I run a script which generated about 10k files in a directory. I just discovered that there is a bug in the script which causes some filenames to have a carriage return (presumably a '\n' character).
我想运行一个sed命令用来删除文件名回车。
I want to run a sed command to remove the carriage return from the filenames.
任何人都知道这PARAMS传递给sed将清理文件名的方式进行描述?
Anyone knows which params to pass to sed to clean up the filenames in the manner described?
我运行Linux(Ubuntu的)
I am running Linux (Ubuntu)
推荐答案
我不知道该怎么 SED
将做到这一点,但这蟒蛇
脚本应该做的伎俩:
I don't know how sed
would do this, but this python
script should do the trick:.
这是不是 SED
,但我觉得蟒蛇
轻松很多做这样的事情时要使用:
This isn't sed
, but I find python
a lot easier to use when doing things like these:
#!/usr/bin/env python
import os
files = os.listdir('.')
for file in files:
os.rename(file, file.replace('\r', '').replace('\n', ''))
print 'Processed ' + file.replace('\r', '').replace('\n', '')
这条既 \\ r
和任何出现\\指定目录ñ
从所有的文件名。
要运行它,拯救它的地方, CD
到目标目录(与要处理的文件),并运行蟒蛇/路/到/的/ file.py
。
To run it, save it somewhere, cd
into your target directory (with the files to be processed), and run python /path/to/the/file.py
.
另外,如果你打算做更多的批量重命名,考虑变态。它是这个东西了一个非常好的和强大的图形用户界面。而且,它的免费的!
Also, if you plan on doing more batch renaming, consider Métamorphose. It's a really nice and powerful GUI for this stuff. And, it's free!
祝你好运!
事实上,试试这个: CD
进入目录,键入蟒蛇
,然后只需粘贴在:
Actually, try this: cd
into the directory, type in python
, and then just paste this in:
exec("import os\nfor file in os.listdir('.'):\n os.rename(file, file.replace('\\r', '').replace('\\n', ''))\n print 'Processed ' + file.replace('\\r', '').replace('\\n', '')")
这是previous脚本的单行版本,您不必将其保存。
It's a one-line version of the previous script, and you don't have to save it.
2版,空间置换权力:
Version 2, with space replacement powers:
#!/usr/bin/env python
import os
for file in os.listdir('.'):
os.rename(file, file.replace('\r', '').replace('\n', '').replace(' ', '_')
print 'Processed ' + file.replace('\r', '').replace('\n', '')
而这里的单行:
exec("import os\nfor file in os.listdir('.'):\n os.rename(file, file.replace('\\r', '').replace('\\n', '')replace(' ', '_'))\n print 'Processed ' + file.replace('\\r', '').replace('\\n', '');")
这篇关于sed命令修复文件名中的一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!