AIX : Find files ignoring certain file extensions


 
Thread Tools Search this Thread
Operating Systems AIX AIX : Find files ignoring certain file extensions
# 1  
Old 06-22-2015
Code AIX : Find files ignoring certain file extensions

Hi All,

I am scripting a program to find and archive files. There are certain file types that I do not want to archive. Below is the scenario.

I have created a lookup file which has details on folders days and file extensions that needs to be ignored
Quote:
/Folder1,30,.txt;.csv
/Folder2,60,.sh,.ksh,.txt
/folder3,90,.param
I have separated the individual into fields and I am having trouble while excluding the file extensions in the find command. I tried to methods to ignore files.

Method 1: This method there are no errors, but the file extensions are still appearing.
Code:
IGNOREFILES="-o name *.txt* -o name *.csv*"
find $DIRNAME/* -prune -type f ! \( -name $IGNOREFILES \) -mtime +$TIME -exec ls -ltr {} \;

Method 2: This method I am using "grep -v" to ignore. It works when executed manually. But in the script is it throwing the following error.

Code:
IGNOREFILES="grep -v .txt | grep -v .csv"
find $DIRNAME/* -prune -type f ! \( -name $IGNOREFILES \) -mtime +$TIME -exec ls -ltr {} \;

Quote:
# echo $IGNORELIST
grep -v .xml | grep -v .sql | grep -v .dat | grep -v .csv
# find $SDIRNAME/* -prune -type f -mtime +$MTIME -exec ls -ltr {} \; | $IGNORELIST
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .sql.
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .dat.
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .csv.
Thanks,
Kev
# 2  
Old 06-23-2015
Hello Kev,

Following may help you in same, I haven't tested it but you can take it as a startup and let us know if you have any queries.
Code:
awk -F, '{if(NR>1){printf "\n"};A="find " $1 "/*" " -prune -type f \\( "; {for(i=3;i<=NF;i++){O="! -name *" $i;if(i!=NF){O=O " -o"};P=P?P OFS O:O;print A OFS P;A=P="";};print " \\) -mtime +" $2 " -exec ls -ltr {} \\;"}}' ORS=" " Input_file

Output will be as follows.
Code:
find /Folder1/* -prune -type f \(  ! -name *.txt -o  ! -name *.csv  \) -mtime +30 -exec ls -ltr {} \;
find /Folder2/* -prune -type f \(  ! -name *.sh -o  ! -name *.ksh -o  ! -name *.txt  \) -mtime +60 -exec ls -ltr {} \;
find /folder3/* -prune -type f \(  ! -name *.param   \) -mtime +90 -exec ls -ltr {} \;

If happy with results then you can pass these output to sh as follows. But it is advisable to test it first and if it prints correct expected output then do pass output to sh.


Thanks,
R. Singh

Last edited by RavinderSingh13; 06-23-2015 at 02:29 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-23-2015
I haven't tested it either, but the basic principles used by RavinderSingh13 look sound. But the logic is slightly off. If you are trying to exclude files with names ending in .txt and exclude files with names ending in .csv you want an AND of the ! -name clauses; not an OR. And, with all ANDs, the parentheses aren't needed. And, the -name primaries require a single argument, so an argument containing an asterisk has to be quoted. And, it will be much more efficient to pass ls multiple operands rather than invoking ls for each selected file individually. And, finally, note that the sample lookup file uses both commas and a semicolon as field separators (although the semicolon on the 1st line might be a typo). I think the commands you need are more like:
Code:
find "/Folder1"/* -prune -type f ! -name "*.txt" ! -name "*.csv" -mtime +30 -exec ls -ltr {} +
find "/Folder2"/* -prune -type f ! -name "*.sh" ! -name "*.ksh" ! -name "*.txt" -mtime +60 -exec ls -ltr {} +
find "/folder3"/* -prune -type f ! -name "*.param" -mtime +90 -exec ls -ltr {} +

So, try:
Code:
awk -F'[,;]' '
{	printf("find \"%s\"/* -prune -type f ", $1)
	for(i = 3; i <= NF; i++)
		printf("! -name \"*%s\" ", $i)
	printf("-mtime +%s -exec ls -ltr {} +\n", $2}
}' Input_file

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-23-2015
Quote:
Originally Posted by kavinmjr
Method 2: This method I am using "grep -v" to ignore. It works when executed manually. But in the script is it throwing the following error.

Code:
IGNOREFILES="grep -v .txt | grep -v .csv"
find $DIRNAME/* -prune -type f ! \( -name $IGNOREFILES \) -mtime +$TIME -exec ls -ltr {} \;

# find $SDIRNAME/* -prune -type f -mtime +$MTIME -exec ls -ltr {} \; | $IGNORELIST
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .sql.
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .dat.
grep: 0652-033 Cannot open |.
grep: 0652-033 Cannot open grep.
grep: 0652-033 Cannot open -v.
grep: 0652-033 Cannot open .csv.

You already get very sound advice but you might probably want to know why this doesn't work in the form you wrote it:

The shell evaluates a commandline in certain steps. All the evaluation in a certain step is done at once. For instance:

Code:
A="foo"
B="bar"

# command1 $A | command2 $B

"$A" and "$B" are expanded to their respective values at the same time. This is why the following:

Code:
A='$B'
B="bar"

# command1 $A

will provide command1 not with "bar" but with a literal "$B". When the step "evaluate variables" is done and "$A" is replaced by its value "$B" there will be no further evaluation of "$B" to "bar".

Furthermore, when the shell evaluates a variable it surrounds it by so-called "IFS" (internal field separator) characters. This IFS character is per default a blank but for the next evaluation of a command the shell is aware that there is a difference between a simple blank inside a variables value and an IFS character separating two arguments. here is another example for exactly the same problem, this time created by the guys who develop the operating system.

If you need a solution to such problems (you won't need it here, because you got better procedures already) there is: the eval command. I said that the shell is aware of the difference between a simple space and the IFS character, but only so for ONE evaluation. Fortunately you can (re-)start the command evaluation with a certain keyword, which is "eval". Just prepend your command with it and the shell will evaluate your command line, then evaluate the result again, and only then execute it. This way you could do:


Code:
A='$B'
B="bar"

# eval command1 $A

and have "bar" as argument to command1. In the first evaluation "$A" will be replaced by "$B" and in the second pass "$B" will be replaced by "bar". This will also work for your construct:

Code:
IGNOREFILES="grep -v .txt | grep -v .csv"
# eval find $SDIRNAME/* -prune -type f -mtime +$MTIME -exec ls -ltr {} \; | $IGNOREFILES

I hope this helps.

bakunin
# 5  
Old 06-24-2015
Method 1 as a shell script. Does it print the desired files?
Code:
#!/bin/sh
IFS=" ,;"
while read dir days exts
do
  omit=""
  set -- $exts
  for ext do
    omit="$omit ! -name *$ext"
  done
  find $dir/* -prune -type f -mtime +$days $omit -print
done < lookupfile

Don, you had ( } in
Code:
  printf("-mtime +%s -exec ls -ltr {} +\n", $2)


Last edited by MadeInGermany; 06-24-2015 at 03:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find wild card directory and its files of some extensions

I want to use Find command to find directories that have certain name and them find files in that directory having only some extensions. So far, I have come up with this command to list directories with wild card name and list ALL the files in that directory. find . -type d -name prog\* -print... (11 Replies)
Discussion started by: sssccc
11 Replies

2. Shell Programming and Scripting

Add file extensions to files EXCEPT the script that is issuing

Greetings all, On a RedHat System - I am issuing a command from script.sh that will add a file extension to a listing of files in a directory. It works, but I need to script from having an extension added as well. Here is what I have tried to no luck: for file in `ls * | awk ' /\./{print... (6 Replies)
Discussion started by: jeffs42885
6 Replies

3. AIX

Loading AIX kernel extensions on reboot

Greetings, Does anyone know how to load AIX kernel extensions on reboot? I know that Oracle loads it's postwait kernel extension via a executable in /etc/inittab. I'm assuming this executable calls the "sysconfig" system call and loads it. What if I wrote my own? What is the proper way in AIX to... (3 Replies)
Discussion started by: jbleistein
3 Replies

4. Shell Programming and Scripting

Find duplicate files but with different extensions

Hi ! I wonder if anyone can help on this : I have a directory: /xyz that has the following files: chsLog.107.20130603.gz chsLog.115.20130603 chsLog.111.20130603.gz chsLog.107.20130603 chsLog.115.20130603.gz As you ca see there are two files that are the same but only with a minor... (10 Replies)
Discussion started by: fretagi
10 Replies

5. Shell Programming and Scripting

adding file extensions to split output files

Hello, I've searched this forum and others for a solution to my problem but nothing seems just right, I'm hoping I can get some help (seems like this should be easy, and I apologize if I've missed something on the forum): I have several large .fastq DNA sequence files (~20million reads,... (2 Replies)
Discussion started by: ljk
2 Replies

6. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

7. Shell Programming and Scripting

Ignoring certain extensions

Dear Friends, I want to move all the files to temp folder except files having following extensions which are case sensitive. .ttM .Hmt .dMt Request you to guide me to do the same Thank you in advance Anushree (3 Replies)
Discussion started by: anushree.a
3 Replies

8. UNIX for Dummies Questions & Answers

Find all the unique file extensions

Hi How can i find the unique list of file extensions in a folder/subfolders e.g. MAIN/ a.txt b.txt a.clas a.java b.class a.txt.112 c.12.ram.jar i just need to get the below out irrespective of file being present in folder or subfolders txt clas java (5 Replies)
Discussion started by: reldb
5 Replies

9. Shell Programming and Scripting

Find files ignoring subdirectories

Hi guys, I am searching some files not equal to the pattern with this command find ! -name "PATTERN" -type f but my problem is the find command because he also search inside subdirectories and that's the thing i don't want that. Is there any comand to ignore the directories... (4 Replies)
Discussion started by: osramos
4 Replies

10. Shell Programming and Scripting

Find files with 3 different extensions

Hi all, From one directory I need to fetch only files of type *.xls,*.csv,*.txt. I tried the find . -name '*.txt,*.csv,*.xls' -print. But it throws me error. Please do help me on this. Thanks Mahalakshmi.A (11 Replies)
Discussion started by: mahalakshmi
11 Replies
Login or Register to Ask a Question