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.
These commands are for unix-based systems such as Linux and Mac OS X. For windows download git bash from https://gitforwindows.org/ and install it.
First open terminal application in Linux and Mac OS X or git bash in windows then follow next sections.
Type ls in terminal and press enter.
2014 2015 hardware.txt
- 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.
A filesystem organizes a computer’s files and directories into a tree structure:
- The first directory in the filesystem is the root directory. It is the parent of all other directories and files in the filesystem.
- Each parent directory can contain more child directories and files. Here blog/ is the parent of 2014/, 2015/, and hardware.txt.
- 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.
At any point, you can reference the filesystem for this lesson here.
pwd stands for “print working directory”. It outputs the name of the directory you are currently in, called the working directory.
Together with ls, the pwd command is useful to show where you are in the filesystem.
|$ cd 2015|
- 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.
- 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.
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 jan/memory|
To move up one directory, use cd … Here, cd .. navigates up from jan/memory/ to jan/.
|$ cd ..|
Make sure you are in the directory /home/ccuser/workspace/blog/2015/jan by using the command pwd.
|$ 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.
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.
Congratulations! You’ve learned five commands commonly used to navigate the filesystem from the command line. What can we generalize so far?
- 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