Connect to shared folders from the commandline

If you work in an office environment or have more than one PC in your home you might be used to connecting to shared folders across the network. Ubuntu's Places ^ Network view should show the computers that are local to you and let you connect.

If you're working at the command-line and want to access shared folders then it's a little trickier. Once a shared folder has been accessed by Nautilus you'll find it mounted in the hidden .gvfs folder of your /home folder. But if the desktop isn't up and running, or if the shared folder isn't mounted, then it won't be accessible.

In such a case, you might want to use smbclient, which effectively lets you "ftp" into a shared folder, and use almost exactly the same commands to down/upload files as the command-line ftp program (see Tip 131, on page 173 for details of how the ftp command-line program works).

1. Start by using smbclient with the -L option to list the shared resources on the computer you want to connect to. You can either specify the computer's network name or the IP address. You can find out the computer name on a Windows XP computer by right-clicking My Computer, selecting Properties, and then looking under the Computer Name tab for the Full computer name entry. You can also click

Connect to shared folders from the command-line M 146

the Change button to assign a new name if the existing one is too complex. To find out the IP address of a Windows computer, click Start ^ Run and type cmd. In the DOS box that appears, type ipconfig and, in the output, look for the line that reads IP addresss.

2. Here's how to list what's available on a computer with the network name keir-windows:

$ smbclient -L keir-windows

You might be prompted for a password. It was enough in my tests just to hit Enter at this stage. Then look in the output for listings under the Sharename heading. Those with disk in the Type heading alongside equate to the shared folders available on the computer. You must specify a particular shared folder when you want to connect—you can't just connect to a computer and then switch to whichever folder you want to access.

3. Connecting to the shared folders is a little strange because the network name needs to be specified in an unusual way. Just like Windows, smbclient uses backslashes (\) for addresses (rather than forward slashes, as is typical with Linux/Unix), but these have a quite distinct meaning at the Linux command-prompt and this causes problems. Backslashes are used to tell the shell not to interpret the next character you type in the way it normally does. See the sidebar on page 37 for more information.

Perhaps ironically, we therefore have to use another backslash to tell the command-line not to interpret the backslash in the way it normally does. Confused? Don't be. The simple fact is that, when using the smbclient command to connect to a shared folder, one slash should be replaced by two slashes. So an address like \\keir-windows\sharedfolder\, normally used under Windows, becomes \\\\keir-windows\\sharedfolder\\. Here's how I'd connect to a folder called sharedfolder on the keir-windows computer: $ smbclient \\\\keir-windows\\sharedfolder\\

4. If the share name has a space in it, or a strange character (such as an exclamation mark), they too will need to be escaped with a backslash. So to connect to the shared folder accounts 2009! on the computer called keir-windows, we would type:

Deactivate Caps Lock M 147

Once connected you can manipulate files on the shared computer using FTP commands. See Tip 131, on page 173 for a brief rundown of the ftp command-line program. As in ftp, type help for a list of commands.

Was this article helpful?

0 0

Post a comment