在 bash 脚本中批量 mv 或重命名 - 附加日期作为后缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19670137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch mv or rename in bash script - append date as a suffix
提问by user2755742
After much searching and trial and error, I'm unable to do a batch mv
or rename
on a directory of files. What I'd like to do is move or rename all files in a directory so that the mv
'd or rename
d file has $date (+ '%Y%d%m')
added to the original suffix.
经过多次搜索和反复试验,我无法批量处理mv
或rename
处理文件目录。我想要做的是移动或重命名目录中的所有文件,以便将mv
'd 或rename
d 文件$date (+ '%Y%d%m')
添加到原始后缀中。
All the original files have unique prefixes but are either .xml
or .txt
so I'd like to go from org_prefix.org_suffix
-> org_prefix.org_suffix.DATE
所有原始文件有唯一的前缀,但无论是.xml
或.txt
,所以我想从去org_prefix.org_suffix
- >org_prefix.org_suffix.DATE
I've tried this:
我试过这个:
$ mv /directory/* /directory/*$(date (+ '%Y%m%d')
but always get /directory/*.actualdate' is not a directory
error.
但总是/directory/*.actualdate' is not a directory
出错。
I've tried this:
我试过这个:
$ for f in *; do mv $ $f.$(date +'_%m%d%y'); done
but I get mv: cannot stat '$'; No such file or directory
但我明白了 mv: cannot stat '$'; No such file or directory
Lastly, I've even tried this:
最后,我什至试过这个:
$ rename 's/*/.test/' *
just to see if I could change all the files to org_prefix.test
but nothing happens (no errors, nada, zip)
只是想看看我是否可以将所有文件更改为org_prefix.test
但没有任何反应(没有错误,nada,zip)
Any help greatly appreciated.
非常感谢任何帮助。
回答by gniourf_gniourf
The proper way to loop through files (and e.g., print their name) in the current directory is:
在当前目录中循环文件(例如,打印它们的名称)的正确方法是:
for file in *; do
echo "$file"
done
How will you append the date? like so, of course:
你将如何附加日期?像这样,当然:
for file in *; do
echo "$file.$(date +%Y%m%d)"
done
And how are you going to do the move? like so, of course:
你打算怎么做?像这样,当然:
for file in *; do
mv -nv -- "$file" "$file.$(date +%Y%m%d)"
done
I've added:
我已经添加:
-v
so thatmv
be verbose (I like to know what's happening and it always impresses my little sister to watch all these lines flowing on the screen).-n
so as to no overwrite an otherwise existing file. Safety first.--
just in case a file name starts with a hyphen: without--
,mv
would confuse with an option. Safety first.
-v
所以mv
要冗长(我想知道发生了什么,我的小妹妹总是看到屏幕上流动的所有这些线条给我留下了深刻的印象)。-n
以免覆盖其他现有文件。安全第一。--
以防万一文件名以连字符开头:如果没有--
,mv
会与选项混淆。安全第一。
If you just want to look through the files with extension .banana
, replace the for
with:
如果您只想查看扩展名为 的文件.banana
,请将其替换为for
:
for file in *.banana; do
of for files that contain the word banana
:
对于包含单词的文件banana
:
for file in *banana*; do
and so on.
等等。
Keep up with the bananas!
跟上香蕉!
回答by eminor
$ mv /directory/* /directory/*$(date (+ '%Y%m%d')
This does not work, because the *
is expanded to a list of all files, so after the expansion the command will be something like:
这行不通,因为*
被扩展为所有文件的列表,所以扩展后命令将类似于:
mv /directory/file1 /directory/file2 /directory/file3 /directory/file1_date /directory/file1_date ...
So you have specified many destinations, but the syntax for mv allows only one single destination.
所以你已经指定了许多目的地,但是 mv 的语法只允许一个单一的目的地。
for f in *; do mv $ $f.$(date +'_%m%d%y'); done
Here you forgot the f
after the $
, that's why you get the error message.
在这里您忘记了f
之后$
,这就是您收到错误消息的原因。
for f in *; do mv $f $f.$(date +'%m%d%y'); done
I think this should work now, but don't forget to quote all the variables!
我认为这现在应该可以工作了,但不要忘记引用所有变量!
Finally:
最后:
for f in *; do mv "$f" "$f.$(date +'%m%d%y')"; done
Edit: When there are characters directly after a variable, it's good practice to use {}
to make clear that they are not part of the variable name:
编辑:当变量后直接有字符时,最好使用{}
以明确它们不是变量名称的一部分:
for f in *; do mv "$f" "${f}.$(date +'%m%d%y')"; done