Recursively look for a directory for each file in an IBMi IFS directory

I am trying to write two scripts (edit: shell) and I have some difficulties. I will explain the purpose and then provide the script and the current output.

1: get a list of all the file names in the directory recursively. Then find the contents of all the files in this directory for each file name. Must return the path, file name and line number of each occurrence of a specific file name.

2: get a list of all the file names in the directory recursively. Then search for the contents of all files in the directory for each file name. Should return the path and file name for each file that is NOT found in any of the files in directories.

Ultimately, I want to use script 2 to find and delete (actually move them to another directory for archiving) unused files on the website. Then I would use script 1 to view each event and filter through any duplicate file names.

I know that I can make script 2 move every file since it works, and not as a second step, but I want to validate the script functions correctly before I do anything. I would change it after confirming that it is working correctly.

I am currently testing this on an IMBi system in strqsh.

Test folder structure:

scriptTest
---subDir1
------file4.txt
------file5.txt
------file6.txt
---subDir2
------file1.txt
------file7.txt
------file8.txt
------file9.txt
---file1.txt
---file2.txt
---file3.txt

I have text in some files containing existing file names.

This is my current script 1:

#!/bin/bash
files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d -exec basename {} \;`
for i in $files
do
    grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt;
done

, , . grep ?

script 2:

#!/bin/bash
files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d`
for i in $files
do
    #split $i on '/' and store into an array
    IFS='/' read -a array <<< "$i"

    #get last element of the array 
    echo "${array[-1]}"

    #perform a grep similar to script 2 and store it into a variable
    filename="grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt;"

    #Check if the variable has anything in it
    if [ $filename = "" ]   
            #if not then output $i for the full path of the current needle.
        then echo $i;
    fi
done

, $i . 6

001-0059 Syntax error on line 6: token redirection not expected.

linux, , .

.

+4
1

, 100%, , . , , , , .

cd /tmp
mkdir -p scriptTest/subDir{1,2}
mkdir -p scriptTest/subDir1/file{4,5,6}.txt
mkdir -p scriptTest/subDir2/file{1,8,8}.txt
touch scriptTest/file{1,2,3}.txt

find -exec Bash grep . , , , , fdupes duff (, , ) .

, , .txt , duff fdupes

Duff , . , duff -e0 * | xargs -0 rm . , :

$ duff -r scriptTest/
8 files in cluster 1 (0 bytes, digest da39a3ee5e6b4b0d3255bfef95601890afd80709)
scriptTest/file1.txt
scriptTest/file2.txt
scriptTest/file3.txt
scriptTest/subDir1/file4.txt
scriptTest/subDir1/file5.txt
scriptTest/subDir1/file6.txt
scriptTest/subDir2/file1.txt
scriptTest/subDir2/file8.txt

fdupes

. fdupes . --delete --noprompt, , . , :

$ fdupes -R scriptTest/
scriptTest/subDir1/file4.txt            
scriptTest/subDir1/file5.txt
scriptTest/subDir1/file6.txt
scriptTest/subDir2/file1.txt
scriptTest/subDir2/file8.txt
scriptTest/file1.txt
scriptTest/file2.txt
scriptTest/file3.txt

,

$ find scriptTest -name \*.txt
scriptTest/file1.txt
scriptTest/file2.txt
scriptTest/file3.txt
scriptTest/subDir1/file4.txt
scriptTest/subDir1/file5.txt
scriptTest/subDir1/file6.txt
scriptTest/subDir2/file1.txt
scriptTest/subDir2/file8.txt

find -exec {} + grep, --recursive --files-with-matches .

Bash

, , , Bash , Bash for-loop, :

files=$(find scriptTest -name \*.txt)
for file in "${files[@]}"; do
  : # do something with each "$file"
done

, , , , , - . YMMV.

+1

Source: https://habr.com/ru/post/1570519/


All Articles