5 Working with the Bash Shell.
5.1 Ryans Tutorials
The following tutorial provides an intro to using Bash. The title of this tutorial is “Linux Tutorial” as Bash is commonly used on the Linux operating system. However, this tutorial also applies to using Bash on the Mac and on Windows.
https://ryanstutorials.net/linuxtutorial/
Most of the information that we covered before the midterm can be found in the first 8 sections of this tutorial. Links are below along with some of my own comments. Please use the notes on this website to study for the midterm.
The Command Line - What is it, how does it work and how do I get to one.
Basic Navigation - An introduction to the Linux directory system and how to get around it.
Manual Pages - Learn how to make the most of the Linux commands you are learning.
You should know how to read the contents of a “man page”.
Note that the “man” command isn’t installed by default on posit.cloud nor on Mac or Windows Bash environments. However, you can search online for any man page, (e.g. google for man ls).
Alternatively , you can access the man pages here: https://man7.org/linux/man-pages/dir_section_1.html
File Manipulation - How to make, remove, rename, copy and move files and directories.
You are NOT responsible for the “vi” editor. “vi” is a text editor that is commonly found in most Bash environments. While it is useful to know, vi can be a bit confusing for new users. Vi Text Editor - Discover a powerful Linux based text editor.
Wildcards - Also referred to as globbing, this is a means to refer to several files in one go.
WILF ONLY (we learned the basic concepts about how to understand the first column of info that is output by the “ls -l” command. You are NOT responsible for the chmod command that is described here.)
Permissions - Learn to identify and change the permissions of files and directories and what the consequences of these are.
5.2 Download files for use in these lessons
A “zip file” is a single file that contains a condensed version of one or more files or folders. Click on this link to download a “zip file” named myFiles.zip that contains files that we will be working with to demonstrate many of the commands available with the Bash CLI.
To “extract” (or unzip) all of the files that are in this zip file, run the following command:
unzip myFiles.zip
5.3 Working with ZIP files in Bash
A “zip” file is a single file into which many different individual files have been placed. For example, if you want to mail several different files to someone, you can place all of those files into a single “zip” file and then email the single zip file. The recipient will need to “unzip” the file to retrieve the individual files. The single zip file is often smaller in size than the collection of individual files (i.e. the “zip” file is “compressed”).
On Mac’s the contents of a zip file is automatically extracted into a folder when you try to access the zip file by clicking on it. The Mac creates a new folder to place the contents of the zip file. On Windows, this doesn’t happen automatically. Rather you must “uncompress” the zip file. You can do so by right clicking on the file name and choosing “uncompress”. On posit.cloud, it seems like zip files are automatically unzipped into a folder when a zip file is uploaded.
The following commands can be used from Bash to zip or unzip a set of files. There are different types of zip files - however, the different types often use the same .zip filename extension. The zip/unzip commands work with one type of zip file and gzip/gunzip commands work with another type of zip file. To unzip a file you can try the unzip command and if that doesn’t work try the gunzip command.
- zip -r FILENAME.zip SOME_FOLDER - creates a new zip file
- unzip FILENAME.zip - extracts the contents of a zip file
- gzip / gunzip - zip/unzip Gnu types of zip files
5.3.2 Important Bash commands that everyone should know
The following gives a brief description of how many commands work. In the descriptions below, any part of the command that appears in [square brackets] is optional.
pwd - “Print the Working Directory” (ie display the “path” for the current working directory (CWD)
cd SOME_DIRECTORY - set the “working directory” to the specified directory
cd - set the “working directory” to your “home directory”
cd .. - set the “working directory” to the “parent” of the current working directory
cd ../.. - set the “working directory” to the parent of the parent of the current working directory
cd ../../.. (etc.) - set the “working directory” to 3 levels up in the directory hierarchy)
mv SOME_FILE SOME_DIRECTORY - move SOME_FILE to SOME_DIRECTORY
mv SOME_DIRECOTRY ANOTHER_DIRECTORY - move SOME_FILE to ANOTHER_DIRECTORY
ls [SOME_DIRECTORY] - list the files in the current directory or the specified [DIRECTORY]
ls -l [SOME_DIRECTORY] - show a “long” listing (i.e. more info about each file/folder)
ls -r [SOME_DIRECTORY] - reverse the order that the files are listed
ls -lt [SOME_DIRECOTRY] - show the contents in time order (with a long listing format)
ls -l -t [SOME_DIRECOTRY] - SAME THING - show the contents in time order (with a long listing format)
ls -a [SOME_DIRECTORY] - show all files (including files that start with a period ( . )
ls -R [SOME_DIRECTORY] - show the files that are in all folders below the CWD (or the specified directory)
ls SOME_FILES - list the names of the files
cat SOME_FILES - display the contents of the files (only for “text” files)
file SOME_FILE - determine the type of file (e.g. ASCII, Excel, etc.)
tree . - show directory hierarchy (tree is not installed on many systems)
5.4 creating/removing directories (aka folders)
mkdir SOME_DIRECTORY - “make” the specified directory
rmdir SOME_DIRECORY - “remove” directory (must be empty, NOTE also see rm command)
5.5 Removing files
BE CAREFUL WITH THE FOLLOWING COMMANDS!!!
THERE IS NO UNDO
ONCE YOU’VE REMOVED A FILE/FOLDER YOU CANNOT GET IT BACK!
rm SOME_FILES - remvove the files (THERE IS NO WAY TO GET THEM BACK)
if you are brave, you can research how to use rm to remove entire directories (that aren’t empty)
5.6 creating files using > and >>
echo SOME_MESSAGE - display the message
echo SOME_MESSAGE > SOME_FILE - put the message in the file (erase current contents of file)
echo SOME_MESSAGE >> SOME_FILE - append the message to the file
5.7 Working with BASH variables
env - display the variables that currently exist
VARIABLE=VALUE - set the value of VARIABLE to VALUE (do NOT type any spaces next to the = sign)
echo $VARIABLE - show the value of a specific variable
For an example see see Section 4.2 for info about the PS1 variable
5.8 Material that is NOT on the midterm
5.8.1 Working with “tar” files
“tar” files are similar to zip files but tar files are not “compressed”. “tar” files are popular mostly on Linux computers while “zip” files are popular on all computer platforms.
- tar - create or extract a tar archive
5.8.2 SOME MORE COMMANDS - search online for info about these
wc - count words, lines, symbols in a file
head - show first few lines of a file
tail - show last few lines of a file
sort
uniq
cut
paste
curl - download files from the Internet
touch SOME_FILE - update the timestamp of the file OR create the file if it didn’t exist
less (or “more” depending on your system) - display the contents of a file one page at a time
grep - search for a specific string of text in a file or multiple files
find - search for files and directories in a file system
chmod - change the permissions of a file or directory
chown - change the ownership of a file or directory
history - display a list of previously executed commands
5.9 Launch VSCode from Bash (on Windows)
VSCode is a popular text editor. To launch VSCode directly from Bash on Windows, you can use the following commands directly from Bash. While VSCode is also popular on Mac, unfortunately the following commands do now work on Macs.
code - launch the VSCode text editor program (if you’ve installed it)
code [SOME_FILE] - launch VSCode and open SOME_FILE
code [SOME_DIRECTORY] - launch VSCode with the “explorer” window set to SOME_DIRECTORY.
code . (there is a period there) - launch VSCode with the “explorer” window set to the current working directory.