Title: Shell Script is copying every file from parent directory
Tags: linux;bash;shell;unix;aix
Question: I'm trying to copy $CurrentFile from $CurrentDir to $CurrentBackup. The problem that I'm having is that this file copies every $CurrentFile from parent directory.
So for example,
let say input is this
CurrentDir=/home/usr/logs
CurrentFile='SystemOut_*'
CurrentDay=6
CurrentBackup=/backup
But, this command will copy every $CurrentFile from /home/usr/Serverinf to $CurrentBackup...
find . $CurrentDir . -type f -name $CurrentFile -mtime +$CurrentDay -exec cp {} $CurrentBackup \;
I'm not sure what I did wrong...Can anyone tell me what I did wrong?
If I can't use this command, could you tell me alternative command for this?
Thanks for your help!
Comment: Oh ops I meant copying not moving...but it still copies everything...

Comment: so like find . "$CurrentDir" . -type f -name $CurrentFile -mtime +$CurrentDay -exec cp {} $CurrentBackup \;

Comment: $CurrentFile is being read. ONLY moves $CurrentFiles but it also copies $CurrentFile from different directory

Comment: You're saying it is moving, but you are using cp.  Are you sure it is moving and not copying?

Comment: Why is there a "." before and after $CurrentDir?

Comment: I would expect "find $CurrentDir -type f ..." not "find . $CurrentDir . -type f ..."

Comment: Just to check it on your own, have you tried using the actual fine name after -name? If that works, then you at least know that the problem is with how the value of $CurrentFile is being read. This is not an answer but something you can try and then post the findings.

Here is the accepted answer: As far as I can tell the problem is the extra dots:
find . $CurrentDir . -type f -name $CurrentFile -mtime +$CurrentDay -exec cp {} $CurrentBackup \;
-             -
You're telling find to search in the current directory and $CurrentDir. Get rid of the dots and also use double quotes to prevent the shell from expanding names before find ever sees them.
find "$CurrentDir" -type f -name "$CurrentFile" -mtime "+$CurrentDay" -exec cp {} "$CurrentBackup" \;
Comment for this answer: @user1516649 that is telling you the name `$CurrentDir` is unset (expanding to the empty string). Check your spelling and capitalization to make sure it is the name you want. (Also note the same problem with `$CurrentBackup`.)
Comment for this answer: @user1516649 are you using `let`? That's not what you show above.
Comment for this answer: @user1516649 `let` in bash is for arithmetic expressions. What you have there does not look like you need `let`.
Comment for this answer: this is what i get if i debug if i run this code                          ++ find '' -type f -name 'SystemOut_*' -mtime +6 -exec cp '{}' '' ';'
find: cannot search `': No such file or directory
++ find '' -name 'SystemOut_*' -mtime +6 -delete
find: cannot search `': No such file or directory
Comment for this answer: Darn it yes you are right....but now i get this error(just made typo when i was rewriting)                                         run.sh: line 57: let: CurrentBackup=/home/usr/log: syntax error: operand e        xpected (error token is "/home/usr/log").. same thing for CurrentDir
Comment for this answer: yes... is this causing the problem? let CurrentDir=$(eval "echo \$DIR$i$l") sorry i didn't know this will cause prob :(

Here is another answer: Yup, your problem boils down to doublequoting, to prevent bash from doing filename expansion, as noted by Vaughn Cato.  See the difference in output when you do the following two commands:
echo $CurrentFile
echo "$CurrentFile"
Generally, it's a good idea to double- quote shell variables.
Comment for this answer: I can't downvote this because doublequoting is *part* of the problem, but it is not the whole problem, so I can't upvote it either.

Here is another answer: There are extra dots before and after your $CurrentDir.  This will make it look for the files in the actual current directory in addition to $CurrentDir.
Also, using double quotes around any variables that may contain wildcards is important.
find $CurrentDir -type f -name "$CurrentFile" -mtime +$CurrentDay -exec cp {} $CurrentBackup \;
Even better, put double quotes around all variables unless you explicitly need to have it expand to multiple parameters:
find "$CurrentDir" -type f -name "$CurrentFile" -mtime +"$CurrentDay" -exec cp {} "$CurrentBackup" \;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:10/03/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<linux><bash><shell><unix><aix>', 'Title': 'Shell Script is copying every file from parent directory', 'ViewCount': 451, 'id': 12709510, 'user_AboutMe': None, 'user_AccountId': 1644957.0, 'user_DisplayName': 'user1516649', 'user_Reputation': 155.0, 'user_Views': 22.0, 'user_id': 1516649.0}}