whitneyrm
Posts: 3
Joined: Thu Feb 27, 2014 11:06 pm

Copying Files

Fri Feb 28, 2014 2:51 am

Using wheezy-raspbian I am trying to copy files from one folder to another.
i.e
list files:
pi@raspberrypi ~ $ sudo ls /var/lib/mysql
debian-5.5.flag ibdata1 ib_logfile0 ib_logfile1 mysql mysql_upgrade_info performance_schema

pi@raspberrypi ~ $ sudo ls /media/HDD
lost+found mysql

and copy with wildcard:
pi@raspberrypi ~ $ sudo cp -R /var/lib/mysql/* /media/HDD/mysql

unfortunately:
cp: cannot stat `/var/lib/mysql/*': No such file or directory

Does not wildcard '*' work like that?

W. H. Heydt
Posts: 12783
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Copying Files

Fri Feb 28, 2014 5:39 am

The wildcard should work as you've used it.

I would be inclined to use:

sudo cp -r /var/lib/mysql /media/HDD

That would let the system take care of everything.

When in doubt, try reading the manual entry ("man cp" in this case). I will admit to being surprised that 'R' is allowed as an alternate for 'r'. You may also wish to include the "-p" option, though.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: Copying Files

Fri Feb 28, 2014 12:10 pm

Wildcards are expanded by your shell before the command itself is started. Using "sudo" as the command does not give the shell permission to see inside /var/lib/mysql.

In this case you can easily avoid the wildcard, which is the best option. More generally you would need to start a temporary shell as root:

Code: Select all

$ sudo -s
# cp -R /var/lib/mysql/* /media/HDD/mysql
# exit
It is also possible to sudo a shell non-interactively, but this gets tricky if the command includes its own quotes or other special characters:

Code: Select all

sudo sh -c 'cp -R /var/lib/mysql/* /media/HDD/mysql'
(POSIX specified cp -R to avoid forcing a silent change on systems that might already implement cp -r with different rules. For maximum portability you should add -P, -H, or -L to indicate how you want symbolic links to be handled. But in GNU, cp -r and cp -R are both the same as cp -RP.)

Return to “Beginners”