In this session, we will get familiar with shell commands with pop and rock music!
- Clone this repo
git clone https://github.com/thoughtworks-jumpstart/learn-shell.git - get into the project directory (cd):
cd learn-shell - Complete the tasks below sequentially
- Find out the absolute path to where you currently are
pwd - List the contents of your current directory
ls - List the contents of your current directory with some options:
ls -landls -la - Navigate to the
popdirectory:cd pop - Navigate back to the parent directory
cd .. - Navigate to the
adeledirectory:cd pop/adele - Navigate back:
cd ../..
- Print the lyrics to adele's "hello.txt"
cat ./pop/adele/hello.txtorcat pop/adele/hello.txt(the . is optional) - Print the lyrics to adele's "someone-like-you.txt"
cat pop/adele/someone-like-you.txt - Replace
catwithless(pressqwhen you want to exit)
- Renaming files/directories
mv ./pop/adele/SET-FIRE-TO-THE-RAIN.txt ./pop/adele/set-fire-to-the-rain.txtmv pop old-pop
- Move
set-fire-to-the-rain.txtto the correct folder (pop/adele)mv my_source_filepath my_target_filepath- Move a file:
mv ./set-fire-to-the-rain.txt ./pop/adele/ - Move and rename simultaneously:
mv ./set-fire-to-the-rain.txt ./pop/adele/SET-FIRE-TO-THE-RAIN.txt
- Move a file:
- Create files
touchtouch my-first-file.txt - Create directories
mkdir jazz
- Delete files
rm somefileorrm file.txt another-file.js - Delete directories
rm -rf jazz
- Type
historyto see a history of commands you have typed
Pipe (|) allows us to pipe the output of one command as the input to another command
-
Example use case #1:
grephistory | grep 'touch'history | grep '.txt'
-
Example use case #2:
wc(count)- count number of lines in a file
cat ./pop/adele/hello.txt | wc -lcat ./pop/adele/someone-like-you.txt | wc -l
- count number of lines in a file
- Read manuals with the
mancommand. E.g.man mkdir,man ls
manuseslessas its default pagerhto get help (this is the only one you have to remember)qto quitfto move forward (you can also use theSPACEkey)bto move backward/patternto search for something; usenfor next search occurence andpto go back to the first occurence
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.
We can check what's in your PATH with echo $PATH
If you have scripts you want to run, you would have to cd into your directory and then run ./my-script.sh.
Alternatively, you can add a directory to your PATH which will allow you to execute my-script.sh. Let's see how to do this.
-
Temporarily add folder
scriptsto your path to make it available to the terminalexport PATH=$PATH:<absolute_path_to_folder>- Example would be
export PATH=$PATH:/Users/sgyaminmhd/jumpstart/learn-shell/scripts echo $PATHto check that it's added to the end of the $PATH- Return to your home directory using
~and runhello_world.sh - It will run the executable script!
- Close your terminal session and run
echo $PATHto notice that your $PATH is back to normal
-
Permanently add the directory to your PATH by adding it in your
.zshrcfileexport PATH=$PATH:<absolute_path_to_folder>at the top of the file- save your file
- Open a new terminal session (important). Close any old session and open a new one for the new PATH to take effect
- Return to your home directory using
~and runhello_world.sh - It will run the script!
- Close your terminal and run
echo $PATHto notice the new addition is persisted to the PATH