Find the answer to your Linux question:
Results 1 to 4 of 4
May i know how to check two arguments whether it is a file and the other is a directory, check if files exist in directory and copy it if it ...
  1. #1
    Banned
    Join Date
    Jun 2011
    Location
    Malaysia
    Posts
    5

    URGENT need help!~

    May i know how to check two arguments whether it is a file and the other is a directory, check if files exist in directory and copy it if it is a newer version. Seriously i need idea to do tis, jst some simple sample wil do

  2. #2
    Just Joined!
    Join Date
    Jun 2011
    Location
    US
    Posts
    2
    There may be a single command method to do this, but here's how I would do it:

    First use the find command:
    Code:
    find [directory to search through] -name [file searched for] -print
    This will list the pathname to the file if it exists in the directory or any sub-directories.
    Next use the cp command to copy the file to whatever directory you would like:

    Code:
    cp [found file] [new directory to place file]

  3. #3
    Banned
    Join Date
    Jun 2011
    Location
    Malaysia
    Posts
    5
    Code:
    hey..reli thx for ur hlp, nw i get some idea to do it, bt for the find and cp, i jst wn to use if-else to do it, im nt sure hw to do it,so here what i get:
    if [ find $2 -name $1 -print ]
    then 
    cp $1 $2
    echo"File copied"
    else
    echo"Error!"
    fi

  4. #4
    Banned
    Join Date
    Jun 2011
    Location
    Malaysia
    Posts
    5
    ops, sory for my mistake above, bt the code is wat i want to write, $1 wil be my file 1(f1) and $2 wil be directory 1(d1)

Posting Permissions

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