HomeRecipesSnippets


find

Text

Category: Bash
Date: Jan. 28, 2025, 11:35 p.m. PST
Tags: cli,find

<style> table.find {font-family: verdana, arial, sans-serif; font-size: 12px; margin: 1em auto; } input[type=text] {border: 1px solid #ababab; width: 12em;} textarea { font-family: monospace; width: 100%; padding: 2px; margin: 0; border: none; background: #dcedb4; } select {border:1px solid #ababab } tr:nth-of-type(odd) { background-color:#ddd; } td { padding: 4px; } tr td:first-child { text-align: right; padding-right: 1em; } .center { text-align: center; } input.small { width: 3em; } input[type=text] { border-radius: 4px; padding: 2px; } input.large { width: 30em; } </style>

<h1 class="center"><code>find</code> Builder</h1> <table class="find"> <tr> <td>Search directory</td> <td><input class="large" type="text" id="path" value="."></td> </tr> <tr> <td>File name</td> <td> <input type="text" id="name" value=""> <select id="nameConnect"> <option value=" -o " selected>or</option> <option value=" -a ">and</option> </select> <input type="text" id="name1" value=""> </td> </tr> <tr> <td>Skipping directories/files the pattern just matched</td> <td><input type="checkbox" id="prune" value=" -prune "></td> </tr> <tr> <td>Execute command</td> <td> <input type="text" id="exec" value=""> with confirmation? <input type="checkbox" id="ok" value=" -ok "> </td> </tr> <tr> <td>List current file in <code>ls -dils</code> format</td> <td> <input type="checkbox" id="ls" value=" -ls "> </td> </tr> <tr> <td>Print filename</td> <td> <input type="checkbox" id="print" value=" -print " checked> </td> </tr> <tr> <td>Process directory contents before directory itself</td> <td> <input type="checkbox" id="depth" value=" -depth "> </td> </tr> <tr> <td>Directory depth</td> <td> <select id="dirDepthChecker"> <option value=" -maxdepth " selected>Maximum</option> <option value=" -mindepth ">Minimum</option> </select> <input class="small" type="text" id="dirDepth" value=""> </td> </tr> <tr> <td>File was created (N days ago)</td> <td> <select id="ctimeChecker"> <option value="" selected> == </option> <option value="-"> &lt; </option> <option value="+"> &gt; </option> </select> <input class="small" type="text" id="ctime" value=""> </td> </tr> <tr> <td>File was modified (N days ago)</td> <td> <select id="mtimeChecker"> <option value="" selected> == </option> <option value="-"> &lt; </option> <option value="+"> &gt; </option> </select> <input class="small" type="text" id="mtime" value=""> </td> </tr> <tr> <td>File was accessed (N days ago)</td> <td> <select id="atimeChecker"> <option value="" selected> == </option> <option value="-"> &lt; </option> <option value="+"> &gt; </option> </select> <input class="small" type="text" id="atime" value=""> </td> </tr> <tr> <td>File was modified more recently than file</td> <td> <input type="text" id="newer" value=""> </td> </tr> <tr> <td>File type</td> <td> <select id="type"> <option value="" selected>Any</option> <option value=" -type f">Regular file</option> <option value=" -type d">Directory</option> <option value=" -type l">Symbolic link</option> <option value=" -type p">Fifo file</option> <option value=" -type b">Block special file</option> <option value=" -type c">Character special file</option> <option value=" -type s">Socket</option> </select> </td> </tr> <tr> <td>File size (in bytes)</td> <td> <select id="sizeChecker"> <option value=" " selected>==</option> <option value=" -">&lt;</option> <option value=" +">&gt;</option> </select> <input class="small" type="text" id="size" value=""> <select id="sizeSuffix"> <option value="c">bytes</option> <option value="k">kilobytes</option> <option value="M">megabytes</option> <option value="G">gigabytes</option> <option value="T">terabytes</option> <option value="P">petabytes</option> </select> </td> </tr> <tr> <td>File has inode number</td> <td> <select id="inumChecker"> <option value=" " selected>==</option> <option value=" -">&lt;</option> <option value=" +">&gt;</option> </select> <input class="small" type="text" id="inum" value=""> </td> </tr> <tr> <td>File has links</td> <td> <select id="linksChecker"> <option value=" " selected>==</option> <option value=" -">&lt;</option> <option value=" +">&gt;</option> </select> <input class="small" type="text" id="links" value=""> </td> </tr> <tr> <td>File permision for user</td> <td> <select id="permUserChecker"> <option value="u=">==</option> <option value="u+">with</option> <option value="u-">without</option> </select> <label>r <input id="permUserR" type="checkbox" value="r"></label> <label>w <input id="permUserW" type="checkbox" value="w"></label> <label>x <input id="permUserX" type="checkbox" value="x"></label> set-ID <input id="permUserS" type="checkbox" value="s"> </td> </tr> <tr> <td>File permision for group</td> <td> <select id="permGroupChecker"> <option value="g=">==</option> <option value="g+">with</option> <option value="g-">without</option> </select> <label>r <input id="permGroupR" type="checkbox" value="r"></label> <label>w <input id="permGroupW" type="checkbox" value="w"></label> <label>x <input id="permGroupX" type="checkbox" value="x"></label> <label>set-ID <input id="permGroupS" type="checkbox" value="s"></label> </td> </tr> <tr> <td>File permision for other</td> <td> <select id="permOtherChecker"> <option value="o=">==</option> <option value="o+">with</option> <option value="o-">without</option> </select> <label>r <input id="permOtherR" type="checkbox" value="r"></label> <label>w <input id="permOtherW" type="checkbox" value="w"></label> <label>x <input id="permOtherX" type="checkbox" value="x"></label> </td> </tr> <tr> <td>Special permission</td> <td> <select id="permSpecialChecker"> <option value="+">with</option> <option value="-">without</option> </select> <label>sticky bit <input id="permSpecialT" type="checkbox" value="t"></label> <label>mandatory locking <input id="permSpecialL" type="checkbox" value="l"></label> </td> </tr> <tr> <td>Don't descend directories on other filesystems</td> <td> <input type="checkbox" id="mount" value=" -mount "> </td> </tr> <tr> <td>Filesystem to which the file belongs is of type</td> <td> <select id="fstype"> <option value="" selected>&nbsp;</option> <option value=" -fstype ufs">ufs</option> <option value=" -fstype 4.3">4.3</option> <option value=" -fstype 4.2">4.2</option> <option value=" -fstype nfs">nfs</option> <option value=" -fstype tmp">tmp</option> <option value=" -fstype mfs">mfs</option> <option value=" -fstype S51K">S51K</option> <option value=" -fstype S52K">S52K</option> <option value=" -fstype vxfs">vxfs</option> <option value=" -fstype zfs">zfs</option> </select> <input class="small" type="text" id="fstypeTxt" value=""> </td> </tr> <tr> <td>Dereference symbolic links</td> <td> <input type="checkbox" id="follow" value=" -follow "> </td> </tr> <tr> <td>No user corresponds to file's numeric user ID</td> <td> <input type="checkbox" id="nouser" value=" -nouser "> </td> </tr> <tr> <td>No group corresponds to file's numeric group ID</td> <td> <input type="checkbox" id="nogroup" value=" -nogroup "> </td> </tr> <tr> <td>Current file or directory is empty</td> <td> <input type="checkbox" id="empty" value=" -empty "> </td> </tr> <tr> <td>File belongs to a user</td> <td> <input type="text" id="user" value=""> </td> </tr> <tr> <td>File belongs to a group</td> <td> <input type="text" id="group" value=""> </td> </tr> </table> <textarea id="result" rows="2"></textarea> <p class="center"> <input class="btn btn-primary btn-sm" type="reset" value="Reset" onclick="result.innerHTML = '';"> </p> <script> (function() { "use strict"; const get = id => document.getElementById(id); const value = id => get(id).value;

    const permString = (perms, prefix) => {
        let bits = '';
        for(const c of perms.toUpperCase()) {
            if(get(prefix + c).checked) {
                bits += c;
            }
        };
        return bits ? ' -perm ' + value(prefix + 'Checker') + bits : bits;
    };

    const simpleChecked = (f, ...ids) => {
        ids.forEach(id => {
            const o = get(id);
            o.checked && f.push(o.value);
        });
    };

    const firstChecked = (f, ...ids) => {
        for(const id of ids) {
            const o = get(id);
            if(o.checked) {
                f.push(o.value);
                return
            }
        }
    };

    const simpleChecker = (prefix, extra='') => {
        const val = value(prefix);
        let str = '';
        if(val != '' ) {
            str = ` -${prefix} ${value(prefix + 'Checker')}${val}${extra}`;
        }
        return str;
    };

    const showValue = () => {
        let f = [
            'find ' + value('path') + value('type'),
            simpleChecker('atime'),
            simpleChecker('ctime'),
            simpleChecker('mtime')
        ];


        if(value('newer') != '' ) {
            f.push(' -newer ' + value('newer'));
        }

        let val = value('name');
        if(val != '' ) {
            const name1Val = value('name1');
            if(name1Val != '' ) {
                f.push(
                    ' \\( -name "' + val + '"' + value('nameConnect') +
                    ' -name "' + name1Val + '" \\) '
                );
            }
            else {
                f.push(' -name "' + val + '"');
            }
        }

        f.push(simpleChecker('size', value('sizeSuffix')));
        firstChecked(f, 'depth', 'prune');

        simpleChecked(f, 'empty', 'follow', 'mount');
        f.push(simpleChecker('inum'), simpleChecker('links'));

        if(value('fstypeTxt') != '' ) {
            f.push(' -fstype ' + value('fstypeTxt'));
        }
        else {
            f.push(value('fstype'));
        }

        simpleChecked(f, 'nogroup', 'nouser');

        if(value('user') != '' ) {
            f.push(' -user ' + value('user'));
        }

        if(value('group') != '' ) {
            f.push(' -group ' + value('group'));
        }

        val = value('dirDepth');
        if(val != '') {
            f.push(value('dirDepthChecker') + val);
        }

        f.push(
            permString('rwxs', 'permUser'),
            permString('rwxs', 'permGroup'),
            permString('rwx',  'permOther'),
            permString('tl',   'permSpecial')
        )

        val = value('exec');
        if(val != '' ) {
            if(get('ok').checked ) {
                f.push(' -ok ' + val + ' {} \\; ');
            }
            else {
                f.push(' -exec ' + val + ' {} \\; ');
            }
        }

        firstChecked(f, 'ls', 'print');
        get('result').innerHTML = f.join('');
    };

    document.querySelectorAll('input[type=checkbox],select').forEach(
        el => el.addEventListener('change', showValue)
    );

    document.querySelectorAll('input[type=text]').forEach(
        el => el.addEventListener('keyup', showValue)
    );

    showValue();
})();

</script>