Any way to do this quickly without using a temp variable? Is there a built in function?
Edit: 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.
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.
You can use the built-in Linux command mv to rename files. Here are some of the options that can come in handy with the mv command: -v , --verbose : Explains what is being done. -i , --interactive : Prompts before renaming the file.
To achieve that, we can concatenate the commands with “&&“. This is because the && operator ensures the later command gets executed only if the preceding command succeeds. As the output above shows, the command works. We've swapped the content of the two files.
Rename Multiple Files with the mv Command Next, -exec executes the mv command on any files that match the search, changing their current filenames to the new one. Another method is to use the mv command as a part of a <strong>for</strong> loop in a bash script.
This can be done with little helper, just put in .bashrc, .zshrc or where your configs.
function swap() { mv "$1" "$1._tmp" && mv "$2" "$1" && mv "$1._tmp" "$2"; }
And use it as regular function:
$ cat a b
Alfa
Beta
$ swap a b && cat a b
Beta
Alfa
Darwin/Mac OS X has the exchangedata()
system call:
The
exchangedata()
function swaps the contents of the files referenced by path1 and path2 in an atomic fashion. That is, all concurrent processes will either see the pre-exchanged state or the post-exchanged state; they can never see the files in an inconsistent state.
However it only actually works on a few filesystems that specifically support it (such as Apple's HFS and HFS+), and I haven't seen any similar system call on other systems. The portable way to do this is using a third temporary file name, and the operation will not be atomic.
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.
At shell script level - there isn't a standard command and the rename involves at least a temporary file name (beware files on different file systems!).
At the C code level, there isn't a standard function available on all machines to swap file names - and one of the factors is the issue of dealing with files on different file systems.
On a single file system:
file1=one-file-name
file2=tother-file
file3=tmp.$$
trap "" 1 2 3 13 15
ln $file1 $file3
rm $file1
ln $file2 $file1
rm $file2
ln $file3 $file2
rm $file3
trap 1 2 3 13 15
That isn't completely fool-proof - but it is a semi-decent approximation if $file1 and $file2 are on the same file system (and I've assumed $file3 is a name on the same file system). Fixing it to deal with all the warts is ... non-trivial. (Consider $file1 == $file2, for instance.)
The code simulates pretty much the system calls that a C program would have to make - ln
to map to link()
and rm
to map to unlink()
. The newer (as in, only twenty years old) function rename()
can probably be used to good effect - provided you understand what it won't do. Using the mv
command instead of link and remove means that the files will be moved between file systems if needed; that might be helpful - or it might mean that your disk space fills up when you didn't intend it to. Recovering from an error part way through is not entirely trivial, either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With