How To Bash Script For Mass Changes

How To Bash Script For Mass Changes

You can use some bash scripting to make for loops and have mass changes in a Linux Server. Follow the simple steps below.

Collect the data that is needed to be changed.

  • Here we will use the “find” command to look for 777 directories under user “whomever.”
  • find /home/whomever/public_html -type d -perm 777
  • What this does is find in directory /home/whomever/public_html, any directories (-type d) that are 777 permissions (-perm 777).
  • You can change this to work with whatever directories you want, permissions you want, etc.

Alright that’s neat, but kind of hard to read if you have a lot of 777 permissions in this directory. Let make them all go into a file.

  • Start with the same command, but we’ll add something to make it go into a file we create.
  • find /home/whomever/public_html -type d -perm 777 > badpermlist
  • What this does is take our query and creates a file called “badpermlist” where all the data will be put.

Now that we have our data, we can start with some basic scripting.

Verify your data

  • for each in `cat badpermlist` press -enter-
  • do press -enter-
  • ll -d $each press -enter-
  • done | more press -enter
  • This will show your data that is in the file.
  • IMPORTANT: the ` ` marks are the ones that are under the ~ key, NOT the apostrophe key

Change the data

  • for each in `cat badpermlist` press -enter-
  • do press -enter-
  • chmod 755 $each press -enter-
  • done press -enter-
  • Congrats, you just changed all those pesky 777 permissions to 755 like they should be.

Verify that the change took place because it is never a bad idea to check your work.

  • for each in `cat badpermlist` press -enter-
  • do press -enter-
  • ll -d $each press -enter-
  • done | more press -enter-
  • Now all the data that showed up as 777 should now show as 755.

Now as you can tell, there is a lot of potential to a very simple script like this. I hope this helps breakdown some basic concepts for you and helps you on your scripting way!

Note

  • Play with changing the items specified in a “for loop,” just remember what you changed so you can change it back!
  • Watch that you use the ` ` (backwards apostrophes) symbols under the ~ (til-day). If you use ‘ ‘ (apostrophes) it will NOT work!

Tags:

Raja Rajan Avatar

Help Us Grow

If you like this post, please share it with your friends.

You are free to copy and redistribute this article in any medium or format, as long as you keep the links in the article or provide a link back to this page.

Subscribe to Newsletter




Privacy Settings

Privacy & Cookie Overview

Our website uses cookies to provide you with the best user experience possible. These cookies are stored in your browser and perform essential functions such as recognizing you when you return to our website, as well as helping us to understand which sections of the website you find most useful and engaging.

To learn more, you can read our Privacy & Cookie Policy or reach out through our Contact form.

Strictly Necessary Cookies

Strictly Necessary Cookies must always be enabled to ensure the proper functioning of this website and to allow us to provide you with excellent service. These cookies are also essential for saving your cookie preferences.

Google Adsense

We use Google AdSense to keep this site free by displaying relevant ads. AdSense requires essential cookies that cannot be disabled, but you can manage other cookies. We respect your privacy and provide options to control non-essential cookies.

For more details on how Google handles your data, visit Google's Data Usage Policy. Please review our Privacy Policy for more information on how we protect your data.

AddToAny

We use AddToAny for social sharing. It doesn’t store cookies, ensuring a privacy-friendly experience. AddToAny complies with GDPR and CCPA by default.

For more, see their Privacy Policy.

OneSignal

We use OneSignal to send notifications to users who opt in. OneSignal complies with GDPR and is certified under the EU-US and Swiss-US Privacy Shield frameworks.

For more, see their Privacy Policy.

3rd Party Cookies

This website utilizes third-party cookies, which can enhance your experience and support our ongoing efforts to improve our services.

Google Analytics

We use Google Analytics to collect anonymous data, such as visitor numbers and popular pages, to improve user experience and site performance. Keeping this cookie enabled helps us refine the site based on visitor activity.

For more information, see Google’s Privacy Policy.

Discover more from Prime Inspiration

Subscribe now to keep reading and get access to the full archive.

Continue reading