You are on page 1of 4

Linux common cmd:

- pwd: show working directory


- mkdir <new directory name> : create new folder
- Delete folder or file:
+ rm -r <folder_name>
+ rm <file_name>
- Create an empty file: touch <file_name>
- Copy files: cp <file_need_copy> <where_copy_to>
- Move files: mv <file_need_move> <place_to_go>
- Edit file content: nano <file name>
+ Search text: Ctrl + W
-> Move to next search position: Alt + W
+ Save changes: Ctrl + O
+ Exit nano: Ctrl + X
- View file content: cat <file_name>
+ -n <line number> :
- locate: search files in existing database
- wc: display info about file
- history: rv history of entered cmd
- diff: compare 2 files
- df: report on system disk space usage
- du: check how much space a file or folder takes up
Redirect:
ls > list.txt: write the output of ls cmd into list file
+ >: overwrite entire file content
+ >> : Serialize the existing content on the file
+ 2> : Save output to file only if command fails
Redirect: <
student01@lab−computer :~ $ cat mycal

September 2022
Su Mo Tu We Th Fr Sa
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 2

student01@lab−computer :~ $ tr ’ a−z ’ ’A−Z ’ < mycal

SEPTEMBER 2022
SU MO TU WE TH FR SA
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

tr: replace characters in string

|: redirect output of a command as a input of another command

FILTER OUTPUT WITH GREP:


ls | grep txt
PERMISSION ON LINUX
- Change permission for files and directories:
chmod <options> <permission index> <file/directory name>
VD: chmod 744 a.sh => change permission to execute a.sh file

DIRECTORY/ FOLDER AND FILE COMMAND


- echo “text-content…” > text1 : create text1 file with content
- remove folder which has subfolder: rm -r folder
=> put all bash commands to a script file: (with extension “.sh”)
#! /bin/bash

Cmd1
Cmd2

SCRIPT FILE
MyVariable = “I will do some math!”
Number = 1

Echo $MyVariable $Number + $Number = $((Number + Number))

Output: I will do some math!: 1 + 1 = 2

=> Ensure execute permission: chmod +x bashvar.sh


Loop:
for VARIABLE in 1 2 3 4 5 … N
do
cmd1 on $VARIABLE
cmd2

cmdN
done
Gcc command:
gcc -o hello hello.c

Makefile rule: exact name “Makefile”

<target> : <dependency 1>< dependency 2>


<tab> cmd1
<tab> cmd2

BASH SCRIPT PROGRAMMING

You might also like