Copying Files

 One of the most common problems encountered when working with UNIX systems is file permissions. This is due to the level of security built into UNIX systems and is actually a great benefit. This gives you the ability to control who gets access to your files, who can share files, and what files are to be locked and restricted from everyone else. It’s great to have such control over your system. However, if someone wants access to a particular file and they can’t get at it, chances are you’ll have to get in there and change them so they can. This UNIX How-to explains just how to do that.

 This time we’ll cover the following commands:

  • cp
  • Mv
  • rm
  • mkdir
  • rmdir
  • chmod

 So, let’s start with copying files since it’s one of the most common commands and also one of the easiest. When using cp, you must specify the name of the file (and path if necessary) you want to copy, and the destination path. If you wanted to copy the file UNIX.htm in my home directory to sean’s home directory, you would type:

cp /home/dan/UNIX.htm /home/sean

Of course, that’s the longhanded version. If you remember the last how-to, on relative and absolute pathnames, there are a few shortcuts that can be used. If you were in the /home/dan directory, you could type:

cd UNIX.htm ../sean

This command will look for any files in the current directory called UNIX.htm and copy them to sean’s home directory. Remember that .. means “one directory up”, in our case it would be /home. Piece of cake. You can also rename the file UNIX.htm with the cp command by simply adding the new filename to the destination path:

cp /home/dans/UNIX.htm /home/sean/UNIX._new.htm

or you can even create a duplicate of the UNIX.htm file in the current directory:

cp UNIX.htm UNIX_backup.htm

This can be helpful when creating backups of important files before they’re edited. You can also copy multiple files to a single location with cp. Let’s say you had a bunch of html files that needed to be moved over to Lyle’s home directory. You would type:

cp *.htm ../lyle

Or if only two html files needed to be copied to his directory:

cp UNIX.htm bogus.htm ../lyle
Moving and deleting files

 The mv command works almost identically to the cp command. But instead of just copying the file, it moves it. Typing:

mv /home/dan/UNIX.htm /home/sean

would copy the UNIX.html file into sean’s home directory and then remove it from my directory. mv is also used to rename files. If you wanted to rename the UNIX.test file to UNIX.htm file, type:

mv UNIX.test UNIX.htm

Okay, so now you know a little bit about copying and moving files around. What about deleting all of those old crap files you don’t need anymore? An easy but dangerous command called rm can solve all your problems (and maybe create some new ones!) Let’s say you needed to delete the file UNIX.backup:

rm UNIX.backup

Simple as that. The UNIX system won’t bog you down with questions like, “Are you sure you want to delete this file?”. Just type in the command, and it’s gone. There’s no recycle bin or trash can although recovering a deleted file is about as likely as a snowball’s chance in hell, so be careful to make sure you really want to delete it beforehand. The rm command is extremely powerful, and in the wrong hands can wreak havoc on a system. (More on this later!)

You can remove multiple files at once with the rm command. So, if you wanted to delete all files that ended with backup, you would type:

rm *backup

Or if you wanted to remove two files called UNIX.backup and foo.backup:

rm UNIX.backup foo.backup
Moving and deleting directories

Okay, so all of the files have been deleted. Now what about all those lingering directories? If you try to remove directories with rm, you’ll get an error:

rm foobar
rm: foobar directory

The command rmdir is designed to remove directories – but only if the directory is empty. If you used mkdir to create a directory, and there are no files in there, you could use rmdir to trash it:

rmdir foobar

And it’s gone. But what if the directory has files in it?

rmdir foobar
rmdir: foobar: Directory not empty

Damn. You could go in there and clean it out with rm and rmdir but what if it’s made up of hundreds of files and directories? There is an easier way:

rm -r foobar

The -r flag means recursive. This means the rm command is invoked on every single file and directory wherever rm -r is executed. It’s very efficient, but very dangerous to use.

If you want to feel a little safer before using the

rm

command, you can use the

-i

option (interactive mode) so that you will be prompted to make a choice whether or not to delete each file:

rm -ir foobar
examine files in directory foobar ? y
remove foobar/new ? y
remove foobar/crap ? y
remove foobar/totalcrap ? y
remove foobar/bogus ? y
remove foobar: ? y

Notice how it asks you to delete each file, and then the directory itself. This can be nice, but is a bit too slow for most tastes. If you wanted to get rid of your entire netscape directory, you probably wouldn’t want to have be prompted to hit “y” hundreds or even thousands of times.

Ok, for those of you who love to be naughty, here’s the single worst command you could execute on your UNIX system:

cd /
rm -r *

What does this do? It erases the entire root filesystem without ever asking you, “Are you sure you want to do this?” That’s it, adios amigos. Break out the media and reinstall.

Can any user get away with this? Probably not. That’s due to one of the most powerful features of the UNIX environment: File Permissions.
File permissions

 File permissions are tricky business, but once you get them down, they’re a piece of cake. Let’s take a look at the file permissions of some files and directories. The ls -l command displays the “long” list of files, directories and permissions. So go ahead and run it:

#ls -l drwxr-xr-x 5 dan dan 1024 Jul 23 13:21 Desktop drwx—— 2 dan dan 1024 Sep 17 14:23 Mail drwxr-xr-x 26 dan root 1024 Sep 16 15:27 Office51 -rw-r–r– 1 dan root 4714 Sep 29 13:12 UNIX.txt -rw-rw-r– 1 dan dan 10106880 Sep 16 11:30 navigator-v408.tar -rw-rw-r– 1 dan dan 7 Jul 23 14:18 newfile drwx—— 2 dan dan 1024 Jul 23 14:45 nsmail -rw-rw-r– 1 dan dan 115407 Sep 3 12:27 relnotes.ps -rw-rw-r– 1 dan dan 54387 Sep 3 12:27 relnotes.txt drwxrwxr-x 3 root root 1024 Sep 16 13:46 staroffice

Okay, what we’re interested in is the columns of letters d, r, w, and x. From last time you should remember that the “d” in the first column means it’s a directory. The “-” means it’s a file.

 So, we’ve got 10 columns, each with different letters and no idea what it all means. In those 10 columns are three areas of distinction for file permissions: owner, group, other. Here’s how they read:

d rwx rwx rwx directory/file owner file perms group file perms other file perms

As you’ve noticed, there are three sets of rwx letters, each specifying permissions for different groups or individuals. So, just what does rwx mean?

r- read permissions w- write permissions x- execute permissions

If a file is listed as having rw- permissions, it means you can read the file, edit the file, but you can’t execute it. This is because the x is replaced with a “-“. This goes for all the settings. If a file has r-w permissions, it means you can read the file, execute it, but you can’t edit it. Make sense?

Let’s take a closer look at the file UNIX.txt:

-rw-r–r– 1 dan root 4714 Sep 29 13:12 UNIX.txt

The first column reads “-“, so we know it’s a file. Owner file permissions read “rw-“, which means the owner (dan) can read and edit the file, but not execute it. Anyone else that shares the group root can only read the file. “r–“. And anyone else on the system can only read the file as well “r–“. If this distinction isn’t clear between owner, groups and others, let’s take a closer look at each one:

 Owner should be pretty obvious. It’s the owner of the file or the creator. If I’m logged into a system as dan, any file I create will have dan listed as its owner.

Users can also belong to a series of groups, specifically to enable a wider range of file sharing. For example, let’s say there are three groups: Engineering, Doc, and Test. All engineers are part of the Engineering group, all Doc writers are a part of the Doc group, and all Test engineers are part of the Test group. If I was a part of the Test group and wanted to access the file Testresults, it would have to have “test” as the group name.

-rwxrw—- 1 paul test 4714 Dec 7 14:14 Testresults

Okay, so here we see that paul is the owner, and he has read, write and execute permissions to the file Testresults “rwx”. Being a part of the test group, I can see that I would have read and write, but not execute permissions “rw-“.

Let’s say I was a part of the group “Sales” and wanted to get a sneak peek at the latest Testresults for our product. Could I?
 Let’s take a look at the permissions again… Since I’m not the owner, I don’t have owner permissions, and since I’m not a part of the Test group, I don’t have their group’s access permissions, so I’m left with the last category… “other”. In this case, it’s “—” which means I wouldn’t have read, write or execute permission for this file at all.
 I could try and access it, but here’s what would happen:
more Testresults
more: Permission denied

Snubbed!

Changing file permissions

 Okay, let’s pretend that we’re still in the “Sales” group and we went to Paul to see if he could change the permission on the file so we could read it. The command to change a file’s permissions is called chmod (change mode). Paul could type something like this to let sales in on the file Testresults:

chmod 764 Testresults

The command arguments require the permission settings (in this case, the 764) and then the file name to change the permissions on. Of course, the person changing permissions must have write permission to the file already – and in this case, paul does. But what the hell does 764 mean?

Each permission string has a binary equivalent. We won’t get into the dirty details of it all here – just use this handy dandy cheat sheet when invoking chmod:

— 0 –x 1 -w- 2 -wx 3 r– 4 r-x 5 rw- 6 rwx 7

So if Paul used the 764 argument, what would the new permissions look like? Old Permissions:

-rwxrw—- 1 paul test 4714 Dec 7 14:14 Testresults

New Permissions:

-rwxrw-r– 1 paul test 4714 Dec 7 14:14 Testresults

So you see how the 7,6,4 translate to the file permissions:

7 6 4 rwx rw- r–

By invoking this chmod command, Paul has granted anyone in the Sales group read permissions to the file only. If they tried to change it, they couldn’t because they don’t have that permission. Don’t be worried if you don’t quite get it yet. Once you start mucking about with your own files, changing permissions to your heart’s content, you’ll quickly get a hold of what it’s all about.