Programming simple file copy, move, and other file mngmnt routines

Programming simple file copy, move, and other file mngmnt routines

Post by Travis Jense » Tue, 04 Mar 1997 04:00:00



I am trying to find out if there is a fairly simple way to
do the impliment the following functions (anywhere you see
file below, realize that it could be a directory as well):

Delete: Delete a given file.
Copy:   Copy a source file or directory to a destination
        directory.
Move:   As above, only moving.  Could be implimented as
        a Copy() then Delete().
Chmod:  Change the attributes on a given file.
Touch:  Update a file's last access time, creating an empty
        file if necessary.
Check:  Find out if a file is a regular file, a directory,
        a symbolic link, etc.

Unfortunately, I don't have access to a UNIX programming book
and I can't find anything regarding this stuff on the
'net.  Regarding Copy(), I look at the GNU sources for 'cp'
and got lost really quickly.  I am hoping for something
closer to the Window's API call CopyFile() if it exists or
a 10-20 line set of code that impliments something like
CopyFile().

If worse comes to worse, I could do a system("cp ...")
call if I had to, but I would rather not create another
process if I can help it (not to mention our de*
chokes stepping across system() calls).

Pointers and help would be greatly appreciated.  If you
know of a book that can answer my questions, that would
be great, too.  WWW sources are always wonderful.

Thanks.

Travis
--
Travis Jensen

Viewpoint Datalabs International, Inc.            Phone: 801-229-3189
---------------------------------------------------------------------
If you eat a live frog in the morning, nothing worse will happen to
either of you for the rest of the day.

 
 
 

Programming simple file copy, move, and other file mngmnt routines

Post by Ronald Fische » Wed, 05 Mar 1997 04:00:00


| I am trying to find out if there is a fairly simple way to
| do the impliment the following functions (anywhere you see
| file below, realize that it could be a directory as well):
|
| Delete: Delete a given file.
| Copy:   Copy a source file or directory to a destination
|         directory.
| Move:   As above, only moving.  Could be implimented as
|         a Copy() then Delete().
| Chmod:  Change the attributes on a given file.
| Touch:  Update a file's last access time, creating an empty
|         file if necessary.
| Check:  Find out if a file is a regular file, a directory,
|         a symbolic link, etc.

From this context, I assume you are looking for a solution in C.

Copy: I don't see a better way than to use a combination of
      fopen + fread/fwrite + fclose
Move: rename("old","new");
Chmod: chmod("old",perm);
Touch: No idea - please let me know if you find out.
Check: See the man pages for opendir and lstat

--
Ronald Otto Valentin Fischer


http://ourworld.compuserve.com/homepages/ronald_fischer

 
 
 

Programming simple file copy, move, and other file mngmnt routines

Post by Stephen Bayn » Wed, 05 Mar 1997 04:00:00


: I am trying to find out if there is a fairly simple way to
: do the impliment the following functions (anywhere you see
: file below, realize that it could be a directory as well):

: Delete: Delete a given file.
: Copy:   Copy a source file or directory to a destination
:         directory.
: Move:   As above, only moving.  Could be implimented as
:         a Copy() then Delete().
: Chmod:  Change the attributes on a given file.
: Touch:  Update a file's last access time, creating an empty
:         file if necessary.
: Check:  Find out if a file is a regular file, a directory,
:         a symbolic link, etc.

: Unfortunately, I don't have access to a UNIX programming book
: and I can't find anything regarding this stuff on the
: 'net.  Regarding Copy(), I look at the GNU sources for 'cp'
: and got lost really quickly.  I am hoping for something
: closer to the Window's API call CopyFile() if it exists or
: a 10-20 line set of code that impliments something like
: CopyFile().

The best place to start with is in the man pages.
Use man -k topic if you are not sure of the command.
Look at
man remove
man rename
man 2 chmod            #don't forget the 2!
man fstat
man utime

This will cover almost everthing except the copy which you will have
to do yourself.

--

Philips Semiconductors Ltd                  
Southampton SO15 0DJ                        +44 (01703) 316431
United Kingdom                              My views are my own.
Do you use ISO8859-1? Yes if you see ? as copyright, as division and ? as 1/2.

 
 
 

Programming simple file copy, move, and other file mngmnt routines

Post by Andrew Giert » Wed, 05 Mar 1997 04:00:00


 Travis> I am trying to find out if there is a fairly simple way to
 Travis> do the impliment the following functions (anywhere you see
 Travis> file below, realize that it could be a directory as well):

 Travis> Delete: Delete a given file.

for files: use remove() or unlink().
for directories: on some (few) systems remove() works, otherwise rmdir().

 Travis> Copy:   Copy a source file or directory to a destination
 Travis>         directory.

Ah. There's no system call for that, you have to do your own
open/read/write/close loop.

If the source is a directory, you'll have to use opendir/readdir/closedir
(or ftw(), if you have it) to get all the filenames, and copy each file
separately.

 Travis> Move:   As above, only moving.  Could be implimented as
 Travis>         a Copy() then Delete().

Use rename(), and if it fails with EXDEV, fall back to copy/delete.
(rename() can't move files across file systems.)

 Travis> Chmod:  Change the attributes on a given file.

The chmod() system call, oddly enough.

 Travis> Touch:  Update a file's last access time, creating an empty
 Travis>         file if necessary.

The underlying system call here is utime() or utimes(), but it won't
create the file for you; if the file doesn't exist you'll have to create
it yourself.

If you own the file, you can set the access and modification times to
any arbitrary value. If you don't own the file, but have write permission
on it, you can set the access and modification times to 'now' (by passing
NULL to utime or utimes). If you only have read permission, you can only
set the access time to 'now' if the file has nonzero length, which you
do by reading the file, strangely enough.

 Travis> Check:  Find out if a file is a regular file, a directory,
 Travis>         a symbolic link, etc.

stat(), fstat() and lstat().

If you don't *need* to know if it's a symlink, use stat()/fstat() which
will follow a symlink and look at the pointed-to file or directory. If
you really need to treat symlinks specially, use lstat().

 Travis> Unfortunately, I don't have access to a UNIX programming book
 Travis> and I can't find anything regarding this stuff on the
 Travis> 'net.

You should get some mileage from looking up the above-mentioned system
calls in your system's manpages.

--
Andrew.

 
 
 

Programming simple file copy, move, and other file mngmnt routines

Post by Don Re » Thu, 06 Mar 1997 04:00:00


In the beginning, or at least on 3 Mar 1997 20:27:56 GMT,  Travis Jensen

Quote:

>I am trying to find out if there is a fairly simple way to
>do the impliment the following functions (anywhere you see
>file below, realize that it could be a directory as well):

man opendir ftw

Quote:

>Delete: Delete a given file.

unlink(name);

Quote:>Copy:   Copy a source file or directory to a destination
>        directory.

#define BUFSIZ 4096
char buff[BUFSIZ];
int cnt
if (NULL == (pFrom=fopen(fromname,"r")))
        return(-1):
if (NULL != (pTo=fopen(toname,"w"))) {
        do {
                cnt=fread(buff,1,BUFSIZ,pFrom);
                fwrite(buff,1,cnt,pTo);
        } while(cnt==BUFSIZ);
        fclose(pTo);fclose(pFrom);
        return(0)
Quote:}

fclose(pFrom);
return(-1):

Quote:>Move:   As above, only moving.  Could be implimented as
>        a Copy() then Delete().

if (0 != rename(oldname,newname)) {
        if (0== Copy(oldname,newname))
                unlink(oldname);

Quote:}
>Touch:  Update a file's last access time, creating an empty
>        file if necessary.
>Check:  Find out if a file is a regular file, a directory,
>        a symbolic link, etc.

man stat utime

--


 Calcasieu Lumber Co.                    Austin TX
  ---- "Ya jus' mash that butt'n, righ' jere"  ---

 
 
 

1. Simple shell program wanted to search and copy file(s)....

Hi,
    I want to write a shell program (sh or csh) which will
    do the following..

    1. I want to search for files ending with *001_.a
       and/or *002_.a in the directory which I specify
       in the command line. I want the shell program to
       search for these files in all the directories
       under this parent directory.

       files ending with 001_.a are called specs and
       ending with 002_.a are called body. so the command line
       will look like..

       my_script parent_directory target_directory specs

       The script will search for spec in the entire tree
       structure under the parent_directory (i.e. go into
       each subdirectory under parent directory, and if these
       subdirectory has some more directories it should search
       in them too..so on and so forth..) and copy those
       files to target_directory.

       I am sure it is a simple program, I need some hints...
       I have written very small shell programs but never
       of this type.

       Please respond by email.

thanks

--
B.G. Mahesh
SETT, Inc. (An Affiliate of EVB Software Engineering, Inc.)


2. RS/6000 and 3rd party SCSI devices

3. loop to copy a file from one server to others

4. comp.unix.sco.misc Administrative FAQ

5. What files to copy to move all printers to new system?

6. slackware cd-rom in Israel?

7. Copying and moving kernel file!

8. Gnome Zip drive support

9. When my HDD writes a big file (ftp/move/copy), Linux pauses...

10. Moving Files (not as simple as it sounds!)

11. efficient copy to user and copy from user routines in Linux

12. Copying files across file systems

13. How do I grep a file and move found files to a directory?