Friday, October 16, 2009
Linux - Changing Directories with the 'cd' Command
Sunday, October 11, 2009
Linux - File Permissions and the 'chmod' Command
Linux – File Permissions and the ‘chmod’ Command
Linux was designed to be a multi-user operating system. So
there must be a way to limit who has access to what files, and how much access.
There are three sets of users in linux,
· User-this is an individual, such as yourself, that has login credentials to the system. Each user has their own home directory where they have full access to the files.
· Group-this is a set of multiple users. A group is typically created by the system admin. For instance, at a university, there could be a group setup for each course, where the professor is the group leader, and students are group members.
· Other-this refers to individuals that are not the user and not in the group being discussed.
Linux uses file permission attributes to set who has access to what files. Type in 'ls -l' in any directory with at least one file in it. This will show you all the information about each file/directory.
$ ls
-l
drwxr-x--- 1 jdoe jane 4096 Dec 28 04:09 tmp
-rw-r--r-- 1 jdoe jdoe 969 Dec 21 02:32 foo
|
Column |
Value |
Meaning |
|
1 |
drwxr-x--- |
File Permissions |
|
2 |
1 |
Forget this one! |
|
3 |
Jdoe |
Owner |
|
4 |
Jane |
Group owner |
|
5 |
4096 |
Size in bytes |
|
6 |
Dec |
Last Modified Month |
|
7 |
28 |
Last Modified Day |
|
8 |
04:09 |
Last Modified Time |
|
9 |
tmp |
File Name |
File Permission Attribute
Let's break down the 10 character file permission attribute in Column 1:
|
Special |
User |
Group |
other |
|
- |
--- |
--- |
--- |
· Special attribute- This is blank(-) for normal files, but it is set to d for directories. This can be other letters for other devices etc, but do not worry about that now.
· 'user' permission- The user refers to the person who owns the file. So if you own the file, these characters apply to you.
· 'group' permissions- These apply to any user who is in the group owned by the group owner.
· 'other' permissions- These apply to any one who is not the owner, and who is not in the group owned by the group owner.
Read, Write and Execute
The user, group, and other permissions can have read(r),
write(w), and or execute(x) permissions. The order of the permissions is 'rwx'
for each permission set. Each permission attribute can either be on(r,w, or x)
or off(-).
What does read, write and execute mean?
· Read(r)-the contents of the file/directory can be viewed.
· Write(w)-the file can be modified or deleted.
· Execute(x)-the file can be executed, or run. This only makes sense if the file is a program or script that does something. Also, having the execute permission for a directory means the directory can be cd'd into(eg. cd directoryName)
Examples
-rwxrwxrwx
All users have full access to this file.
-rw-r--r--
Owner has read, write access to the file. All other users have read access.
drwx------
This is a directory that the user has full access, while all other users have no access.
Changing the Permissions with 'chmod' Command
There are several ways to change the attributes with the chmod command. In my opinion, the easiest way to use chmod is as follows (you would insert file permissions after the equals sign, see examples below),
chmod u=,g=,o= fileName
Fittingly, u, g and o are as follows,
· u-user
· g-group
· o-other
If you leave out u,g or o, that attribute will not be modified.
chmod u=,g= fileName
Note: An error will occur if you put spaces between u=,g=.
Examples
$ chmod u=rwx,g=r,o=w foo
$ ls -l
-rwxr---w- jdoe jdoe 969 Dec 21 02:32 foo
Not including g will keep it the same as before,
$ chmod u=x,o=r foo
$ ls -l
---xr--r-- jdoe jdoe 969 Dec 21 02:32 foo
A blank after o will clear all attributes,
$ chmod u=rx,g=w,o= foo
$ ls -l
-r-x-w---- jdoe jdoe 969 Dec 21 02:32 foo
Using + and – to add and remove attributes
You can also use + and – to add and subtract attributes.
Examples
$ chmod u=wx,g=w,o= foo
$ ls -l
--wx-w---- jdoe jdoe 969 Dec 21 02:32 foo
$ chmod u+r-w,g-w+r,o+r foo
$ ls -l
-r-xr--r-- jdoe jdoe 969 Dec 21 02:32 foo
Using 3-digit octal value to set attributes
Lastly, you can specify the attributes with a 3 digit octal. Each of the user, group and other permissions have 3 attributes(rwx) that can be on or off, and as such can be thought of as a 3 bit word which is easily converted to an octal digit(0-7),
chmod (u#)(g#)(o#) fileName
· 0='---'
· 1='--x'
· 2='-w-'
· 3='-wx'
· 4='r--'
· 5='r-x'
· 6='rw-'
· 7='rwx'
Example
$ chmod 764 foo
$ ls -l
-rwxrw-r-- jdoe jdoe 969 Dec 21 02:32 foo
Previous - Finding A File | Next - Creating and Deleting Files and Directories
Friday, October 9, 2009
Linux - Bash Shell Scripting - Expressions and Substrings
Linux - Bash Shell Scripting - Expressions and Substrings
Expressions
Sometimes in a script, you would like to evaluate an expression. For example, to find the sum of two variables. The expr command can be used to do this,
$ cat testsum.sh
#!/bin/bash
v1=12
v2=39
v3=`expr $v1 + $v2`
echo $v3
$ ./testsum.sh
51
Note that the expr command must be enclosed in backquotes `…` for this to properly work. Typing ‘man expr’ will display a help screen about the expr command.
Substrings
The expr substr command can be used to extract a substring from a string variable, e.g., this program prints the individual characters of a string stored in a variable v,
v="foobar"
len=`expr length $v`
for i in `seq 1 1 $len`; do
echo -n `expr substr $v $i 1`
done
echo
The syntax of expr substr is, 'expr substr string pos len' which will extract len characters from the string string starting at position pos. Note that the first character of the string string is at position 1.
Previous - For and While Loops
Linux - Bash Shell Scripting - For and While Loops
For Loop
The basic syntax of the for loop is:
for var in list; do
some commands
done
Note the semicolon; it is important to not omit it. The commands are executed once for every item in the list. The current item is accessed through the variable $var, e.g.,
Use VI to edit for.sh
--------------------------------
for i in 1 2 3 4 5 6 7 8 9 10; do
echo -n "$i "
done
echo
--------------------------------
$ chmod 755 for.sh
$ ./for.sh
1 2 3 4 5 6 7 8 9 10
The option -n to echo means do not print a newline character after outputting the text.
Another example,
for i in $(ls); do
echo $i
done
This script display the name of each of the files in the current directory one per output line. Another example,
Use VI to edit ls-dirs.sh
--------------------------------
for i in *; do
if [ -d "$i" ]; then
echo "$i [dir]"
else
echo $i
fi
done
--------------------------------
$ mkdir foo1
$ mkdir foo2
$ chmod 755 ls-dirs.sh
$ alias ls-dirs=./ls-dirs.sh
$ ls
foo1 foo2 for.sh if.sh ls-dirs.sh
$ ls-dirs
foo1 [dir]
foo2 [dir]
The seq command (type man seq for help) can be used to generate an integer sequence. The syntax is, seq first increment last which will generate an integer sequence starting at first, incrementing by increment, and stopping at last. For example, Here is a loop which displays the first 100 odd integers,
for i in `seq 1 2 100`; do
echo -n $i " "
done
Note that the seq command must be enclosed in backquotes `seq…` so the shell will execute it to generate the desired output. This loop prints the first 100 odd integers in reverse order,
for i in `seq 99 -2 1`; do
echo -n $i " "
done
While Loops
The basic syntax of the while loop is,
while [ conditional-expression ]; do
statements
done
I have ommitted examples as the while loop is used in a similar context to the for loop.
Linux - Bash Shell Scripting - If Statements
if [ conditional-expression ]; then
or
if [ conditional-expression ]; then
some commands
else
some other commands
fi
The character "[" actually refers to a built-in Bash command—it is a synonym for another command named test. The result of the conditional expression is 0 or nonzero with 0 being false and nonzero being true. Some examples,
Use VI to edit if.sh
--------------------------------
today=0
if [ $today = 1 ]; then
echo "today = 1"
else
echo "today <> 1"
fi
--------------------------------
$ chmod 755 if.sh
$ ./if.sh
today <> 1
Since using [ ] is equivalent to using test,
today=0
if test $today = 1; then
echo "today = 1"
else
echo "today <> 1"
fi
The test command has command line options for checking the status of files, e.g.,
test -e file
True if file exists
Multiple conditions can be checked using -a (and) and -o (or), e.g.,
if test -e if.sh -a -x if.sh; then
echo "if.sh is an executable"
else
echo "if.sh either does not exist or it is not an executable"
fi
In a schell script, the ! symbol is used as the NOT logical operator, e.g.,
echo '"'src'"' does not exist, creating directory.
mkdir src
fi
To learn more about conditional expressions I suggest you try $ man test.
Linux - Bash Shell Scripting - Variables in Output
VarName=$(command)
#!/bin/bash
LS_OUT=$(ls)
echo $LS_OUT
Or
#!/bin/bash
#the 'date' command retrieves the current date(many options)
DOW=$(date +%a) #get day of week
MONTH=$(date +%b)
DAY=$(date +%d)
YEAR=$(date +%Y)
echo "Today is ${DOW} ${MONTH}-${DAY}-${YEAR}"
$ alias today="./today.sh"
$ today
Today is Wed Oct-17-2007
Linux - Bash Shell Scripting - Reading From the Keyboard
#!/bin/bash
echo "Enter your name: "
read name
echo "Pleased to meet you $name."