Archive

Articles taggués ‘scripting’

Bash Shell Loop Over Set of Files

16/03/2024 Comments off

bash shell loopBash Shell Loop

How do I run shell loop over set of files stored in a current directory or specified directory?

You can use for loop easily over a set of shell file under bash or any other UNIX shell using wild card character.

Syntax

The general syntax is as follows:

for f in file1 file2 file3 file5
do
 echo "Processing $f"
 # do something on $f
done

You can also use shell variables:

FILES="file1
/path/to/file2
/etc/resolv.conf"
for f in $FILES
do
	echo "Processing $f"
done

You can loop through all files such as *.c, enter:

$ for f in *.c; do echo "Processing $f file.."; done

Lire la suite…

Categories: Système Tags: , , ,

Delete files by creation date

04/03/2024 Comments off

source: http://ubuntuforums.org/showthread.php?t=625132
On the command line, you can use the “find” command to select certain files, and then use the “-exec” switch to cause some other command to be run on those files. So if you use “-exec rm” you will delete the files that are found. So, for example:

Code:
 find -mmin +4 -exec rm {} +

will delete any files in the current directory (and sub-directories) that are older than 4 minutes. This is because the “-mmin +4” switch causes find to return files older than 4 minutes. There are other options, like “-mtime” that return files based on modified date in days. You can use + or – depending on what kind of behavior you are trying to achieve. For a more complete explanation of all the options, see the manual page:
http://unixhelp.ed.ac.uk/CGI/man-cgi?find

Be careful when using the rm command! It’s usually a good idea to test your command first, before using it. So, for instance, use something like:

Code:
 find -mmin +4 -exec ls {} +

which will just list the files that you are selecting for. If the list looks right, then you can switch the “ls” to “rm” in the command and it will delete the files.

Categories: Système Tags: ,

Configuring Log Rotation of Apache2 and Other Logs

22/02/2024 Comments off

source: lifeonubuntu.com

I went to check out my apache2 logs

ls /var/log/apache2/

and I noticed that they were being automatically rotated (access.log, access.log.1, etc.) and compressed with gzip (access.log.2.gz, etc.). This seems to be the default Ubuntu configuration. I wanted to make find out more, and I found this helpful article about Ubuntu logs, including Apache2 Log info and some basic log rotation info.

After reading through the info, I decided that I wanted to make a few changes. The log rotation happens via the brilliantly named logrotate command. It turns out that logrotate settings kept in 2 places. Lire la suite…

Cron Job Tutorial: Crontab Scheduling Syntax and Script Example

29/01/2024 Comments off

source: Devshed

Crontabs is very useful when doing website maintenance. You can use this feature for the following purposes:

  1. Deleting all files in a certain folder at regular intervals. If you accept user-uploaded files, then eventually it will clog your web hosting server. If you do not regularly delete these files, they will consume a lot of disk space and can slow down your website.
  2. Deleting files of a specific type at regular intervals. You can also choose to delete files of a specific type, instead of deleting all of the files in the folder. If you have PHP, HTML and MP3 files together inside the folder, you can delete only the MP3 files.
  3. Regularly backing up your MySQL database. This is one of the most important webmaster tasks. By using crontabs or the cron job feature, you can create a PHP script that will back up a selected MySQL database, and have it execute automatically executed at a specific, regular interval (e.g monthly, yearly, etc).

This tutorial will focus on creating a cron PHP script application, then configuring your cron hosting feature to execute these scripts automatically. Lire la suite…

Categories: Système Tags: , , ,

Diff à distance

16/01/2024 Comments off

I just had a problem to solve: Compare two server config files on two servers to make sure they’re the same. Rather than using scp to copy the file from one machine to another, I used ssh’s ability to run commands remotely to get the contents of the file and piped it into diff. Here’s an example:

ssh user@server1 'cat /path/to/config/file.conf' | diff /path/to/other/config/file.conf -

I’ll leave it as an exercise for the user to write a shell script that will do this automatically, though it should be fairly easy to do.

Source: movetoiceland.com

Categories: Système Tags: , , ,