简体   繁体   中英

Unix create multiple files with same name in a directory

I am looking for some kind of logic in linux where I can place files with same name in a directory or file system.

For eg i create a file abc.txt , so the next time if any process creates abc.txt it should automatically check and make the file named as abc.txt.1 should be created, then next time abc.txt.2 and so on...

Is there a way to achieve this.

Any logic or third party tools are also welcomed.

You ask,

For eg i create a file abc.txt, so the next time if any process creates abc.txt it should automatically check and make the file named as abc.txt.1 should be created

(emphasis added). To obtain such an effect automatically, for every process, without explicit provision by processes, it would have to be implemented as a feature of the filesystem containing the files. Such filesystems are called versioning filesystems , though typically the details are slightly different from what you describe. Most importantly, however, although such filesystems exist for Linux, none of them are mainstream. To the best of my knowledge, none of the major Linux distributions even offers one as a distribution-supported option.

Although it's a bit dated, see also Linux file versioning?

You might be able to approximate that for many programs via a customized version of the C standard library, but that's not foolproof, and you should not expect it to have universal effect.

It would be an altogether different matter for an individual process to be coded for such behavior. It would need to check for existing files and choose an appropriate name when opening each new file. In doing so, some care needs to be taken to avoid related race conditions, but it can be done. Details would depend on the language in which you are writing.

You can use BASH expression to achieve this. For example if I wanted to make 10 files all with the same name, but having a unique number value I would do the following:

# touch my_file{01..10}.txt

This would create 10 files starting at 01 all the way to 10. This method is also hand for looping over files in a sequence or if your also creating directories.

Now if i am reading you question right your asking that if you move a file or create a file in a directory. you would want the a script to automatically create a new file for you? If that is the case then just use a test and if there is a file move that file and mark it. Me personally I use time stamps to do so.

Logic:

# The [ -f ] tests if the file is present
if [ -f $MY_FILE_NAME ]; then
    # If the file is present move the file and give it the PID
    # That way the name will always be unique
    mv $MY_FILE_NAME $MY_FILE_NAME_$$
    mv $MY_NEW_FILE .
else
    # Move or make the file here
    mv $MY_NEW_FILE .
fi

As you can see the logic is very simple. Hope this helps.

Cheers

I don't know about Your particular use case, but You may try to look at logrotate: https://wiki.archlinux.org/index.php/Logrotate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM