Wildcards provide a shortcut for file handling in the bash shell. Wildcards can be used to match multiple filenames, making moves, copies, and deletions less work. The wildcard characters are:
| Character | Description | Examples |
|---|---|---|
| ? | matches a single character | test?.c matches test1.c through
test9.c and testa.c through testz.c |
| * | matches 0 or more characters | test*.c matches test.c, test1.c
through test9999….c, and testwhatever.c |
| [chars] | matches any characters in the brackets | test[1-9].c matches test1.c
through test9.c |
file
The file command helps you identify files for type and purpose, as in these examples:
file
/home/dbrown> file CP367/ctest CP367/ctest: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=e9a5ffe3f20638c014cff096402e09bba16d68fa, not stripped /home/dbrown> file CP367/ctest.c CP367/ctest.c: C source, ASCII text /home/dbrown> file CP367 CP367: directory
find
The find allows you to search for files by file name, type of file, directory, or even file size. It automatically searches subdirectories of the start directory you name. The command skeleton is:
find directory -name "targetfile" -type type -size size -mtime time -ls
(There are more options, but these are a good start.)
Examples:
| Command | Explanation |
|---|---|
find . |
Lists the names and directories of all the files in and below the current folder. |
find . -type f |
Lists the names of all files (f) in and below
the current directory
|
find . -type d |
Lists the names of all directories (d) in and
below the current directory
|
find . -size +25k |
Lists the name of all files bigger than 25 KB in and below the current directory |
find . -mtime -7 |
Lists the name of all files in and below the current directory that have been modified within the last seven days |
find . -mtime +7 |
Lists the name of all files in and below the current directory that have not been modified within the last seven days |
find . -name "*.c" |
Lists the name of all files in and below the current
directory that end in ".c"
|
find . -type d -name "cp*" -ls |
Lists all the information for all directories in and below
the current directory that start with "cp" (the order
of the options in important - try moving the -ls
earlier in the command
|
find has numerous other options - use man find to see more.
Last week you looked briefly at the output of the ls -l command. the first ten characters of the long listing gave information about file types and permissions. The first character notes the file type, and the next nine characters identify the file permissions. A file permission simply tells the unix system whom is allowed to read, write, or execute a file.
The nine characters are divided up into three groups of three characters each. The three areas of permission (in order from left to right) are:
hopper is
the student groupEach group of three permissions has three characters. These characters represent the following permissions, in order:
r allows read permission for
the permission areaw allows write permission for
the permission areax allows execution
permission for the permission areaExamples:
Read, write, and execute permission for the owner
| Permissions | Explanation |
|---|---|
r-xr-x--- |
Read and execute permission for the user and the group |
rwx------ |
Read, write, and execute permission for the owner |
r--r--r-- |
Read permission for other |
rwxr-xr-- |
Read, write, and execute permission for the owner, read and execute permission for the group, and read permission for other |
Changing Permissions
File and directory permissions are changed with the chmod command. chmod (change mode) is a complex command, and you are welcome to use it in any way you understand, but we will show you one simple way to use it. The command skeleton is:
chmod [-R] who[+/-/=]permissions file
-R: [optional] recurse into subdirectories -
changes permissions in the current directory and any subdirectories
it contains
who
: set permissions for the owner, group, or other. The options are:
| Code | Meaning |
|---|---|
u |
The file owner |
g |
The group |
o |
All other users |
a |
All of the above |
permissions
: the read, write, and execute permissions to set:
+r / -r |
add (+) / remove (-) read
permissions
|
+w / -w |
add (+) / remove (-) write
permissions
|
+x / -x |
add (+) / remove (-) execution
permissions
|
chmod Permission Examples
| Command | Result | Description |
|---|---|---|
chmod a=rx ctest.c |
r-xr-xr-x |
changes the permissions on ctest.c to read and
execute for everyone - write is turned off
|
chmod u+rwx,g+rx-w,o-rwx
ctest.c |
rwxr-x--- |
changes the permissions on ctest.c to read,
write, and execute for owner, read and execute for group, and no
permissions for others
|
chmod -R u+rwx,g+rx-w,o= CP367 |
rwxr-x--- |
changes the permissions on everything in the CP367
directory - note that = without any follow up
characters removes all permisions
|
A process (or thread) represents CPU time and
calculation resources that are allocated to a program when that
program is executed. A multi-processor, multi-user machine such as
hopper
may support tens of thousands of simultaneous processes. Generally
individual users have a limit to the number of processes they may
run (256, for example), because despite having such a large number
of available processes, very clever (or very stupid) users can bring
a unix machine to its knees by using up all of the available
processes.
Processes are numbered. Using this number, you can determine when a process was started, how much CPU time it is using, and what its parent process is, if any.
ps
(Process Status) Command
The
ps
command lists running processes, and in particular tells you process
numbers. This is important, because once you know a process number
you can kill the process if it is a runaway process. The
ps -f
command displays information like this:
ps -f
/home/dbrown> ps -f UID PID PPID C STIME TTY TIME CMD dbrown 46379 24231 99 12:40 pts/1 00:01:09 ./ouch dbrown 46482 24231 99 12:42 pts/1 00:00:15 ./ouch dbrown 39286 39283 0 16:11 pts/1 00:00:00 -bash dbrown 46452 39286 0 17:40 pts/1 00:00:00 emacs CP367/ctest.c dbrown 46506 39286 0 17:41 pts/1 00:00:00 ps -f
The useful parts of the
ps -f
output are:
UID
PID
PPID
STIME
TIME
CMD
This sample output tells us a number of things:
dbrown is using the bash shell,
and running emacs, and has a couple versions of the program ouch
running. The ./ in front of the ouch
command means it is very likely that these are programs written by
the user and being run from a user's directory.
bash process is the parent process for the
ps and emacs commands. The ouch processes are running under the
parent process 24231, which is a different parent process, implying
that they were started in an different session.
ps, bash, or emacs
commands are using any significant time resources on the server.
The ouch commands, on the other hand, are, as the TIME
field shows.
ouch commands come
before the start time for the current bash session.
This implies that the ouch commands are left over from
the user's previous on-line session, and therefore must be runaway
processes. These runaway processes should be killed.
To see only the processes owned by a particular user, generally yourself, enter:
ps -fu login
Thus, to list only the processes owned by the
dbrown
account, enter:
ps -fu dbrown
kill
(Terminate Process) Command
The
kill
command allows you to terminate processes that you own. You may kill
an individual process or a parent process. Killing a parent process
kills all child processes. Although there are various forms of the
command,
kill -9 PID
is typically used. To kill the emacs process listed above (id: 46452), for example:
kill -9
/home/dbrown> kill -9 46452 /home/dbrown> ps -f UID PID PPID C STIME TTY TIME CMD dbrown 46379 24231 99 12:40 pts/1 00:03:25 ./ouch dbrown 46482 24231 99 12:42 pts/1 00:02:32 ./ouch dbrown 39286 39283 0 16:11 pts/1 00:00:00 -bash dbrown 46659 39286 0 17:43 pts/1 00:00:00 ps -f [2]+ Killed emacs CP367/ctest.c
Attempting to kill a process you do not own fails with an error message:
kill
/home/dbrown> kill -9 1 -bash: kill: (1) - Operation not permitted
Upon occasion, child processes either cannot be killed or have
sibling processes automatically spawned by a parent process for
reasons that we will not go into. Killing the parent process kills
the children and the spawning. Be cautious with this, however. The
following command successfully kills the parent (id: 39286) of the
emacs
process:
/home/dbrown> kill -9 39286
but because it is the
bash
shell process, you have just killed the shell and closed your
connection.
Get a partner. Try to access one of your partner's directories
using
ls
. What happens?
Both your and your partner change permissions on one of your existing directories. What do you have to do to allow your partner to see the contents of your directories, and visa-versa?
Both your and your partner change permissions on one of your existing directories. What do you have to do to allow your partner to create a file in one of your directories, and visa-versa?
Attempt to delete a file that your partner created in your directory. How's that working for you?