Issue when I try passing parameters to find command?

10,758

You have quoting problems.

Tip: stick an echo in front of the command line to see what it's actually expanding to. Even more explicit, for showing exactly where each argument is separated, stick python -c "import sys; print sys.argv[1:]" in front of the command line.

python -c "import sys; print sys.argv[1:]" \
    find $FILE_DIR -name "*.*"  "${exc_lst}" -type f -mtime +20 -user sh79790 -ls

outputs:

['find', '-name', '*.*', '! -path  "/var/app/s2/pnl/incoming/recondata/*.*"', '-type', 'f', '-mtime', '+20', '-user', 'sh79790', '-ls']

As you can see, ! -path "/var/app/s2/pnl/incoming/recondata/*.*" is provided as a single big argument with spaces and quotes inside it. That's what you ask for when you quote ${exc_lst}": don't expand. find does not recognize this. It needs !, -path, and the path all as separate arguments.

Now:

echo find $FILE_DIR -name "*.*"  ${exc_lst} -type f -mtime +20 -user sh79790 -ls

outputs:

find -name *.* ! -path "/var/app/s2/pnl/incoming/recondata/*.*" -type f -mtime +20 -user sh79790 -ls

As you can see, there are literal double quote characters around the pathname. It's going to exclude a path that literally contains those quotes, which won't occur.

Try defining exc_lst without those quotes:

export exc_lst='! -path  /var/app/s2/pnl/incoming/recondata/*.*'

and then using your second form:

find $FILE_DIR -name "*.*"  ${exc_lst} -type f -mtime +20 -user sh79790 -ls

Luckily, the path to exclude does not contain any spaces. If it did, you would have a much harder time of accomplishing this.

Note: All my sample output is missing the first argument to find because $FILE_DIR is not defined in my shell (you haven't specified its value) but if it were defined it would be there.

Share:
10,758

Related videos on Youtube

suraj hebbar
Author by

suraj hebbar

Updated on September 18, 2022

Comments

  • suraj hebbar
    suraj hebbar over 1 year

    Parameter:

    export exc_lst='! -path  "/var/app/s2/pnl/incoming/recondata/*.*"';
    

    When I try using the below find commands:

    find $FILE_DIR -name "*.*"  "${exc_lst}" -type f -mtime +20 -user sh79790 -ls
    

    it throws an error :Missing conjunction

    find $FILE_DIR -name "*.*"  ${exc_lst} -type f -mtime +20 -user sh79790 -ls
    

    it doesn't exclude the mentioned path.

    When I pass the value directly, it works fine i.e.

    find $FILE_DIR -name "*.*" ! -path  "/var/app/s2/pnl/incoming/recondata/*.*" -type f -mtime +20 -user sh79790 -ls
    

    I need to resolve the variable which will have all the files to exclude from find command.

  • suraj hebbar
    suraj hebbar over 8 years
    Thanks, but when i try to export the parameter without the single quotes and use it in the find command ,the * is expanded to actual name and find command fails: echo find /var/app/s2/pnl/incoming/ -name . ! -path /var/app/s2/pnl/incoming/recondata/OASYSFX_FOR_GENESISFX_ori‌​g.20150731 /var/app/s2/pnl/incoming/recondata/OASYS_FOR_GENESIS_orig.20‌​150731.gz /var/app/s2/pnl/incoming/recondata/PASS2_POSTPROC.......
  • Celada
    Celada over 8 years
    @surajhebbar indeed — that's why you need the single quotes or backslashes around the asterisks or something
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    Relying on the unquoted $exc_lst won't work if /var/app/s2/pnl/incoming/recondata/*.* matches a file (which is likely if the exclusion is useful).