使用mv命令将无法识别*作为通配符 [英] Using mv command won't recognize * as wildcard
问题描述
我有这样的剧本,我知道awk命令在它的工作原理,但是当我想用文件同名保存输出它使用 MV 用来做任务code>命令
*
不使用通配符,而是作为一个性格和我结束了用这样的名称的文件:
Algeria_BER _ * _ sites.txt
块引用>脚本:
#!/斌/庆典因为我在Algeria_BER
做 回声的awk'FNR == {NR一个[++ i] = $ 0;接下来} {打印[FNR] RS $ 0}'\\
reduced_filt_MAF_b37_chr1 _ $ {I} _ldhat.txt_names $ {I} _ * _ sites.txt> $$ TMP和放大器;&安培;
MV $$。TMP$ {I} _ * _ sites.txtDONE
解决方案的
MV
命令无法识别*
通配符;外壳一样。这
富* .TXT
扩展到所有文件在当前目录中其名称以
富
列表和.TXT
结束。如果你通过富* .TXT
作为参数传递给MV
的MV
命令不会看到*
;它只能看到的文件的列表。这意味着,如果你有文件
foo1.txt
和foo2.txt
,这样的:MV富* .TXT酒吧* .TXT
会的不的重命名
foo1.txt
到bar1.txt
和foo2.txt
到bar2.txt
- 因为酒吧* .TXT
只能扩展到一个列表文件的已经存在的。如果有匹配的酒吧* .TXT
不存在的文件,那么上面的扩展为:MV foo1.txt foo2.txt bar42.txt
这是一个错误,除非
bar42.txt
恰好是一个目录。这将扩大各种其它方式取决于哪些文件发生的时刻存在 - 他们都不很可能是你想要的东西。如果没有文件匹配给定的通配符的行为取决于外壳和当前设置。在
庆典
,使用默认设置,通配符只是没有展开 - 这可能是为什么你得到一个文件中的*
在其名称中。有批量重命名工具,将你想要做什么(我没有用他们自己)。
另一种方法我经常使用的是文件列表到一个文本文件,手动编辑该文件,然后运行它作为一个脚本。例如,我可以做到这一点:
LS富* .TXT> TMP
然后手动编辑
TMP
把每行成我想要做什么的命令:foo1.txt
foo2.txt- >
MV foo1.txt bar1.txt
MV foo2.txt bar2.txt然后运行命令:
。 ./tmp
I have this script, and I know the awk command in it works, but when I want to save the output using the same name of the file it used to do the task using the
mv
command the*
is not used as a wildcard but instead as a character and I end up with a file with a name like this:Algeria_BER_*_sites.txt
The script:
#!/bin/bash for i in Algeria_BER do echo awk 'FNR==NR{a[++i]=$0;next} {print a[FNR] RS $0}' \ reduced_filt_MAF_b37_chr1_${i}_ldhat.txt_names ${i}_*_sites.txt > $$.tmp && mv $$.tmp "${i}_*_sites.txt" done
解决方案The
mv
command doesn't recognize the*
wildcard; the shell does.This:
foo*.txt
expands to a list of all files in the current directory whose names start with
foo
and end with.txt
. If you passfoo*.txt
as an argument tomv
, themv
command never sees the*
; it only sees the list of files.Which means that if you have files
foo1.txt
andfoo2.txt
, this:mv foo*.txt bar*.txt
will not rename
foo1.txt
tobar1.txt
andfoo2.txt
tobar2.txt
-- becausebar*.txt
can only expand to a list of files that already exist. If there are no existing files matchingbar*.txt
, then the above expands to:mv foo1.txt foo2.txt bar42.txt
which is an error unless
bar42.txt
happens to be a directory. It will expand in various other ways depending on what files happen to exist at the moment -- and none of them are likely to be what you want.If there are no file matching a given wildcard, the behavior depends on the shell and your current settings. In
bash
, with default settings, the wildcard just isn't expanded -- which is probably why you're getting a file with a*
in its name.There are bulk rename tools that will do what you want (I haven't used them myself).
Another approach I often use is to list the files to a text file, edit the file manually, and then run it as a script. For example, I might do this:
ls foo*.txt > tmp
then manually edit
tmp
to turn each line into a command that does what I want:foo1.txt foo2.txt
-->
mv foo1.txt bar1.txt mv foo2.txt bar2.txt
and then run the command:
. ./tmp
这篇关于使用mv命令将无法识别*作为通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!