2 - Reading & Writing Files
What will we learn?
By the end of this lesson you will have:
Learned a little more about the BASH shell
Manipulated text files in a shell
Modified your BASH profile / preferences
Recall
The most common *nix shell is BASH (Bourne Again SHell)
There are many (many) shells available for *nix operating systems. Most are derivatives of SH or BASH.
Recently we learned how to...
see the contents of a folder with ls
move to a directory with cd (along with some shortcuts like ~ and ..)
make new folders and empty files with mkdir and touch
move or rename items with mv
delete items with rm
autocomplete with TAB
see command history with 🠩 and 🠫
Let's continue!
The Lesson
Here's a few more interesting commands:
pwd - not a password tool, this prints the current working directory
ps - print the current process status (for this terminal)
man - get the manual for a particular command
top - a program to see computer resource usage and running programs
Inside most shells you can run programs after each other (asynchrounously) or at the same time (synchronously)
To run programs one after the other no matter what we use the semi-colon ;
sudo apt update; sudo apt -y upgrade
But what if the first command fails?
cd my_directory; rm -fR *
To run programs one after the other only if the first was successful use two ampersands &&
cd my_directory && rm -fR *
To run programs at the same time use a single ampersand &
sudo apt update & vim my_script.sh
What was that vim command?
Reading & Writing Files
The basics of any OS is input and ouput (IO). As with anything, there are many tools designed to help with this in the shell.
To write to a file there are several options - too many to list - but here's the basics:
touch <filename> - create a new, empty file
echo some text or information > filename.txt the chevron redirects the output to a new location (overwriting the file)
Note: one chevron overwrites the file but two chevrons concatonates onto the end of the file
vi <filename> - a very old file editing program that was improved with vim
vim <filename> - a complex and powerful text editor that takes lots of getting used to
emacs <filename> - another extremely popular and powerful text editor (overkill, really)
nano <filename> - a tiny text editor that gets the job done (Mr. Brash's typical choice)
Most distros have nano pre-installed but if not: sudo apt install nano
To list the contents of a file (read) there are a few tools:
more - older command that loads the entire file in memory first and displays it page-by-page
less - similar to more but moves line-by-line, reads the file sequentially, and has search tools
cat - used to concatonate files but if you don't give arugments, it lists the file contents
head - print the first 10 lines of a file (configurable)
tail - print the last 10 lines of a file (configurable)
echo "$(<filename)" - the actual BASH command to print a file's contents
Most people use less or cat
Last Lesson - BASH Commands
ls → list (ls)
mkdir → make an empty directory (mkdir ICS4U)
cd → change directory (cd ICS4U)
inside here create a "Unit0" directory and then inside that a "Lesson1" directory
Things to note:
cd .. will go back one directory
cd ~ will change to your home directory
cd / will change to the "root" directory
pwd → print working directory (what directory are you currently in?)
touch myfirstscript.sh → create an empty file called myfirstscript.sh
Now create another file called "some_file.txt"
rm → remove (rm some_file.txt) to delete a file
mv → move or rename a file (mv myfirstscript.sh hello.sh)
echo → print text or data (echo Hi Mom!)