Bash Shell Latihan Di Codeacademy
Bash Shell Latihan Di Codeacademy
Command
- ls = list
1. In the terminal, first you see $. This is called a shell prompt. It appears when the
terminal is ready to accept a command.
2. When you type ls, the command line looks at the folder you are in, and then “lists”
the files and folders inside it. The directories 2014, 2015, and the file hardware.txt
are the contents of the current directory.
Instructions
When using the command line, we refer to folders as directories. Files and directories on
your computer are organized into a filesystem.
Filesystem
A filesystem organizes a computer’s files and directories into a tree structure:
1. The first directory in the filesystem is the root directory. It is the parent of all other
directories and files in the filesystem.
2. Each parent directory can contain more child directories and files. Here blog/ is the
parent of 2014/, 2015/, and hardware.txt.
3. Each directory can contain more files and child directories. The parent-child
relationship continues as long as directories and files are nested.
You’re probably already familiar with this tree structure - Mac Finder and Windows Explorer
represent the filesystem as trees as well.
pwd
$ pwd
/home/ccuser/workspace/blog
pwd stands for “print working directory”. It outputs the name of the directory you are
currently in, called the working directory.
Here the working directory is blog/. In Codecademy courses, your working directory is
usually inside the home/ccuser/workspace/ directory.
Together with ls, the pwd command is useful to show where you are in the filesystem.
cd I
$ cd 2015
1. cd stands for “change directory”. Just as you would click on a folder in Windows
Explorer or Finder, cd switches you into the directory you specify. In other words, cd
changes the working directory.
2. The directory we change into is 2015. When a file, directory or program is passed into
a command, it is called an argument. Here the 2015 directory is an argument for the
cd command.
The cd command takes a directory name as an argument, and switches into that directory.
cd II
$ cd jan/memory
To navigate directly to a directory, use cd with the directory’s path as an argument. Here, cd
jan/memory/ command navigates directly to the jan/memory directory.
$ cd ..
To move up one directory, use cd ... Here, cd .. navigates up from jan/memory/ to jan/.
mkdir
$ mkdir media
The mkdir command stands for “make directory”. It takes in a directory name as an
argument, and then creates a new directory in the current working directory.
Here we used mkdir to create a new directory named media/ inside the feb/ directory.
touch
touch keyboard.txt
The touch command creates a new file inside the working directory. It takes in a filename as
an argument, and then creates an empty file in the current working directory.
Here we used touch to create a new file named keyboard.txt inside the 2014/dec/ directory.
Command Line Interfaces (CLIs) come in many forms. The CLI we’ll use is called Bash.
What is Bash?
Bash, or the Bourne-Again SHell, is a CLI that was created over twenty-seven years ago by
Brian Fox as a free software replacement for the Bourne Shell. A shell is a specific kind of
CLI. Bash is “open source” which means that anyone can read the code and suggest changes.
Since its beginning, it has been supported by a large community of engineers who have
worked to make it an incredible tool. Bash is the default shell for Linux and Mac. For these
reasons, Bash is the most used and widely distributed shell. If you want to learn more about
Bash, this Wikipedia article is a good place to start.
As mentioned before, Bash is the default shell on Linux and Mac OS X, so good news, you
don’t have to install anything!
1. First open the Applications folder, then open the Utilities folder.
2. Once you’re in the Utilities folder you will see the application Terminal. Open the
Terminal application and you’re ready to go!
3. For ease of access later, you can keep Terminal in your Dock. Simply right click (alt-click)
the Terminal icon in your dock, then select “Options”, then “Keep In Dock.”
Continue to the Try it Out! section below for some simple first steps with your new tool.
Windows users:
Windows has a different CLI, called Command Prompt. While this has many of the same
features as Bash, Bash is much more popular. Because of the strength of the open source
community and the tools they provide, mastering Bash is a better investment than mastering
Command Prompt.
To use Bash on a Windows computer, we will download and install a program called Git
Bash. Git Bash allows us to easily access Bash as well as another tool we’ll be using later
called Git, inside the Windows environment.
1. Navigate to the Git Bash installation page and click the Download button.
2. Once Git Bash is downloaded, run the downloaded .exe file and allow the application to
make changes to your PC. You will get a prompt that says “Do you want to allow this app to
make changes to your device?” Click Yes.
3. To keep things simple, we will use the default settings for everything in this installation, so
all you need to do now is keep clicking Next, and finally Finish.
4. Open the Start menu by clicking on the Windows icon and typing “Git Bash” into the
search bar. The icon for Git Bash and the words “Git Bash Desktop App” will appear. Click
on the icon or the words “Git Bash Desktop App” to open Git Bash.
5. A new window will open. This is the Git Bash CLI where we will run Bash commands.
Whenever a new window of the Git Bash app is opened, you will always be placed in the
same directory, your home directory.
The home directory is represented by the tilde sign, ~, in the CLI after MINGW64. The tilde is
another way to say /c/Users/username in Git Bash or C:\home\Users\username in
Windows’ Command Prompt.
The absolute path of your current working directory, how you got from the root directory to
the directory you are currently in, will always be noted at the top of the window:
Git Bash works by giving you a CLI that acts like a Bash CLI. That means you can now work
with your files and folders using Bash commands instead of Windows commands.
Congratulations, you now have Bash installed on your computer, ready to use!
Try it out!
Now that you have your Command Line Interface open on your desktop, you are ready to use
it. Go ahead and try some of the commands on your personal computer. Here are some good
commands for practice:
1. ls to list the contents of the current directory It may look something like this:
2. $ ls
3. Applications Pictures
4. Codecademy Public
5. Desktop Downloads
Documents Library
6. mkdir test to make a new directory named test Now, when you type ls you should see a
folder called test:
7. $ ls
8. Applications Pictures
9. Codecademy Public
10. Desktop Downloads
11. Documents Library
test
12. cd test to navigate into the new directory. You won’t see an output when you do this.
13. echo "Hello Command Line" >> hello_cli.txt to create a new file named
hello_cli.txt and add Hello Command Line to that file. When you type ls, you should see
the following:
14. $ ls
hello_cli.txt
15. cat hello_cli.txt to print the contents of the hello_cli.txt file to the terminal. You
should see something like:
16. $ cat hello_cli.txt
Hello Command Line
Good job! You’re ready to explore the world of the Command Line Interface on your own
computer.
ls, revisited
So far we’ve used the command line to navigate the filesystem.
We can do more with the command line to view directories and files. We can also use the
command line to copy, move, and remove files and directories. Let’s see how to do this.
You can reference the filesystem for this lesson here. It is highly recommended for this
lesson.
Instructions
1.
ls
2.
Then type
ls -a
Do you see the differences between the outputs of both commands? Click Next to learn how
this works.
ls -a
$ ls -a
. .. .preferences action drama comedy genres.txt
1. The ls command lists all files and directories in the working directory.
2. The -a modifies the behavior of the ls command to also list the files and directories
starting with a dot (.). Files started with a dot are hidden, and don’t appear when
using ls alone.
The -a is called an option. Options modify the behavior of commands. Here we used ls -a
to display the contents of the working directory in more detail.
In addition to -a, the ls command has several more options. Here are three common options:
ls -l
$ ls -l
drwxr-xr-x 5 cc eng 4096 Jun 24 16:51 action
drwxr-xr-x 4 cc eng 4096 Jun 24 16:51 comedy
drwxr-xr-x 6 cc eng 4096 Jun 24 16:51 drama
-rw-r--r-- 1 cc eng 0 Jun 24 16:51 genres.txt
The -l option lists files and directories as a table. Here there are four rows, with seven
columns separated by spaces. Here’s what each column means:
1. Access rights. These are actions that are permitted on a file or directory.
2. Number of hard links. This number counts the number of child directories and files.
This number includes the parent directory link (..) and current directory link (.).
3. The username of the file’s owner. Here the username is cc.
4. The name of the group that owns the file. Here the group name is eng.
5. The size of the file in bytes.
6. The date & time that the file was last modified.
7. The name of the file or directory.
ls -alt
$ ls -alt
drwxr-xr-x 4 cc eng 4096 Jun 29 12:22 .
-rw-r--r-- 1 cc eng 0 Jun 29 12:22 .gitignore
drwxr-xr-x 5 cc eng 4096 Jun 30 14:20 ..
drwxr-xr-x 2 cc eng 4096 Jun 29 12:22 satire
drwxr-xr-x 2 cc eng 4096 Jun 29 12:22 slapstick
-rw-r--r-- 1 cc eng 14 Jun 29 12:22 the-office.txt
The -t option orders files and directories by the time they were last modified.
In addition to using each option separately, like ls -a or ls -l, multiple options can be used
together, like ls -alt.
Here, ls -alt lists all contents, including hidden files and directories, in long format,
ordered by the date and time they were last modified.
cp I
cp frida.txt lincoln.txt
The cp command copies files or directories. Here, we copy the contents of frida.txt into
lincoln.txt.
cp II
cp biopic/cleopatra.txt historical/
To copy a file into a directory, use cp with the source file as the first argument and the
destination directory as the second argument. Here, we copy the file biopic/cleopatra.txt and
place it in the historical/ directory.
To copy multiple files into a directory, use cp with a list of source files as the first arguments,
and the destination directory as the last argument. Here, we copy the files biopic/ray.txt and
biopic/notorious.txt into the historical/ directory.
Wildcards
cp * satire/
In addition to using filenames as arguments, we can use special characters like * to select
groups of files. These special characters are called wildcards. The * selects all files in the
working directory, so here we use cp to copy all files into the satire/ directory.
cp m*.txt scifi/
Here, m*.txt selects all files in the working directory starting with “m” and ending with
“.txt”, and copies them to scifi/.
mv
The mv command moves files. It’s similar to cp in its usage.
mv superman.txt superhero/
To move a file into a directory, use mv with the source file as the first argument and the
destination directory as the second argument. Here we move superman.txt into superhero/.
To move multiple files into a directory, use mv with a list of source files as the first
arguments, and the destination directory as the last argument. Here, we move
wonderwoman.txt and batman.txt into superhero/.
mv batman.txt spiderman.txt
To rename a file, use mv with the old file as the first argument and the new file as the second
argument. By moving batman.txt into spiderman.txt, we rename the file as spiderman.txt.
rm
rm waterboy.txt
The rm command deletes files and directories. Here we remove the file waterboy.txt from
the filesystem.
rm -r comedy
The -r is an option that modifies the behavior of the rm command. The -r stands for
“recursive,” and it’s used to delete a directory and all of its child directories.
Be careful when you use rm! It deletes files and directories permanently. There isn’t an
undelete command, so once you delete a file or directory with rm, it’s gone.
Generalizations
Congratulations! You learned how to use the command line to view and manipulate the
filesystem. What can we generalize so far?
$ echo "Hello"
Hello
The echo command accepts the string “Hello” as standard input, and echoes the string
“Hello” back to the terminal as standard output.
Let’s learn more about standard input, standard output, and standard error:
Redirection reroutes standard input, standard output, and standard error to or from a different
location.
The > command redirects the standard output to a file. Here, "Hello" is entered as the
standard input. The standard output "Hello" is redirected by > to the file hello.txt.
$ cat hello.txt
The cat command outputs the contents of a file to the terminal. When you type cat
hello.txt, the contents of hello.txt are displayed.
>
$ cat oceans.txt > continents.txt
>takes the standard output of the command on the left, and redirects it to the file on the right.
Here the standard output of cat oceans.txt is redirected to continents.txt.
Note that > overwrites all original content in continents.txt. When you view the output data
by typing cat on continents.txt, you will see only the contents of oceans.txt.
>>
$ cat glaciers.txt >> rivers.txt
>> takes the standard output of the command on the left and appends (adds) it to the file on
the right. You can view the output data of the file with cat and the filename.
Here, the the output data of rivers.txt will contain the original contents of rivers.txt with the
content of glaciers.txt appended to it.
< takes the standard input from the file on the right and inputs it into the program on the left. Here,
lakes.txt is the standard input for the cat command. The standard output appears in the terminal.
|
$ cat volcanoes.txt | wc
| is a “pipe”. The | takes the standard output of the command on the left, and pipes it as
standard input to the command on the right. You can think of this as “command to command”
redirection.
Here the output of cat volcanoes.txt is the standard input of wc. in turn, the wc command
outputs the number of lines, words, and characters in volcanoes.txt, respectively.
Multiple |s can be chained together. Here the standard output of cat volcanoes.txt is
“piped” to the wc command. The standard output of wc is then “piped” to cat. Finally, the
standard output of cat is redirected to islands.txt.
You can view the output data of this chain by typing cat islands.txt.
sort
$ sort lakes.txt
sort takes the standard input and orders it alphabetically for the standard output. Here, the
lakes in sort lakes.txt are listed in alphabetical order.
$ cat lakes.txt | sort > sorted-lakes.txt
Here, the command takes the standard output from cat lakes.txt and “pipes” it to sort.
The standard output of sort is redirected to sorted-lakes.txt.
You can view the output data by typing cat on the file sorted-lakes.txt.
uniq
$ uniq deserts.txt
uniq stands for “unique” and filters out adjacent, duplicate lines in a file. Here uniq
deserts.txt filters out duplicates of “Sahara Desert”, because the duplicate of ‘Sahara
Desert’ directly follows the previous instance. The “Kalahari Desert” duplicates are not
adjacent, and thus remain.
A more effective way to call uniq is to call sort to alphabetize a file, and “pipe” the standard
output to uniq. Here by piping sort deserts.txt to uniq, all duplicate lines are
alphabetized (and thereby made adjacent) and filtered out.
Here we simply send filtered contents to uniq-deserts.txt, which you can view with the cat
command.
grep I
$ grep Mount mountains.txt
grep stands for “global regular expression print”. It searches files for lines that match a
pattern and returns the results. It is also case sensitive. Here, grep searches for “Mount” in
mountains.txt.
grep -i enables the command to be case insensitive. Here, grep searches for capital or
lowercase strings that match Mount in mountains.txt.
The above commands are a great way to get started with grep. If you are familiar with
regular expressions, you can use regular expressions to search for patterns in files.
grep II
$ grep -R Arctic /home/ccuser/workspace/geography
grep -R searches all files in a directory and outputs filenames and lines containing matched
results. -R stands for “recursive”. Here grep -R searches the
/home/ccuser/workspace/geography directory for the string “Arctic” and outputs filenames
and lines with matched results.
grep -Rl searches all files in a directory and outputs only filenames with matched results. -R
stands for “recursive” and l stands for “files with matches”. Here grep -Rl searches the
/home/ccuser/workspace/geography directory for the string “Arctic” and outputs filenames
with matched results.
sed
$ sed 's/snow/rain/' forests.txt
sed stands for “stream editor”. It accepts standard input and modifies it based on an
expression, before displaying it as output data. It is similar to “find and replace”.
s: stands for “substitution”. it is always used when using sed for substitution.
snow: the search string, the text to find.
rain: the replacement string, the text to add in place.
In this case, sed searches forests.txt for the word “snow” and replaces it with “rain.”
Importantly, the above command will only replace the first instance of “snow” on a line.
The above command uses the g expression, meaning “global”. Here sed searches forests.txt
for the word “snow” and replaces it with “rain”, globally. All instances of “snow” on a line
will be turned to “rain”.
Generalizations
Congratulations! You learned how to use the command line to redirect standard input and
standard output. What can we generalize so far?