How to Change File Permissions Using Cron in Linux: Troubleshooting Guide

Managing file permissions is a common task in Linux administration. Automating this process using cron jobs can save time and ensure consistency. However, setting up cron jobs for file permissions and troubleshooting errors can sometimes be tricky. This article will guide you through changing file permissions using cron, and provide solutions for common errors you might encounter, such as the bad interpreter error.

Setting Up a Cron Job to Change File Permissions

Create the Script:

nano /opt/

Add the Following Content:

find /bank/OUT/ -type f -not -perm 777 -exec chmod 777 {} +

This script uses the find command to locate all files (-type f) in /bank/OUT/ that do not have 777 permissions (-not -perm 777), and changes their permissions (-exec chmod 777 {}).

Save and Exit:

Save the file and exit the editor.

Make the Script Executable:

chmod +x /opt/

Step 2: Set Up the Cron Job

Edit the Crontab:

Open the crontab for editing:

crontab -e

Add the Cron Job:

Add the following line to run the script every minute:

* * * * * /opt/

This entry means that the script will run every minute, ensuring that any new files added to /bank/OUT/ are promptly updated with 777 permissions.

Save and Exit:

Save the crontab file and exit the editor. The cron job is now set up.

Step 3: Verify the Cron Job

To ensure your cron job is working as expected:

  1. Check the Cron Log:
    • Check the cron log for any entries related to your script:

sudo grep CRON /var/log/cron

Verify File Permissions:

After a minute, check the permissions of files in /bank/OUT/:

ls -l /bank/OUT/

The files should now have -rwxrwxrwx permissions.

Troubleshooting Common Errors

Error: bad interpreter: No such file or directory


This error often occurs when a script has been created or edited on a Windows system, resulting in Windows-style line endings (\r\n) instead of Unix-style line endings (\n). The ^M character (\r) can be seen when viewing the file using cat -v and is interpreted incorrectly by Unix/Linux systems.


  1. Check for ^M Characters:
cat -v /opt/

If you see ^M at the end of lines, the file has Windows-style line endings.

Convert Line Endings Using dos2unix:

Install dos2unix if not already installed:

sudo yum install dos2unix  # For RHEL/CentOS

Convert the file:

dos2unix /opt/

Verify and Make Executable:

Check that the ^M characters are removed:

cat -v /opt/

Ensure the script is executable:

chmod +x /opt/

Test the Script:

Run the script manually to ensure it works:


Other Common Issues

Cron Job Not Running

  1. Check Cron Service: Ensure the cron service is running:
sudo systemctl status crond  # For RHEL/CentOS

Start the service if necessary:

sudo systemctl start crond

Verify Crontab Entry:

List crontab entries to ensure your job is listed correctly:

crontab -l

Redirect Output for Debugging:

Modify the cron job to log output and errors for troubleshooting:

* * * * * /opt/ >> /opt/cron.log 2>&1

Check /opt/cron.log for any error messages.

Use Absolute Paths:

Ensure all commands and file paths in your script are absolute paths. Cron jobs do not use the same environment variables as your shell.

Script Not Executable

Set Executable Permissions:

Make sure the script has executable permissions:

chmod +x /opt/

Check File Permissions:

Ensure the file is readable and executable by the cron user:

ls -l /opt/

Environment Variables\

Define Required Environment Variables:

If your script relies on specific environment variables, define them in the script or source them from a profile:

* * * * * . /etc/profile; /opt/

Verify Script Content:

Ensure there are no commands in the script that rely on a specific user environment or interactive prompts.


Automating file permission changes using cron can simplify system administration tasks. By creating a script and scheduling it with cron, you can ensure files in specific directories always have the correct permissions. Troubleshooting common issues, like the bad interpreter error, involves checking for and removing unwanted Windows-style line endings and ensuring the script is properly executable. By following these steps, you can successfully manage file permissions and resolve any cron-related issues.

Hey folks, I'm Vivek Kumar Pandey, a software engineer with a passion for crafting elegant solutions to complex problems. From the bustling streets of Mumbai to the heart of Bangalore's tech scene, I've journeyed through the world of programming, leaving my mark one line of code at a time. Join me as I continue to explore, innovate, and push the boundaries of what's possible in the digital realm.

Related Posts

How to Run a Command in the Background Using Linux: A Comprehensive Guide

Running commands in the background on a Linux system is essential for efficiently managing long-running tasks and services. This allows you to continue using the terminal for…

Understanding Disk Space and Memory Usage in Linux: A Guide for System Administrators

As a system administrator, monitoring and managing system resources is crucial to ensure smooth and efficient operations. Two key aspects of resource management in a Linux environment…

Understanding iptables and Allowing Specific Ports

iptables is a powerful tool for configuring the Linux kernel firewall, allowing you to manage incoming and outgoing traffic on your server. By defining rules, you can…

How to Delete Files from a Specific file names Using the find Command

Managing log files is an essential part of system administration. Over time, log files can accumulate and consume significant disk space. In this article, we will demonstrate…

Leave a Reply

Your email address will not be published. Required fields are marked *