Missing Semesters (2020) 1강 The Shell
Shell
- One of the primary ways of interacting with the computer
- Visual interfaces are limited
- You can only use the functions that the buttons provide.
- Most platforms provide some sort of shells
- Windows - Powershell
- Linux - Bash shell
- MacOS - Bash shell
Shell prompt
- We can customise our shell prompts
- We get to write shell commands on the shell prompts
- We can execute a program with arugments
- Examples:
dateprints today’s dateechoprints out the arguments given- arguments are something that’s separated by a blank space
echo hello.echo "Hello world"echo Hello\ world
- arguments are something that’s separated by a blank space
- How does the shell know what these programs are?
- Your OS comes with some built-in programs, stored on your file system.
- Environment variable
- Things that are set when we start the terminal.
- e.g. Where is the home directory? What is my user name?
- Path variable
echo $Pathwill show all the directories that the shell will search for programs.- When we try to run a program on terminal, bash will search through all the paths stated in the path variables for the file/program that matches with the program we typed onto the terminal.
- e.g. Bash will search for a file called
echothroughout all paths stored in the path variable. - If I’m not sure which
echoI am running, then I can usewhich echocommand to find out what program I am running.
- e.g. Bash will search for a file called
- Things that are set when we start the terminal.
Navigating file systems + using programs
- UNIX systems use
/as the root directory. Windows doesn’t. - Use
pwdto print my working directory. - Use
cdto change my current working directory.- We can use relative paths to navigate around system
cd /- Absolute path for root directorycd ...cd ./home,cd ../../../../../- Use
cd ~to go to home directory (i.e./home/username/). - Use
cd -to go back to the directory you were at.
- Use
lsto see what files and directories are in the current working directory. - Use
program_name --helpto print out what flags and arguments we can use for the program.[]parameters are optional parameters...means multiple of them.
man program_namewill show the program manualmvwill move filescpwill copy filesrmwill remove filesrm -rwill remove file/directory in recursive mannerrmdirwill remove an empty directory
Permissions
- Use
ls -lto view files and directories in long listing format.- The first few letters tell us about many things
- If the first letter is
d, it’s a directory. Otherwise it’s a file. - The rest of the letters mean the permissions granted for each user groups:
- The owner of the file
- The group that owns this file
- Anyone else
r- read permissionw- write permissionx- execute permission-- Don’t have permission
Streams
- Every program has 2 primary streams by default
- Input stream
- Output stream
<or>will rewire the streams<rewire the input of the program to be the contents of a file>rewire the output of the program into a file.- e.g.
echo hello > hello.txtwill save ‘hello’ to hello.txt. Nothing will print, but the content of hello.txt will be hello. We can check this bycat hello.txt.catwill print the contents of a file. - We can also use
cat < hello.txt - We can copy contents of a file without using
cp, by doingcat < hello.txt > hello2.txt. This will print the contents of ‘hello.txt’ and output it to ‘hello2.txt’.
>>will append the output.cat < hello.txt > hello2.txt, thencat < hello.txt >> hello2.txtwill printhello\n hello.
- We can use
|to make more sophisticated streams.|will take the output of the left side’s program, to be the input of the right side’s program.- e.g.
ls -l / | tail -n1.ls -l /will print the files in root directory in long format.tail -n1will read data and output the last line. Overall, this code will output the last line of thels -l /. - e.g.
curl --head --silent google.com | grep --ignore-case content-length | cut --delimiter=' ' -f2. The first part will grab the information of ‘google.com’. The second part usesgrepcommand to extract the information with the header ‘content-length’. The third part then cuts the string by a space, and then outputs the second field (which is the value of the content-length of google.com).
Root user mode
- Use
sudoto activate superuser mode.- ‘do as superuser’.
- You don’t want to run as superuse all the time. Often critical failures mas superuse may lead to complete system failure.
- Use
cd /sys- These files are not actual files in your system.
- These are various kernels parameters (i.e. the core of your computer)
- e.g.
cd /sys/backlight/intel_backlight/,cat brightnessto show the current brightness of the screen. We can usesudo suto activate root user mode, and then usesudo echo 500 > brightnessto modify the brightness.- When running as a superuser (i.e. root user), we will see
#on the terminal.- We use
exitto exit the root user mode.
- We use
- When not running as a super user, we see
$on the terminal.
- When running as a superuser (i.e. root user), we will see
- Running
echo 1000 | sudo tee brightnesswill work without being a root user. Theteecommand will write the input data to a file, but also will print the data at the same time.
How to open a file
- Use
xdg-opento open a file in Linux. This will use an appropriate program to open a file.