Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File move programatically in C

3 views
Skip to first unread message

Roy Strachan

unread,
Jun 15, 2009, 4:23:46 AM6/15/09
to
I'm trying to write a program to reorganize files on an Ubuntu Linux
8.04 box. I have it mostly working but I can't find a function to
move files. Since mv is a common function in both GUI and CLI there
is probably a function to do this. Does anyone know what that
function (or functions) is? It could probably be done with a call to
the system, but you don't learn anything that way. Any help or
pointers on where to look appreciated.

Roy
--
"The tragedy of life doesn't lie in not reaching your goal. The tragedy lies
in having no goal to reach." -- Benjamin Mays

Chris McDonald

unread,
Jun 15, 2009, 4:25:37 AM6/15/09
to
Roy Strachan <r...@bj.com> writes:

>I'm trying to write a program to reorganize files on an Ubuntu Linux
>8.04 box. I have it mostly working but I can't find a function to
>move files. Since mv is a common function in both GUI and CLI there
>is probably a function to do this. Does anyone know what that
>function (or functions) is? It could probably be done with a call to
>the system, but you don't learn anything that way. Any help or
>pointers on where to look appreciated.

rename(2) ?

--
Chris.

Rainer Weikusat

unread,
Jun 15, 2009, 6:13:13 AM6/15/09
to

It is somewhat more complicated than that: rename works only within a
particular filesystem/ block device. This means that the file has to
be copied and the original removed afterwards if rename fails with
EXDEV.

Lew Pitcher

unread,
Jun 15, 2009, 10:56:25 AM6/15/09
to
On June 15, 2009 04:23, in comp.os.linux.development.apps, Roy Strachan
(r...@bj.com) wrote:

> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files. Since mv is a common function in both GUI and CLI there
> is probably a function to do this. Does anyone know what that
> function (or functions) is? It could probably be done with a call to
> the system, but you don't learn anything that way. Any help or
> pointers on where to look appreciated.

High-level abstraction: rename(2)/renameat(2) ("man 2 rename")
Low-level abstraction(s): link(2)/linkat(2), symlink(2)/symlinkat(2),
unlink(2), open(2)/creat(2), read(2), write(2),
close(2)

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


Rainer Weikusat

unread,
Jun 15, 2009, 12:08:23 PM6/15/09
to
Lew Pitcher <lpit...@teksavvy.com> writes:
> On June 15, 2009 04:23, in comp.os.linux.development.apps, Roy Strachan
> (r...@bj.com) wrote:
>
>> I'm trying to write a program to reorganize files on an Ubuntu Linux
>> 8.04 box. I have it mostly working but I can't find a function to
>> move files. Since mv is a common function in both GUI and CLI there
>> is probably a function to do this. Does anyone know what that
>> function (or functions) is? It could probably be done with a call to
>> the system, but you don't learn anything that way. Any help or
>> pointers on where to look appreciated.
>
> High-level abstraction: rename(2)/renameat(2) ("man 2 rename")
> Low-level abstraction(s): link(2)/linkat(2), symlink(2)/symlinkat(2),
> unlink(2), open(2)/creat(2), read(2), write(2),
> close(2)

rename(2) is a system call which changes the name pointing to a
particular file with the limitation that it doesn't work accross file
system boundaries. It is not some type of 'abstraction' build onto
what you refer to as 'lower level' above.

Nate Eldredge

unread,
Jun 15, 2009, 12:08:27 PM6/15/09
to
Roy Strachan <r...@bj.com> writes:

> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files. Since mv is a common function in both GUI and CLI there
> is probably a function to do this. Does anyone know what that
> function (or functions) is? It could probably be done with a call to
> the system, but you don't learn anything that way. Any help or
> pointers on where to look appreciated.

rename(2) will work if the source and destination are on the same file
system. If they are not, you have to manually read the contents of the
source and write them to the destination, fix up permissions,
timestamps, etc, yourself (which can be tricky, especially if your
system has fancy features like ACLs), and finally delete (with
unlink(2)) the source file. If you are moving a directory, you have to
create the new one, recursively move its contents, and then remove the
old one. Non-regular files like FIFOs, devices, symlinks, etc, require
more manual labor.

mv(1) does all this. AFAIK there is no standard or commonly available C
function that does this, so if you want something that just works,
calling mv(1) via system(3) is probably the way to go.

On my system, this code is shared between mv and cp (mv calls cp in some
cases), and totals 1408 lines. So it's non-trivial.

Lew Pitcher

unread,
Jun 15, 2009, 1:11:00 PM6/15/09
to
On June 15, 2009 12:08, in comp.os.linux.development.apps, Rainer Weikusat
(rwei...@mssgmbh.com) wrote:

Given that Unixish systems /started with/ link(2) and unlink(2), and did not
have a rename(2), the rename(2) syscall abstracts the syscall pattern of
{if (link(oldpath, newpath)) unlink(oldpath);} into a single syscall. Thus,
to me, rename(2) is a higher-level abstraction than link(2) and unlink(2).


> It is not some type of 'abstraction' build onto what you refer to
> as 'lower level' above.

See my comment above.

Rainer Weikusat

unread,
Jun 15, 2009, 1:28:10 PM6/15/09
to
Lew Pitcher <lpit...@teksavvy.com> writes:
> On June 15, 2009 12:08, in comp.os.linux.development.apps, Rainer Weikusat
> (rwei...@mssgmbh.com) wrote:
>> Lew Pitcher <lpit...@teksavvy.com> writes:
>>> On June 15, 2009 04:23, in comp.os.linux.development.apps, Roy Strachan
>>> (r...@bj.com) wrote:
>>>
>>>> I'm trying to write a program to reorganize files on an Ubuntu Linux
>>>> 8.04 box. I have it mostly working but I can't find a function to
>>>> move files. Since mv is a common function in both GUI and CLI there
>>>> is probably a function to do this. Does anyone know what that
>>>> function (or functions) is? It could probably be done with a call to
>>>> the system, but you don't learn anything that way. Any help or
>>>> pointers on where to look appreciated.
>>>
>>> High-level abstraction: rename(2)/renameat(2) ("man 2 rename")
>>> Low-level abstraction(s): link(2)/linkat(2), symlink(2)/symlinkat(2),
>>> unlink(2), open(2)/creat(2), read(2), write(2),
>>> close(2)
>>
>> rename(2) is a system call which changes the name pointing to a
>> particular file with the limitation that it doesn't work accross file
>> system boundaries.
>
> Given that Unixish systems /started with/ link(2) and unlink(2), and did not
> have a rename(2), the rename(2) syscall abstracts the syscall pattern of
> {if (link(oldpath, newpath)) unlink(oldpath);} into a single
> syscall.

It doesn't. rename(2) is an atomic operation, a link/ unlink sequence
isn't. And we are not discussing 'functionality missing in ancient
versions of UNIX(*)' but 'Linux', which always had a rename system call.

> Thus, to me, rename(2) is a higher-level abstraction than link(2)
> and unlink(2).

Rename(2) operates at exactly the same level as link(2) and unlink(2)
do, and provides functionality (atomic rename) not available
otherwise. All three are independent operations and the 'usual'
definition of 'higher-level abstraction' is not 'somewhat similar but
newer'.

Roy Strachan

unread,
Jun 15, 2009, 2:50:15 PM6/15/09
to
On Mon, 15 Jun 2009 02:23:46 -0600, Roy Strachan <r...@bj.com> wrote:

>I'm trying to write a program to reorganize files on an Ubuntu Linux
>8.04 box. I have it mostly working but I can't find a function to
>move files. Since mv is a common function in both GUI and CLI there
>is probably a function to do this. Does anyone know what that
>function (or functions) is? It could probably be done with a call to
>the system, but you don't learn anything that way. Any help or
>pointers on where to look appreciated.
>
> Roy

Thanks to all. I actually found rename() this morning after a bit of
a sleep. Shows you should quit this stuff when you're tired. :o)
I normally find man files somewhat obtuse, but the explanation there
in this case made more sense to me than the glibc docs.

--
"Suicide is man's way of telling God, 'You can't fire me - I quit.'"
-- Bill Maher

HASM

unread,
Jun 19, 2009, 10:09:55 PM6/19/09
to
Roy Strachan <r...@bj.com> writes:

> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files.

Use Perl (or Python or ...) instead. It will pay off in the end, believe
me.

use File::Copy;
move ($file1, $file2);

Takes care of most problems discussed in this thread.

-- HASM

0 new messages