delete


Linux: Delete all files that are older than X days

The command find /data/ -type f -mtime +15 -exec rm -f '{}' \; is used to search and delete all the files in the “/data/” directory that have a modification time of more than 15 days old. The following is an explanation of each part of the command:

  1. “find /data/” – This specifies the directory that the search will start from; in this case, it’s the “/data/” directory.
  2. “-type f” – This option specifies that the search should be limited to files, not directories.
  3. “-mtime +15” – This option specifies that the files should be older than 15 days based on the modification time. The “+” sign indicates that we are looking for files older than 15 days.
  4. “-exec rm -f ‘{}’ \;” – This option is used to execute a command on the files found. The command “rm -f ‘{}'” is used to delete the files and the “{}” is a placeholder for the files that are found. The “” at the end of the line is used to escape the semicolon and avoid a syntax error.

The “find /data/ -type f -ctime +15 -exec rm -f ‘{}’ \;” command is similar to the above command, but it searches for files based on their creation time instead of modification time. The “ctime” option specifies that the search should be based on the file creation time instead of the modification time.

In conclusion, both commands are used to delete files in the “/data/” directory that are older than 15 days. Still, the difference is that the first command searches for files based on their modification time, while the second command searches for files based on their creation time.


JavaSript: Remove all non printable and all non ASCII characters from text 1

According to the ASCII character encoding, there are 95 printable characters in total.
Those characters are in the range [0x20 to 0x7E] ([32 to 126] in decimal) and they represent letters, digits, punctuation marks, and a few miscellaneous symbols.
Character 0x20 (or 32 in decimal) is the space character ' ' and
character 0x7E (or 126 in decimal) is the tilde character '~'.
Source: https://en.wikipedia.org/wiki/ASCII#Printable_characters

Since all the printable characters of ASCII are conveniently in one continuous range, we used the following to filter all other characters out of our string in JavaScript.


printable_ASCII_only_string = input_string.replace(/[^ -~]+/g, "");

What the above code does is that it passes the input string through a regular expression which will match all characters out of the printable range and replace them with nothing (hence, delete them).
In case you do not like writing your regular expression with the space character to it, you can re-write the above regular expression using the hex values of the two characters as follows:


printable_ASCII_only_string = input_string.replace(/[^\x20-\x7E]+/g, "");


Delete all empty directories and all directories containing empty directories

Assuming you have a complex filesystem from which you want to delete all empty directories and all directories containing empty directories recursively, while leaving intact the rest.
You can use the following command.

find . -type d -empty -delete;

The configuration we used is the following:

  • -type d restricts the results to directories only
  • -empty restricts to empty directories only
  • -delete removes the directories that matched

The above code will delete all directories that are either empty or contain empty directories in the current directory.
It will successfully delete all empty directories even if they contain a large number of empty directories in any structure inside them.