Task:
Everything is a file.
(Directories, sockets, streams, web addresses, links, etc.
All are files, so obey the same basic interface.)
KISS : Keep It Simple Stupid.
(Commands should do only what is asked,
and should report no output unless there is an error.)
Modularize design.
(Each command should do only one thing well.
Chain them together to do complex tasks.)
Files are organized into a tree structure in linux:
~$ tree -L 1 /
/
├── bin
├── boot
├── dev
├── etc
├── home
├── initrd.img -> boot/initrd.img-3.19.0-26-generic
├── initrd.img.old -> boot/initrd.img-3.19.0-25-generic
├── lib
├── lib32
├── lib64
├── lost+found
├── media
├── mnt
├── opt
├── proc
├── projects
├── projects2
├── root
├── run
├── sage
├── sbin
├── snapshots
├── srv
├── sys
├── tmp
├── tmp2
├── usr
├── var
├── vmlinuz -> boot/vmlinuz-3.19.0-26-generic
└── vmlinuz.old -> boot/vmlinuz-3.19.0-25-generic
26 directories, 4 files
Files are leaves.
Directories are branches.
The "root" directory is "/"
Paths to files are written as:
Special paths:
Useful bash commands:
Task:
Useful control commands:
Commands in bash may be given options:
~$ ls -l
...
~$ ls --all
...
~$ ls -la
...
~$ ls -lh
...
Examples:
Try these : what do they do?
(Note: not available in git-bash for Windows)
Command usually have a "manual page" to give you help:
~$ man ls
Task :
Task :
~$ tree -L 1 /
~/info$ ls -lah
total 157K
drwx------ 4 user user 11 Aug 29 06:57 .
drwx------ 5 user user 6 Aug 29 05:54 ..
drwx------ 8 user user 16 Aug 29 07:08 .git
-rw------- 1 user user 1.2K Aug 29 03:07 .gitignore
-rw------- 1 user user 551 Aug 29 06:56 .vimrc
-rw------- 1 user user 26K Aug 29 03:07 Chapman Coding Standards.pdf
-rw------- 1 user user 12K Aug 29 03:19 GettingStarted.md
-rw------- 1 user user 1.1K Aug 29 03:07 LICENSE
-rw------- 1 user user 6.7K Aug 29 03:07 README.md
drwx------ 4 user user 9 Aug 29 06:59 Templates
~/info$
~$ whoami
user
Permissions - Link # - User - Group - Size - Last Modified - Name
"ls -l" outputs: (learn not to be intimidated by cryptic output!)
Permissions: (as below, omissions indicated by a '-')
Hidden Files: Begin with period ".blah", revealed only with "-a"
Changing permissions:
~$ chmod +x filename # sets filename executable for user
~$ chmod u+w,g-w,o-rw filename # user +write, group -write, other -read&write
~$ thisisavariable="thisisastring"
~$ echo $thisisavariable
thisisastring
(what does echo do?)
~$ dircontents=$(ls)
~$ echo $dircontents
2015-08-30-183918.term github randomwalk.sagews
~$ t1="one"; t2="one"; if [ $t1 = $t2 ]; then echo "Match."; else echo "No match"; fi
Match.
~$ t1="one"; t2="two"; if [ $t1 = $t2 ]; then echo "Match."; else echo "No match"; fi
No match
(Note: the semicolons ; may be replaced with new lines for readability)
~$ if [ $t1 = $t2 ]; then
> echo "Match."
> else
> echo "No match."
> fi
No match.
~$ for w in this is a series of words; do echo $w.ext; done | less
~$ for i in $(seq 9); do echo -n $i; done; echo
123456789
(what does less do?)
~$ for w in this is a series of words; do echo $w.ext; done > output.tmp
~$ cat output.tmp
(what does cat do?)
~$ for i in $(seq 10000); do echo $i >> output.tmp; done
~$ cat output.tmp | grep 9 | head -n 10
(what do grep and head do?)
(what does seq do?)
~$ for user in /home/*; do echo ${user#/home/}; done
(Note: it is wise to use echo as above to make sure commands are correct first,
then press the up arrow to recall previous command, and remove echo second)
~$ for f in $(seq -s ".jpg " 10).jpg; do echo mv $f ${f/.jpg/.png}; done
Ex: ${var/str1/str2} replaces all instances of str1 in var with str2.
Ex: ${var#str1} removes one instance of str1 at start of var.
Ex: ${var%str1} removes one instance of str1 at end of var.
~$ for img in /home/*.jpg; do echo ${img%.jpg}.png; done
All programs in linux have an "exit code", which is an integer value.
The success code is 0, which also means True in an if statement.
If a program exits with a nonzero value, it has failed (False). However, the specific number can give more information about exactly what failed. (See the man page for the program for what codes mean what.)
~$ bash -c "exit 0"
~$ echo $?
0
~$ bash -c "exit 1"
~$ echo $?
1
The "exit" command exits a script and returns the indicated exit code.
The special variable $? stores the exit code of the last run program.
The other special variables $1, $2, $3, etc. store the value of the first, second, and third command line argument, etc.
The variable $0 stores the name of the script itself.
#!/bin/bash
# This is a comment
echo "This is a line of code"
if [ $# == 0 ]; then
echo "No command line arguments"
exit 1 # This exits as a failure
fi
echo $1 # prints first argument
exit 0 # This exits as a success
Contents of file script.sh :
First line tells linux it is a bash script.
$# stores the number of script arguments
The final "exit 0" is usually omitted as implicit.
To make the script executable:
~$ chmod +x ./script.sh
To run the script directly:
~$ ./script.sh
Task: Try creating this script.
Practice makes perfect.
Keep references handy until you remember commands on command.