Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap filenames in Unix?

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.

  • File A has name B-name.file
  • File B has name A-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.

like image 305
volni Avatar asked Dec 31 '09 22:12

volni


People also ask

How do you change a filename in Linux?

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.

How do I swap the contents of two files in Linux?

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.

How rename multiple files in Unix?

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.


4 Answers

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
like image 78
Sonique Avatar answered Nov 01 '22 10:11

Sonique


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.

like image 34
mark4o Avatar answered Nov 01 '22 10:11

mark4o


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.

like image 5
ennuikiller Avatar answered Nov 01 '22 10:11

ennuikiller


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.

like image 3
Jonathan Leffler Avatar answered Nov 01 '22 10:11

Jonathan Leffler