Category: Shell Scripting

  • LINUX: Removing Files Older Than x Days

    It can often be useful to remove files that are unnecessary, such as log files, backup files, etc, when it is not already done automatically. Fortunately there is a very simple command to do just that. Using the find command, it is possible to find the files in the folder you want to clean out […]

  • Automatically Check RSYNC and Restart if Stopped

    I occasionally use RSYNC to synchronize large directories of files between servers. This is especially useful if you’re moving a client from one server to another and they have alot of static files that are always changing. You can copy the files and sync them up, all with RSYNC and if your connection gets cut […]

  • Getting the Last Modification Timestamp of a File with Stat

    If we want to get just the date modified, for a file, in a format of our choosing. This can be done with a utility called stat. The syntax is as follows: stat -f -t “” In this example, we are printing just the date created in the format YYYYMMDD_HHMMSS. stat -f “%Sm” -t “%Y%m%d_%H%M%S” […]

  • Using the Linux Command Line to Find and Copy A Large Number of Files from a Large Archive, Preserving Metadata

    One of my recent challenges is to go through an archive on a NAS and find all of the .xlsx files, then copy them; preserving as much of the file metadata (date created, folder tree, etc) as possible, to a specified folder.  After this copy, they will be gone through with another script, to rename […]

  • Explicitly Setting log4j Configuration File Location

    I ran into an issue recently, where an existing log4j.xml configuration file was built into a jar file I was referencing and I was unable to get Java to recognize another file that I wanted it to use instead.  Fortunately, the solution to this problem is fairly straightforward and simple. I was running a standalone […]

  • Appending to a Remote File via SSH

    Most LINUX users know how to copy and overwrite a file from one server to another; but it can also be useful to directly append to a file, without having to login to the remote server and make the changes manually. This does not appear to be possible with the commonly used SCP utility; however, […]

  • RSYNC a File Through a Remote Firewall

    One of my recent tasks was to set-up a new automatic backup script, which dumps out the MySQL database on the remote host at a regular time, at a later time, it is RSYNC’d from the backup server through a remote firewall. I must say that I was a little surprised, to discover that the […]

  • Quick and Easy Regular Expression Command/Script to Run on Files in the Bash Shell

    I often find it necessary to run regular expressions on, not just one file; but instead a range of files. There are perhaps dozens of ways this can be done, with varying levels of understanding necessary to do this. The simplest way I have encountered utilizes the following syntax: perl -pi -e “s///g” Here is […]

  • Sending Mail in Shell Scripts via an External Server with Nail

    If you’ve ever tried sending email via the command line, using the mail utility, you may find that the method can be unreliable in some cases.  The email messages are often intercepted by spam bots, filtered by security programs, etc.  A more elegant and stable alternative, is to use your existing email server to send […]

  • Copying Yesterday’s Exceptions with Stack Traces from Logs, Then Emailing To Administrators

    When you have a java application server which generates a great deal of logs, it can be tricky to find the most important information, especially if you have detailed logging. Fortunately grep is capable of doing this very well. The following command will gather all WARN, ERROR, FATAL, and Exception stack traces. This command can […]