Last modified: April 07, 2020

Twelve Useful Command-line Procedures

Introduction

Nothing to see here, move along ...

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11. Changing Permissions on Many Files

The "find" command traverses down a directory tree. Unlike most other commands, it does not follow the command line argument standard (SVID) because it's an old command and I don't think they could have converted it to the standard without losing functionality and destroying backward-compatibility. Here is how it can be used to change the permissions on many files in a directory tree.

find . -type f -exec chmod a+rX,u+w,og-w {} \;

The first argument is the starting point, in this case "." for the current directory. Multiple directories can be given. Next is a condition, "-type f", meaning only work on files, and ignore directories, symlinks, devices, and so on. Next, "-exec" denotes there is a command to execute, and here we are going to change permissions on any file found (I'll explain the chmod arguments later). The curly braces "{}" are a standin for the file found when find gets a match. Finally, the semicolon denotes the end of the "-exec" argument, and we have to escape it with a backslash so it doesn't get absorbed by the calling shell.

Find is unbelievably powerful and occasionally dangerous, but it is well worth learning and having in your toolbox.

As far as the "chmod" command goes, the arguments tell it to give "a" (all, everyone or world) read and execute permissions, "u" (the user) write permission, and "og" (other and group) get write permission taken away. These arguments, when separated by commas, stack, so this is roughly the same as "chmod 644" or "chmod 755". I say "roughly" because if you notice I used a capital "X" instead of a small "x". This is a small amount of magic that says "only set execute if the file already had execute set in one of its permissions, otherwise don't." This prevents the command as shown from giving execute permission to files that shouldn't have it.

12. tar Pipes

One of the easiest ways to make a copy of a directory tree is to use a tar pipe such as this, where "oldparent" is the directory above the directory you want to copy, and "newparent" is the directory under which you want the copy:

sudo tcsh
cd oldparent
tar cef - directory | (cd newparent && tar xepf -)

The "sudo" is necessary because the tar command doing the copying has to be able to see all files, including those that might be protected from you under your normal user ID, and the tar file doing the extraction has to run as root in order to set the file and directory ownerships properly. If the files are owned by you, you can omit the "sudo" command. Note that this is one of the ONLY cases where I advocate using a root shell, and you should exit out as soon as you're done.

This can also be used to copy a directory tree from one machine to another:

(On source machine)

sudo tcsh
cd oldparent
tar cef - directory | rsh othermachine 'cd newparent && tar xepf -'

Where parent is the directory above where you want your stuff. Note also that you have to put the source computer in the "/.rhosts" and "/usr/local/etc/hosts.allow" file on the destination computer, and remove those entries until you're done.

A great discussion of this can be found on this Destroy All Software page.

Conclusion

I still have a lot of work to do on this page ...

Made with CSS   Valid XHTML 1.1!   Valid CSS!