[程式筆記-Command Line] Termianl終端機命令列常用指令

Command Line命令列 – 常用指令

Command Line命令列 – 檔案位置/清單/開啟/建立

  • pwd:列出檔案位置
  • ls : 列出目前資料夾中的所有未隱藏的檔案(files)&資料夾(folders)
  • ls -a:列出隱藏的檔案(lists all contents of a directory, including hidden files and directories)
  • ls -l :完整列出所有內容,用列表資訊的方式,檔名/擁有者權限/檔案大小(lists all contents in long format)
  • ls -t:按照時間順序列出檔案,從最新到最舊
  • ls -alt:上面三個命令列同時使用
$pwd, $ls -alt
  • mkdir:建立資料夾
  • cd:進入資料夾
  • touch:建立檔案,空的檔案
  • >:將資料放進檔案中,會全部覆蓋掉原來檔案的內容,英文意思是redireci
  • >> : 將資料貼進另一個檔案中,不會覆蓋只會補上而已,英文是append.
  • echo “hello world” > hello1.txt : 將hello word放進 hello1.txt
  • cat :開啟檔案

hello.txt是用touch產生的空檔案,所以用cat打開時裡面沒有內容。
hello1.txt是用echo “hello word”,並用 > 傳到hello1.txt儲存進去裡面,因此cat打開後可以看到hello world。

Command Line命令列 – 檔案建立/移動/複製/刪除

  • touch:建立檔案
  • cp:複製檔案 ($cp orign.txt duplicate.txt)
  • mv:移動檔案,如果在同一個目錄夾,就是重新命名檔案
  • rm:刪除檔案 (不能亂用,無法復原)
  • rm -rf : 刪除整個資料夾(不能亂用,無法復原)

Copy the orignfile.txt to duplciate.txt file
$cp orignfile.txt duplciate.txt
Copy the orignfile.txt to anotherfolder/duplciate.txt
$cp orignfile.txt anotherfolder/duplciate.txt

Move the orignfile.txt to anotherfolder/
$mv orignfile.txt ../anotherfolder/
Rename orignfile.txt to newname.txt
$mv orignfile.txt newname.txt

Delete the oldfile.txt file
$rm -r oldfile.txt
Delete the oldfile/ directory
$rm -rf oldfile

$cp 複製檔案,也可以指定複製特定位置

$mv 可以用來搬移檔案,如果在相同目錄夾可以變成修改檔名。
$rm 是REMOVE刪除檔案,$rm -rf 是刪除資料夾

$mv 可以用來搬移檔案,如果在相同目錄夾可以變成修改檔名。
$rm 是REMOVE刪除檔案,$rm -rf 是刪除資料夾

Command Line命令列 – uniq/sort/grep/sed

Command Line命令列 – Bash Profile

Bash Profile
You created a file in nano called ~/.bash_profile and added a greeting. How does this work?

$ nano ~/.bash_profile
~/.bash_profile is the name of file used to store environment settings. It is commonly called the “bash profile”. When a session starts, it will load the contents of the bash profile before executing commands.

The ~ represents the user’s home directory.
The . indicates a hidden file.
The name ~/.bash_profile is important, since this is how the command line recognizes the bash profile.
The command nano ~/.bash_profile opens up ~/.bash_profile in nano.
The text echo “Welcome, Jane Doe” creates a greeting in the bash profile, which is saved. It tells the command line to echo the string “Welcome, Jane Doe” when a terminal session begins.
The command source ~/.bash_profile activates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.

Command Line命令列 – Aliases I

Aliases I
What happens when you store this alias in ~/.bash_profile?

alias pd=”pwd”
The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.

Here alias pd=”pwd” creates the alias pd for the pwd command, which is then saved in the bash profile. Each time you enter pd, the output will be the same as the pwd command.
The command source ~/.bash_profile makes the alias pd available in the current session.
Each time we open up the terminal, we can use the pd alias.

Command Line命令列 – Environment Variables

Environment Variables
What happens when you store this in ~/.bash_profile?

export USER=”Jane Doe”
environment variables are variables that can be used across commands and programs and hold information about the environment.

The line USER=”Jane Doe” sets the environment variable USER to a name “Jane Doe”. Usually the USER variable is set to the name of the computer’s owner.
The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
At the command line, the command echo $USER returns the value of the variable. Note that $ is always used when returning a variable’s value. Here, the command echo $USER returns the name set for the variable.

Content Protection by DMCA.com