Configuring a Recycle Bin for Network Shares in CentOS
To implement a recycle bin feature for network-shared folders in CentOS (typically via Samba), follow these structured steps. This ensures deleted files are retained in a structured recovery directory instead of being permanently removed.
First, ensure Samba is installed (required for network sharing) and its dependencies are up to date.
sudo yum install samba samba-client samba-common -y
sudo systemctl start smb
sudo systemctl enable smb
Choose a directory to share (e.g., /data/shared) and create a subdirectory for the recycle bin (e.g., .recycle). The recycle bin directory will store deleted files, organized by user (using %U for the username).
sudo mkdir -p /data/shared
sudo mkdir -p /data/shared/.recycle
Edit the Samba configuration file (/etc/samba/smb.conf) to define the shared folder and enable the recycle bin feature. Add the following section at the end of the file:
[SharedFolder]
comment = Shared Folder with Recycle Bin
path = /data/shared
browseable = yes
writable = yes
valid users = @shared_users # Replace with your user/group
create mask = 0775
directory mask = 0775
vfs objects = recycle
recycle:repository = .recycle/%U # Per-user recycle bin
recycle:keeptree = yes # Retain original directory structure
recycle:versions = yes # Keep multiple versions of files
recycle:maxsize = 0 # Unlimited size (set to a value like 100M for limits)
recycle:exclude = *.tmp|*.log # Files to exclude from recycle bin
Key parameters:
vfs objects = recycle: Enables the recycle bin module.recycle:repository = .recycle/%U: Stores deleted files in a .recycle subdirectory named after the user (e.g., .recycle/john).recycle:keeptree = yes: Maintains the original folder structure of deleted files.recycle:versions = yes: Prevents overwriting of files with the same name (keeps versions).Ensure the shared folder and recycle bin have correct ownership and permissions. Replace shared_users with your actual user/group:
sudo chown -R root:shared_users /data/shared
sudo chmod -R 2775 /data/shared # Sets SGID to inherit group ownership
sudo chmod -R 1777 /data/shared/.recycle # Sticky bit: Users can only delete their own files
Add a Samba user (must be a system user first) and restart Samba to apply changes:
sudo useradd -m shared_user # Create system user
sudo smbpasswd -a shared_user # Set Samba password
sudo systemctl restart smb
To test the shared folder from a Windows client, map it using the UNC path (\\<CentOS_IP>\SharedFolder) and provide the Samba credentials. Deleted files will now appear in the .recycle/<username> directory on the CentOS server.
recycle:exclude to skip temporary files (e.g., *.tmp, *.log) from the recycle bin.recycle:maxsize to a specific value (e.g., 100M) to prevent the recycle bin from consuming too much disk space.cron jobs to periodically empty old files from the recycle bin (e.g., delete files older than 30 days).This setup provides a functional recycle bin for network shares, ensuring data recovery and structured file management.