Question

I am trying to compare the output from a "find" file search with dictionary words in order to find specific hidden files. If the hidden file name is a dictionary word, I'd like to be prompted to delete it, if and only if it is a dictionary word.

Here is what I have so far...

find . \( -iname '*~' -o -iname '.*' \) -type f -exec sudo rm -i '{}' \;

I was thinking I could use the dictionary words from /usr/share/dict/words, but I'm not sure if that is the best/easiest option.

I am pretty content with the performance of the above code. The only thing it is missing, is the comparison to a dictionary word.

Thanks in advance for the help!

Bonus points: I am not totally sure why the '*~' is needed.

Was it helpful?

Solution

I am not sure if you can do comparison of this kind with find. However, you can try something like:

Step 1: Create a file by redirecting the output of your find command.

$ ls -lart
total 0
drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
drwxr-xr-x   2 jaypalsingh  staff   68 Jun 19 00:53 .

$ touch .help .yeah .notinmydictionary

$ ls -lart
total 0
drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
-rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .yeah
-rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .notinmydictionary
-rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .help
drwxr-xr-x   5 jaypalsingh  staff  170 Jun 19 00:54 .

$ find . \( -iname '*~' -o -iname '.*' \) -type f > myhiddenfile.list

$ cat myhiddenfile.list
./.help
./.notinmydictionary
./.yeah

Step 2: Run the following awk command

$ awk -F'/' 'NR==FNR{a[substr($NF,2)]=$0;next}{for(x in a){if(x == $1)system("rm \-i "a[x])}}' myhiddenfile.list  /usr/share/dict/words
remove ./.help? y
remove ./.yeah? y
$ ls -lart
total 8
drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
-rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .notinmydictionary
-rw-r--r--   1 jaypalsingh  staff   37 Jun 19 00:54 myhiddenfile.list
drwxr-xr-x   4 jaypalsingh  staff  136 Jun 19 01:07 .

Explanation of awk command:

# Set the field separator to "/"

awk -F'/' ' 

NR==FNR {   

# Slurp your hidden file list in to an array (index is filename;value is complete path)

    a[substr($NF,2)]=$0

# Keep doing this action until all files are stored in array

    next 
}
{

# Iterate over the array

    for(x in a) { 

# If the filename matches a word in your dictionary

        if(x == $1) { 

# execute the rm command to delete it

            system("rm \-i "a[x]) 
        }
    }
}' myhiddenfile.list  /usr/share/dict/words

Note: This will work for filename without extensions. For filenames with extensions like .help.txt will need additional steps to parse the file name for performing lookups against dictionary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top