linux - Unix - Prevent files with specific name from being in a directory

06
2014-04
  • Binh

    I need to prevent users from accidentally copy/move files with a certain names into a specific directory. Is it possible ?

    I need to set something in directory /home/similar2 that will reject any attempts to put files with names mytimesheet. into it. Files not matching these can be put in the directory. Only need this for command line.

    IBM Red Hat Enterprise Linux (RHEL). I'd love any generic Unix solution also. Thanks. Binh

  • Answers
  • LMNOP

    You could create an alias for the cp/mv commands in the user's .bashrc that checks where they're sending it before actually executing the command, and make their .bashrc immutable. You could also send a warning if its one of the files you don't want this way. This would prevent them from copying/moving files to that directory through a bash shell. Don't know if it would prevent them from copying/moving it in some other way, though; it depends if the other methods reference the active user's cp/mv commands. I don't know enough about all the ways to copy or move a file in Red Hat to know how much this would cover.

  • Travis Banger

    You could run a cron job, say, every 5 minutes and check those directories. Any offending file would be promptly removed.

    Works in all flavors of Unix.

    A complete, foolproof solution requires modifying the kernel or at least the filesystem software.

    In Windows this would be a lot easier, with Watched Directories.


  • Related Question

    Moving files on linux, appending existing directories in destination
  • Questioner

    So let's assume I have:

    dest/dir/file1
    dest/dir/file2
    dest/dir/subdir/subfile1
    dest/dir/subdir/subfile2
    

    and

    src/dir/file3
    src/dir/subdir/subfile3
    src/dir/newdir/anotherfile
    

    ... and let's assume there are thousands of files and subdirectories.

    Now I want to move all the new files from src to dest, to their respective directories, creating new subdirectories when needed. This would be the equivalent of "cp -a src/dir dest/", but as a move operation. The obvious command "mv src/dir dest/" fails as the directory already exists in the destination.

    How can I do this with one command?


  • Related Answers
  • Area 51

    You can use rsync.

    rsync -av --remove-source-files src/ dest/
    

    Unfortunately that won't remove the directories though. You could just add a further command to remove them.