Saturday, April 9, 2016

Command Line_Lesson_Navigation

1.Your First Command
The command line is a text interface for your computer. It's a program that takes in commands, which it passes on to the computer's operating system to run.
From the command line, you can navigate through files and folders on your computer, just as you would with Finder on Mac OS or Windows Explorer on Windows. The difference is that the command line is fully text-based.
The advantage of using the command line is its power. You can run programs, write scripts to automate common tasks, and combine simple commands to handle difficult tasks - making it an important programming tool.

1.1.To access the command line, we use a terminal emulator, often just called the terminal.
In the terminal, after the $ type:
ls
and press Enter.
You should see three items print out below the command.
$ ls                                                                                              
2014  2015  hardware.txt
2.ls
  • In the terminal, first you see $. This is called a shell prompt. It appears when the terminal is ready to accept a command.
  • 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.
3.Filesystem
When using the command line, we refer to folders as directories. Files and directories on your computer are organized into a 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                                                                                            
/home/ccuser/workspace/blog  4.pwd
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.
4.1.Let's continue with more commands. In the terminal, print the working directory.
$ pwd                                                                                            
/home/ccuser/workspace/blog       4.2.List all files and directories in the working directory.
$ ls                                                                                             
2014  2015  hardware.txt     4.3.
Then type
cd 2015
Again, print the new current working directory.
List all files and directories in the working directory.
$ cd 2015                                                                                        
$ pwd                                                                                            
/home/ccuser/workspace/blog/2015                                                                 
$ ls                                                                                             
feb  jan  motherboard.txt     5.cd I
  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.
5.1.Change into the 2015/ directory again.
$ cd 2015  
5.2.Then type
cd jan/memory/
Print the working directory to see the new location.
$ cd jan/memory/                                                                               
$ pwd                                                                                          
/home/ccuser/workspace/blog/2015/jan/memory 
5.3.Then type
cd ..
Print the working directory again to see the new location.
$ cd ..                                                                                        
$ pwd                                                                                          
/home/ccuser/workspace/blog/2015/jan 6.cd II
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.
To move up one directory, use cd ... Here, cd .. navigates up from jan/memory/ to jan/.
6.1.Change the directory to the 2015/feb/ directory.
List all files and directories in the working directory
$ cd 2015/feb/                                                                                   
$ ls                                                                                             
circuit-board.txt  input-output.txt  power-supply.txt      6.2.Type
mkdir media
Again, list all files and directories in the working directory. You'll see that there is now a new directory named media/.
$ mkdir media                                                                                                               
$ ls                                                                                             
circuit-board.txt  input-output.txt  media  power-supply.txt  7.mkdir
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.
7.1.Navigate to the 2014/dec/ directory.
List all files and directories in the working directory.
$ cd 2014/dec/                                                                                 
$ ls                                                                                           
monitor.txt  mouse.txt             7.2.Then type
touch keyboard.txt
Again, list all files and directories in the working directory. You'll see that there is now a new file named keyboard.txt.
$ touch keyboard.txt                                                                           
$ ls                                                                                           
keyboard.txt  monitor.txt  mouse.txt            

8.touch
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.
9.Generalizations
  • The command line is a text interface for the computer's operating system. To access the command line, we use the terminal.
  • A filesystem organizes a computer's files and directories into a tree structure. It starts with the root directory. Each parent directory can contain more child directories and files.
  • From the command line, you can navigate through files and folders on your computer:
    • pwd outputs the name of the current working directory.
    • ls lists all files and directories in the working directory.
    • cd switches you into the directory you specify.
    • mkdir creates a new directory in the working directory.
    • touch creates a new file inside the working directory.








No comments :

Post a Comment