0% found this document useful (0 votes)
166 views

Bash Shell Latihan Di Codeacademy

The document provides instructions for setting up and using the Bash shell on Mac and Windows systems. It explains that Bash is the default shell for Mac and Linux, but must be installed on Windows via Git Bash. The ls, pwd, cd, mkdir, and touch commands are demonstrated and explained as ways to navigate directories and manage files from the command line. Examples are given to help users practice basic commands like listing, creating, and changing directories.

Uploaded by

Triandika Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views

Bash Shell Latihan Di Codeacademy

The document provides instructions for setting up and using the Bash shell on Mac and Windows systems. It explains that Bash is the default shell for Mac and Linux, but must be installed on Windows via Git Bash. The ls, pwd, cd, mkdir, and touch commands are demonstrated and explained as ways to navigate directories and manage files from the command line. Examples are given to help users practice basic commands like listing, creating, and changing directories.

Uploaded by

Triandika Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Bash shell in 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.

ls is an example of a command, a directive to the computer to perform a specific task.

Instructions

When using the command line, we refer to folders as directories. Files and directories on
your computer are organized into a filesystem.

Click Next to find out how the filesystem works.

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.

Make sure you are in the directory /home/ccuser/workspace/blog/2015 by using the


command pwd.

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/.

Make sure you are in the directory /home/ccuser/workspace/blog/2015/jan by using the


command pwd.

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.

Setting Up Your Command Line


The command line is a powerful tool used by developers to find, create, and manipulate files
and folders. This short tutorial will walk you through the steps for setting up the command
line application on your computer.

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.

Bash Setup for Mac and Windows


Mac users:

As mentioned before, Bash is the default shell on Linux and Mac OS X, so good news, you
don’t have to install anything!

To access Bash in OS X, you can use an application called Terminal.

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.

How to install Git Bash:

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.

In the terminal after the shell prompt, type

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:

 -a - lists all contents, including hidden files and directories


 -l - lists all contents of a directory in long format
 -t - order files and directories by the time they were last modified.

Let’s practice using these options below.

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.

cp biopic/ray.txt biopic/notorious.txt historical/

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/.

mv wonderwoman.txt batman.txt 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?

 Options modify the behavior of commands:


o ls -a lists all contents of a directory, including hidden files and directories
o ls -l lists all contents in long format
o ls -t orders files and directories by the time they were last modified
o Multiple options can be used together, like ls -alt
 From the command line, you can also copy, move, and remove files and directories:
o cp copies files
o mv moves and renames files
o rm removes files
o rm -r removes directories
 Wildcards are useful for selecting groups of files and directories
 Redirection
 Up until now, we have run commands in the command line and received a stream of
output in the terminal. In this lesson, we’ll focus on input and output (I/O) redirection.
 Through redirection you can direct the input and output of a command to and from
other files and programs, and chain commands together in a pipeline. Let’s try it out.

stdin, stdout, and stderr


What happens when you type this command?

$ 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:

1. standard input, abbreviated as stdin, is information inputted into the terminal


through the keyboard or input device.
2. standard output, abbreviated as stdout, is the information outputted after a process is
run.
3. standard error, abbreviated as stderr, is an error message outputted by a failed
process.

Redirection reroutes standard input, standard output, and standard error to or from a different
location.

How does redirection work?

$ echo "Hello" > hello.txt

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.

$ cat volcanoes.txt | wc | cat > islands.txt

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.

$ sort deserts.txt | uniq

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.

sort deserts.txt | uniq > uniq-deserts.txt

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 Mount 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 Arctic /home/ccuser/workspace/geography

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”.

Let’s look at the expression 's/snow/rain/':

 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.

$ sed 's/snow/rain/g' forests.txt

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?

 Redirection reroutes standard input, standard output, and standard error.


 The common redirection commands are:
o > redirects standard output of a command to a file, overwriting previous
content.
o >> redirects standard output of a command to a file, appending new content to
old content.
o < redirects standard input to a command.
o | redirects standard output of a command to another command.
 A number of other commands are powerful when combined with redirection
commands:
o sort: sorts lines of text alphabetically.
o uniq: filters duplicate, adjacent lines of text.
o grep: searches for a text pattern and outputs it.
o sed : searches for a text pattern, modifies it, and outputs it.

COPY CATATAN WORD YANG ADA DI LAPTOP

You might also like