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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-04-2017   -   Original Discussion by jcdole
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 8 November 2017, 11:46 AM EST
Location: 64500 - france ( south-ouest )
Posts: 171
Thanks: 58
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   -   Original Discussion by jcdole
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 2:20 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 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   -   Original Discussion by jcdole
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 November 2017, 11:18 PM EST
Location: NM
Posts: 11,239
Thanks: 571
Thanked 1,116 Times in 1,030 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   -   Original Discussion by jcdole
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 21 November 2017, 3:10 PM EST
Location: Simplicity
Posts: 3,826
Thanks: 319
Thanked 1,283 Times in 1,161 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   -   Original Discussion by jcdole
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 8 November 2017, 11:46 AM EST
Location: 64500 - france ( south-ouest )
Posts: 171
Thanks: 58
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   -   Original Discussion by jcdole
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 8 November 2017, 11:46 AM EST
Location: 64500 - france ( south-ouest )
Posts: 171
Thanks: 58
Thanked 3 Times in 3 Posts
And thank you also to Chubler_XL see : https://www.unix.com/shell-programmin...expansion.html
Sponsored Links
Closed

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 06:09 AM
Find command to search files in a directory excluding subdirectories jhilmil Shell Programming and Scripting 8 12-14-2012 09:23 AM
Excluding directories from a find nixie Shell Programming and Scripting 9 08-12-2012 03:03 PM
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 09:39 PM
Excluding directories with find x96riley3 UNIX for Dummies Questions & Answers 3 04-08-2009 05:12 AM



All times are GMT -4. The time now is 04:52 PM.