Tag Archives: Linux How to

Unix Similar multi-user operating systems based on the Linux kernel and essentially on GNU software. Like CentOS, Debian, Ubuntu Fedora.

Find files by modified date with option mtime

Find is the command of choice when searching for files and their modified date and time using the option mtime. The Find Command Line Tool offers the option mtime and many more, also useful for applied in shell scripts. The find --help command gives help, and man find shows the man page.

Find Files using mtime and atime

find files with option mtime

In the file system, each file has three timestamps that are changed when certain operations are performed on the file:

  • [a] access (read the contents of the file) – atime
  • [b] change state (change the file or its attributes) – ctime
  • [modify] change the contents of the file – mtime

Files can be searched for with timestamps within a certain age range, or they can be compared to other timestamps.

Find Files with Option -mtime (modify)

The -mtime option returns a list of files if the file was last accessed N*24 hours ago. For example, to find a file from the last month (N=30 days), the -mtime +30 option can be used.

  • -mtime +30 means find file modified 30 days ago.
  • -mtime -30 means less than 30 days.
  • -mtime 30 without + or – means exactly 30 days.

For example, to find text files that were last modified 30 days ago, ran this command:

$ find /home/user -iname "*.txt" -mtime -30 -print

Show contents of files last modified 30 days ago ran the command:

$ find /home/user -iname "*.txt" -mtime -30 -exec cat {} \;

Count the total number of TXT files using wc (Word Count):

$ find /home/user -iname "*.txt" -mtime -30 | wc -l

Delete gzip archive files older than 30 days with ran this command:

$ find /home/user/*.gz -mtime +30 -exec rm {} \;

Find Files with Option -atime (access)

Search by access time, the following command returns the list of all .txt files that have been accessed in the last 30 days:

$ find /home/user -iname "*.txt" -atime -30 -type -f

List all json files accessed exactly 14 days ago:

$ find /home/user -iname "*.json" -atime 14 -type -f

Note. the switch -type f – search for files only exclude directories.

May find some string recursive in all files from the current directory.

$ find . -type f -print0 | xargs -0 grep "some string"

For example, to change all files recursively with chmod from the current directory, but not the directories.

$ find . -type f -print0 | xargs -0 chmod 0644

and vice versa, change all directories recursively with chmod from the current directory, but not the files.

$ find . -type d -print0 | xargs -0 chmod 0755

Find Files with Option -daystart

The -daystart option is used to measure time from the start of the current day instead of 24 hours ago. Find all C++ files (*.CPP) changed yesterday with the following command:

$ find /home/user -iname "*.CPP" -daystart -mtime 1

To list all LOG files in /var/log accessed yesterday use this command:

$ find /var/log -iname "*.log" -daystart -type f -mtime 1

List C++ files modified 2-7 days ago with this command:

$ find /home/user -iname "*.CPP" -daystart -mtime 2 -mtime -7

To find files in the /home/user directory tree that are newer than the files in /mnt/user, ran the command:

$ find home/user -newer /mnt/user

Conclusion

The find command-line tool can be used in the Linux shell to find files by their modification date. With its numerous options, the Command Line Tool offers many possibilities, which are also useful in script processing.

Sendmail using echo message attachment on Linux

sendmail send message and attachment use echo. sendmail implements Postfix’s compatibility interface and is present in almost all Unix-like operating systems, as mail transfer agent whose history goes back to the early 1980s.

How to sendmail echo message attachment

Sendmail using echo message attachment on Linux

sendmail sends message over the Internet to a specific recipient. This can be done by running the sendmail command on a Linux command line, and can also be used within a shell script.

quote  sendmail: fatal: open /etc/postfix/main.cf: No such file or directory

Make sure sendmail is present on the Linux host with the command which sendmail or man sendmail. If sendmail is not found and no MTA like Postfix is installed, sendmail can be deployed as follows.

$ sudo apt install sendmail -y

sendmail in the command line

The following example shows a sendmail command to send a message from the command line with using the echo command.

$ echo -e "Subject:Test Mail using sendmail \nThis is a Test Message\n" | sendmail youremail@example.tld

The next command sends an email with sender (From:) address.

$ /usr/sbin/sendmail youremail@example.tld
From: nobody@example.tld
Subject: Test Mail using sendmail
This is a Test Message
.

  Enter period (.) ends the editor and the e-mail will by submit.

Example email is interactively created by each line, a message with the subject delivered to inbox of youremail@example.tld.

Change the recipient accordingly, make sure sendmail is present on the host and the host is allowed to send email.

Further, sendmail robs the pipeline instruction, thereby making it possible to send a message using a text file.

$ /usr/sbin/sendmail < message.txt -t -i youremail@example.tld

Example with the content of the message.txt file.

From: nobody@example.tld
Subject: Test Mail using sendmail
This is a Test Message

  The sendmail -i option handles reading the message from standard input without a period (.) at the end of a line.

The -t option extracts the recipients from the message headers and adds them to all recipients specified on the command line.

How to send message with attachment use sendmail

sending an email directly from a host, in this example sendmail is used to send a file as an attachment.

Here is to filter out the delivery attempts rejected as SPAM “blocked using” on a Postfix MTA, and to send them as archives.txt.gz, for this purpose.

Postfix must be configured on the host, it can also be another local MTA, or an external SMTP-Relay is used.

#!/bin/bash
cat /var/log/maillog* | grep 'blocked using' | grep 'middleton.tld' > /tmp/blocked.txt
gzip -f /tmp/blocked.txt
( echo "to: sysop@example.tld"
  echo "from: no_reply@$HOSTNAME"
  echo "subject: Report of messages blocked by $HOSTNAME"
  echo "mime-version: 1.0"
  echo "content-type: multipart/related; boundary=messageBoundary"
  echo
  echo "--messageBoundary"
  echo "content-type: text/plain"
  echo
  echo "Please find the document attached."
  echo
  echo "--messageBoundary"
  echo "content-type: text/plain; name=blocked.txt.gz"
  echo "content-transfer-encoding: base64"
  echo
  openssl base64 < /tmp/blocked.txt.gz) | sendmail -t -i

  The -t option extracts the recipients from the message header.
-i option don´t treat a . character as the end of input.

Conclusion

This example shows how to use sendmail to send an email with attachment directly from the Linux shell.

By changing the individual lines, many other applications can come into play, whereby the Base64 encoding is done by openssl, the echo statements performs the MIME message body.

  more related in this post and here.