0

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 e.g. 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.

2
  • 3
    Note well that abc.txt and abc.txt.1 are manifestly not the same name. What you actually want to do seems clear enough, but the words you use to describe things to others and (especially) to yourself matter. Commented Jul 10, 2020 at 19:54
  • This gets problematic quickly. Should a link to the file point to the latest version, or the version that existed when the file was created? Commented Jul 11, 2020 at 21:59

3 Answers 3

1

You ask,

For e.g. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.