如何在Unix中交换文件名? [英] How to swap filenames in Unix?
问题描述
有什么方法可以在不使用temp变量的情况下快速完成此操作?有内置功能吗?
Any way to do this quickly without using a temp variable? Is there a built in function?
谢谢大家的回答.看来我需要澄清我的问题,但在大多数情况下,你们都假设正确:有两个文件,而文件名却相反.
Thanks for the answers guys. Looks like I need to clarify my question but for the most part you guys assumed correctly: There are two files and the filenames names are reversed.
- 文件A的名称为B-name.file
- 文件B的名称为A-name.file
我想将文件A命名为A-name.file,将文件B命名为B-name.file.
I'd like for File A to be named A-name.file and File B to be named B-name.file.
我同意,这种情况不会经常发生,只是发生在我身上,我想快速解决.
I agree, the situation doesn't happen often but it just happened to me and I wanted a quick fix.
推荐答案
好的,愚蠢的问题,但是为什么不能简单地做一些事情(在shell脚本中):
ok, stupid question, but why can't you simply do something like (in a shell script):
mv $fileA $fileA.$$
mv $fileB $fileA
mv $fileA.$$ $fileB
是的,它当然使用一个临时文件,但是比其他答案更简洁.
and yes of course it uses a temporary file, but its more concise then the other answers.
这篇关于如何在Unix中交换文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!