String variable as part of expression in find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String variable as part of expression in find command
# 1  
Old 06-03-2015
Linux String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type.

this works fine:
Code:
Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname '*.dat' \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
echo "other file searched == $Find_File2Exclude"

but the excluded file to search [eg. *.out, *.dat] change from time to time so I have to change it into a variable. However, if I change my code into this..
Code:
exclude=" ! -iname '*.out' ! -iname '*.log' ! -iname '*.cps' ! -iname '*.auc' ! -iname '*.dat'"
Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( $exclude \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
echo "$Find_File2Exclude"

it will give me this error:
Quote:
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
I realize that the "! -iname" expression shouldn't be inside the string variable $exclude, but I need to concatenate other file to be exclude in the future, how can I make it work? Or is it still possible?

Thanks in advance.

Last edited by rbatte1; 06-03-2015 at 05:26 AM.. Reason: Changed ICODE tags to be CODE tags
# 2  
Old 06-03-2015
Where is the ${paths[]} array being set? Where is $k being set? The error you're getting sounds like ${paths[$k]} could be expanding to an empty string.
# 3  
Old 06-03-2015
Hi there Don Cragun, the
Code:
${path[$k]}

is working fine, I have two directories to look up that's why it is in array form and it is from a config file that I have set. I can echo the path and also like I said earlier the first code is working while in the second code which I change the content of
Code:
-type f \( ... \)

with a variable, the error occurs even if you change the
Code:
${path[$k]}

with an actual path such as
Code:
/home/user/test

# 4  
Old 06-03-2015
Try to reduce it. What would find . \( $exclude \) yield?
# 5  
Old 06-06-2015
Executing commands containing quoted strings produced by command substitution can be tricky. You can easily see the problem if you use set commands to turn tracing on and off around the command substitution in your code:
Code:
set -xv
Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( $exclude \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
set +xv

which clearly shows that you end up with double-quoted single-quoted strings for your patterns containing wildcard such as "'*.log'" which causes find to look for filenames containing literal single-quote characters.

If all of the following are true:
  1. All of the elements of the array ${paths[]} are absolute pathnames.
  2. None of the elements of the array ${paths[]} contain any whitespace characters.
  3. You don't mind the script creating an empty directory in your home directory (which the script will use to run the concocted find command pipeline with no chance that unquoted filename patterns containing wildcards with expand to actual filenames).
then the following replacement for your non-working code snippet may work:
Code:
[ -d "$HOME/_empty_dir" ] || mkdir "$HOME/_empty_dir" || exit 1
cd "$HOME/_empty_dir"
exclude="out log cps suc dat"
cmd="find ${paths[$k]} -maxdepth 1 -type f $(printf '! -iname *.%s ' $exclude) -printf"
Find_File2Exclude=$($cmd '%t %p\n' | sort -k1,1n | head -n 1 | cut -d'/' -f3)
cd - > /dev/null
rmdir "$HOME/_empty_dir"
printf '%s\n' "$Find_File2Exclude"

Note that I could not test this exact code on my system because the find utility on my system does not recognize the -maxdepth, -iname, and -printf primaries. But, similar constructs have been successfully tested with both bash and ksh.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

2. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

3. UNIX for Dummies Questions & Answers

find command: names matching the expression

Hello all, I need to print directories using find command. The directories names contain date in the format YYYYMMDD or the name of directory is only the date format. I want print directories, which doesn't start with this date. E.g I have dirs like foo20120101 foo20120101foo 20120101foo... (1 Reply)
Discussion started by: satin1321
1 Replies

4. Shell Programming and Scripting

Regular Expression in Find command [KSH]

Hello, I am trying to use regex wtih find command in KSH. For some reason it is not working as expected. Input: comm_000_abc_0102.c comm_000_abc.c 456_000_abc_1212.cpp 456_000_abc_.cpp Expected Output: comm_000_abc_0102.c kkm_000_abc_8888.cpp (Basically I want to find all... (6 Replies)
Discussion started by: vinay4889
6 Replies

5. Shell Programming and Scripting

Find, regular expression, anyway to simplify this find command?

Hello everyone, first post here, trying to learn scripting on my own and this forum as been really helpful so far. I made few little scripts working great but I m facing some problems with RE. I have a bunch of files in many subdirectories called *001.ext *002.ext OR simple *.ext or *01.ext... (7 Replies)
Discussion started by: Sekullos
7 Replies

6. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

7. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

8. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

9. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies

10. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies
Login or Register to Ask a Question