Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Find command excluding directories and some files

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-04-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 July 2017, 10:04 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 162
Thanks: 55
Thanked 3 Times in 3 Posts
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

Code:
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 :

Code:
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

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

The command is now :

Code:
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
Sponsored Links
    #2  
Old Unix and Linux 02-05-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 July 2017, 4:41 PM EDT
Location: Aachen, Germany
Posts: 11,067
Thanks: 284
Thanked 3,411 Times in 3,141 Posts
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?
Sponsored Links
    #3  
Old Unix and Linux 02-05-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 July 2017, 8:26 AM EDT
Location: NM
Posts: 11,123
Thanks: 549
Thanked 1,078 Times in 1,000 Posts
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:


Code:
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.


Code:
find path1 path2 path3 | grep -f pattern_file -v

Where pattern_file looks like:

Code:
.direct*
.X*
.x*

    #4  
Old Unix and Linux 02-05-2017
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 27 July 2017, 2:26 PM EDT
Location: Simplicity
Posts: 3,642
Thanks: 285
Thanked 1,217 Times in 1,100 Posts
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

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

Sponsored Links
    #5  
Old Unix and Linux 04-14-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 July 2017, 10:04 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 162
Thanks: 55
Thanked 3 Times in 3 Posts
The problem :
Quote:
Find from path any files
Newer than some date
Excluding files from some folders
Excluding some kind of files
Then sort
Here a solution of my problem

Code:
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
Code:
"${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
Code:
"${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

Code:
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

The Following User Says Thank You to jcdole For This Useful Post:
MadeInGermany (04-20-2017)
Sponsored Links
    #6  
Old Unix and Linux 05-14-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 July 2017, 10:04 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 162
Thanks: 55
Thanked 3 Times in 3 Posts
And thank you also to Chubler_XL see : Bash expansion
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find all files in the current directory excluding hidden files and directories ksailesh1 UNIX for Advanced & Expert Users 7 02-03-2014 05:09 AM
Find command to search files in a directory excluding subdirectories jhilmil Shell Programming and Scripting 8 12-14-2012 08:23 AM
Excluding directories from a find nixie Shell Programming and Scripting 9 08-12-2012 02:03 PM
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 08:39 PM
Excluding directories with find x96riley3 UNIX for Dummies Questions & Answers 3 04-08-2009 04:12 AM



All times are GMT -4. The time now is 08:53 PM.