Files and Directories


Finding Files

The find command is used to locate a file. The format for the command is:
     find path option

Path is the location to start searching. So the path / is the root of the entire directory tree. The path . is the current directory.

There are many options. You can locate files changed within the last n days; files of a certain size; files with a certain name; etc.

For example, to find all empty files in the current directory
     find . -empty
To find the file named bob in my account
     cd
     find . -name bob
To find all directories named "games" in the system's /usr directory
     find /usr -type d -name games
To find all the old junk Java files in my account that end with ".class"
     cd
     find . -name *.class
Never type the following, but to find every file on the system
     find / *

Suppose I know the banner program is somewhere close to the root of the system. The following command would search the entire directory tree for the file.
     find / -name banner
That would take a long time and produce hundreds of permission denied errors. So, I can limit the depth of my search by typing
     find / -maxdepth 3 -name banner


File Permissions

When you execute the command ls -al you get a long listing, similar to
drwx------  2 ACC.dannellys2 ACC.domain^users 4096 2010-09-13 12:39 .
drwxr-xr-x 37 ACC.dannellys2 ACC.domain^users 4096 2010-09-13 12:39 ..
-rwx------  1 ACC.dannellys2 ACC.domain^users 8566 2010-09-13 14:21 bob
-rw-------  1 ACC.dannellys2 ACC.domain^users  227 2010-08-25 12:53 bob.cpp
-rw-------  1 ACC.dannellys2 ACC.domain^users  524 2010-08-31 14:16 temp

The ten characters at the beginning of the line tell you the file permissions. The first character is either a "d" or "-". D indicates directory.

Next, there are three sets of three characters (9 total). The three sets indicate the permissions for "user", "group", then "other". You are the user. Everyone else is "other". You can share files with a selection of people by creating a "group".

The permission characters are
r - read
w - write
x - execute
- - none of the above

So, in the directory listing above the current directory (named .) is a directory in which I (the owner) can read, write, and execute files.

The parent directory (named ..) can be read and executed, but not written into, by everyone. Note: execute permission is required to run the ls command.

The file bob is an executable program. The files bob.cpp and temp are just regular files.

By the way, the file named temp is 524 bytes in size and was last changed on August 31, 2010. That file is owned by "ACC.dannellys2". And Dannelly belongs to the group named "ACC.domain^users".


Changing File Permissions

The command to change file permissions is:
     chmod options filename
Obviously, filename is the file you want to change.

Using the options you can add (+) or remove (-) permissions from the user (u) who owns the file, the group (g), others (o), or all users (a).

For example, to change the permission on the file listed above that is named "temp" so that I can not accidently write over it
     chmod u-w temp
In other words, remove write permission from the user. Now I have read-only access to that file.

To make a file readable to everyone, it takes two changes to permissions: the file must be readable, and the directory must be accessible. So, for me to make my file bob readable to you and everyone else, I have to type
     cd
     chmod a+rx .
     chmod a+r bob
In other words, first change to my home directory. Second, make my home directory (named .) visible to all. Finally, make bob readable by all.


Changing File Ownership

To change the owner of file from you to someone else, then command is
     chown filename newowner

If you are sharing files with someone, be careful of who owns the file. I recently gave Dr. Foster some of my old sample Java programs. Here is what we did:
First, Foster logged in and created a directory named dannellycode to hold all my stuff. Then he ran chmod on his home directory and his new directory to give me permission to write into his account.
Second, I logged in and typed
     cd java_samples
     cp * ~acc.fosterk/dannellycode/.
The copy worked, but Foster could not edit the files. Dannelly did the copy, so Dannelly still owned the files, even though the files were in Foster's account. So, Dannelly had to
     cd ~acc.fosterk/dannellycode
     chown * acc.fosterk

The next week, when Dr. Garrison asked for the same files, I simply gave her read and execute permission. She ran the copy command. Hence she owned the files and we did not have to change the owner.


Other interesting file Stuff

To find out how much disk space you are using
     du -h
To show a directory tree
     ls -R
Sometimes there are multiple versions of programs. For example, there is more than one c++ compiler. The "whereis" program tells you where an executable program is located. So, to find out where your c++ compiler is located
     whereis c++
If you run that command you will notice there are several c++ programs. The one that gets run is the first one on the list.


Assignment

1 - Run the following command:
     printerbanner Hello

2 - Create a new file named "me" with just one line
     printerbanner -w 30 yourname
Of course, use your name, not yourname. So, Professor Dannelly's me file would contain:
     printerbanner -w 30 Steve

3 - Make the file named "me" executable.

4 - Start a script file.

5 - Run the following commands:
     ls -l me
     me

6 - Stop your script. Print your script file.

7 - Let's go on a scavenger hunt. The directory tree that starts with "/var" contains a lot of shared files. What directory in that tree contains the file "mahjongg.difficult.scores"? Write your answer on your printout from step 6.

8 - Who owns that high scores file?

9 - Can you write a new high score?