Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, The below script takes a date and table name from the user. It unzips and untar a file from archive folder based on date. Then select the required file ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16

    find and cp not working

    Hi,

    The below script takes a date and table name from the user. It unzips and untar a file from archive folder based on date. Then select the required file based on the tablename provided and copy it to ../posextract folder.The folder posextract contains already extracted files for the user.

    Code:
    echo "Enter date \n" 
    read lookdate 
    echo "Enter Table Name\n" 
    read tablename 
    cd $POS_ARC 
    find . -name *$lookdate* -exec cp {} ../postempextract \; 
    cd ../postempextract 
    gunzip * | tar -xvf - 
    find . -name *$tablename* -exec cp {} ../posextract \; 
    rm *
    1) The find command throws error with *. However when I try without *, it is not finding the required files.

    2)Can we modify the above script to avoid the use of ../postempextract here?

    Thanks
    Maya

  2. #2
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    When you use wildcards in an argument to a program, you must prevent those wildcards from being expanded by the shell. To do so, enclose them in either single quotes or double quotes. In your case, you need double quotes, to allow interpolation of the enclosed variable.
    Code:
    find . -name "*$lookdate*" -exec cp {} ../postempextract \;
    --- rod.
    Last edited by theNbomr; 08-06-2010 at 01:44 PM.
    Stuff happens. Then stays happened.

  3. #3
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    I just saw the second part of your question...

    To solve the problem of temporary files, instead use variables that contain filenames, and use grep to filter those...

    Code:
    for file in $(echo $(find . -name "*$lookdate*" ) | grep $tablename); do
       cp $file ../posextract
    done
    --- rod.
    Stuff happens. Then stays happened.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...