Find command excluding directories and some files

hello.
I try to print a list of files but excluding some directories and some files.
I would like to write a command for :
find "from_dir" "ignore dir1, dir2, ..." "ignore file1, file2,...." "where file are older than 2017-02-03T06:00:00"

Note that "DO_IT" is a local function in the script and is exported like this

function DO_IT () {
    MY_PATH1="$1"
    if [[  ! -d "$MY_PATH1" ]] ; then
        echo "doing : $MY_PATH1"
    fi
}
export -f DO_IT

The first part run with success :

find /home/tux_user -type d \( -path /home/tux_user/.mozilla -o -path /home/tux_user/.cache -o -path  /home/tux_user/.local/share/RecentDocuments  -o -path  /home/tux_user/.local/share/kscreen  \) -prune -o -newermt '2017-02-03T06:00:00' -exec bash -c 'DO_IT "$0" '  {} \;

Now I want to exclude some files.
So I have add this part of code inside the find command

-o \( ! -name ".direct*" ! -name ".Xauthor*"  ! -name ".xsession-erro*"  ! -name "*.xsession-errors-:*"  ! -name ".bash_histor*"   \)

The command is now :

find /home/tux_user -type d \( -path /home/tux_user/.mozilla -o -path /home/tux_user/.cache -o -path  /home/tux_user/.local/share/RecentDocuments  -o -path  /home/tux_user/.local/share/kscreen  \) -prune -o \( ! -name ".direct*" ! -name ".Xauthor*"  ! -name ".xsession-erro*"  ! -name "*.xsession-errors-:*"  ! -name ".bash_histor*"   \) -o  -newermt '2017-02-03T06:00:00' -exec bash -c 'CHERCHE "$0" '  {} \;

But the specific files are still there (".direct*" ".Xauthor*" ".xsession-erro*" "*.xsession-errors-:" ".bash_histor").

Any help is welcome

Not sure I can follow the entire logics, but

  • what does CHERCHE "$0" do to the files in question?
  • find executes actions if the test's result is TRUE. -o will add TRUE results, not remove any from before. So, to exclude some files, this might not be the desired operator?

What the script does is hard to see. But.
Consider that sometimes using a tool for a complex task sometimes requires other tools.

As a template:

find path1 path2 path3 | grep -v '^\.*'

Which removes so-called hidden files - ones starting the dot character. They are hidden because the default for ls is not to display them. ls -a will display them.

If you have a "crazy" list of filenames to ignore - one that has too many names - use a pattern file with grep.

find path1 path2 path3 | grep -f pattern_file -v

Where pattern_file looks like:

.direct*
.X*
.x*

You want to continue if not certain names, so the last -o should be a -a or omitted.
The following skips all dot-dirs and dot-files

find /home/tux_user -type d -name ".?*" -prune -o -type f ! -name ".*" -print

The problem :

Here a solution of my problem

find "$MY_PATH" -type d \( "${FA[@]}" \) -prune -o \( -newer $MY_PATH/FROM_DATE_FILE.txt \! -newer $MY_PATH/TO_DATE_FILE2.txt   \)  -o -type f \( "${FB[@]}" -exec bash -c 'DO_IT $0 ' {} \; \) | sort

where

"${FA[@]}" contains a list of directories to exclude
FA=( -path  $MY_PATH/.cache  -o -path  $MY_PATH/.dbus ....  -o  -path  $MY_PATH/.config/kdeconnect )

where

"${FB[@]}" contains a list of files to exclude
FB=( ! -name  ".direct*" ! -name ".Xauthor*" ! -name ".xsession*" ....   ! -name "drkonqirc"  ! -name "granatierrc"  )

date are set using the command touch like

touch -d �2017-04-13 16:21:42�  $MY_PATH/FROM_DATE_FILE.txt 
touch -d �2017-04-14 00:00:00�  $MY_PATH/TO_DATE_FILE.txt 
1 Like

And thank you also to Chubler_XL see : Bash expansion