Potohar College of Science Kalar Syedan, Rawalpindi
ENGR 1340
Bash command
1. pwd: present working directory
2. man x: manual of command x\
3. mkdir: create directory
a. mkdir lab
b. mkdir lab1 lab2
c. mkdir “lab 1”
4. rmdir: remove directory
5. mv: rename directory
Useful command:
1. rm -rf or rm -r -f: delete entirely a
non-empty direc
...[Show More]
Bash command
1. pwd: present working directory
2. man x: manual of command x\
3. mkdir: create directory
a. mkdir lab
b. mkdir lab1 lab2
c. mkdir “lab 1”
4. rmdir: remove directory
5. mv: rename directory
Useful command:
1. rm -rf or rm -r -f: delete entirely a
non-empty directory
2. mv lab “lab1” : rename lab dir to lab1 dir
3. ls -l: list the contents in long format which
contain (file size, owner, …)
4. ls -a: list all contents including hidden
ones.
File management:
1. touch file.txt: create file.txt
2. cat file.txt: display content of file
3. mv hello.txt mydir: move file to dir
Useful commands:
1. cp file1 file2: copy file1 into file2
2. cp -r dir1 dir2: copy dir1 into dir2 incl
sub-dir
3. For mv, if two args are the same type, it act
as a rename func
File Permission:
1.User, group, Other
2.Read(r), write(w), execute(x)
-rw
(-): file
(d): directory
Change permissions:
1.chmod [who][operator][permissions] filename
who: u,g,o,a
Operator: +(add), -(take away permission), = (set
permission)
Other useful command
grep ‘abc’ file
cut -d, -f2 file
wc file (no. lines, words, characters)
sort file (alphabetically)
uniq file (remove adjacent duplicate)
Pipe (|)
find [path] [-name] [-type]
Regular expression
grep -E
. : match any single char
^ : match the beginning
$: match the end
?: zero or one occurrence
+: one or more occurrence
* : zero or more occurrence
[ ]: char inside the bracket will be matched
\: escape char
pattern {n}: match n occurrences of pattern
pattern {n,}: match at least n occurrences
pattern {n,m}: match between n and m occurrences
(ab) {3}: 3 occurrence of ‘ab’
( ) : mark substring
Shell Script
#!/bin/bash
Variable: pet=”dog”
*no space is allowed
To retrieve value, use “$”
Command substitution use backquotes (``)
Get the length of string: ${#a}
Substring: ${a:pos:len}
Replace: ${a/$from/$to}
Mathematical variabile: use “let” code
Get command line arg:
./1.sh hello world
$# = 2
$0 ./1.sh, $1 hello, $2 world
If-else:
if [ condition ]
then
Perform some action
fi
String comparison:
\>: string1 sorted after string2
\<: string1 sorted before string2
File/Directory checking:
[ -e $file ]: true iff file exist
[ -f $file ]: true iff file is file
[ -d $file ]: true iff file is dir
[ -s $file ]: true iff file has size > 0
Number comparison:
[ $a -eq $b ] true iff a = b
[ $a -ne $b ] true iff a != b
[ $a -lt $b ] true iff a < b
[ $a -le $b ] true iff a <= b
[ $a -gt $b ] true iff a > b
[ $a -ge $b ] true iff a >= b
list = “1 2 3 4 5”
[Show Less]