bash简单命令使用笔记

@[toc]

食用说明:

本篇笔记包含1. MacOS terminal的简单介绍;2. bash这一shell的简单介绍;3. 文件路径;4. bash简单命令的使用笔记和个人在使用中踩过的坑。

为方便自己查看而写的简短记录,目前中英混杂。日后如果常用,可能会进一步排版。

===============================================

The Terminal application is a command-line Interface.

By default, the Terminal in Ubuntu and Mac OS X runs the so-called bash shell, which supports a set of commands and utilities; and has its own programming language for writing shell scripts.

 

bash (Bourne* Again shell) 是绝大多数 Linux 、macOS 默认的 shell 程序。并且 Shell Script 都大致相同。当您学会一种 Shell 后,其它的 Shell 都能够很快上手,因此您不必在学习哪种 Shell 的选择上耗费太多的时间。

bash 的一个优点是, 在可能性唯一的情况下, 可以按 tab 补全命令.

* Bourne shell (sh). 是 UNIX 最初使用的 shell

 

shell是你(用户)和Linux内核之间的接口程序。你在提示符下输入的每个命令都由shell先解释然后传给Linux内核。shell is a command-language interpreter.

 

输出您的操作系统支持的 Shell 类型,可在终端中输入命令:

$ cat /etc/shells

 

// 但是发现macOS和Linux上bash的指令还有略微差异。例如,ll这个命令在我的Mac上会报错,还比如touch -d。

// TODO: 找出原因。

 

===============================================

 

1. Terminal

A Terminal displays a command prompt ending with a "$" sign, in the form of:

In Mac OS X: "ComputerName:CurrentDirectory Username$"

  • In Linux/Ubuntu: "Username@ComputerName:CurrentDirectory$"

 

===============================================

 

2. File System

Files are organized in directories (aka folders).

The directories are organized in a hierarchical tree structure, starting from the root directory.

 

Root Directory(/)

A file is identified via the directories and filename,

e.g., "/usr/lib/jvm/jdk1.7.0_07/bin/javac".

The leading "/" (forward slash) denotes the root directory. The sub-directories are also separated by a "/".

There is only one root directory for the entire Unix's file system. Hard drives are mounted somewhere under the root directory.

Notes: Windows use "\" (back slash) as the directory separator, and may contain multiple root directories - one for each drive (e.g., c:\, d:\).

 

Home Directory(~)

Unix is a multi-user operating system (although most of you, in particular the Mac users, use it as a single-user personal computer). Each user on the system is allocated a directory for storing his files, known as home directory.

"~/Downloads/jdk/" is the same as "/Users/<yourname>/Downloads/jdk/" in Mac OS.

 

Pathname and Filename

To reference a file, you need to provide the pathname (directory and sub-directories names) and the filename.

 

  1. The pathname can be specified in two ways:
  2. Absolute Pathname: e.g., "/usr/lib/", "~/Downloads/jdk/"
  3. Relative Pathname: A relative path is relative to the so-called current working directory. A relative path does NOT begin with "/" or "~". For example, if the current working directory is "/usr/lib/jvm/", then the relative pathname "jdk1.7.0_07/bin/" refers to "/usr/lib/jvm/jdk1.7.0_07/bin/".

 

* Unix system is case-sensitive; most MacOSX is insensitive.

 

===============================================

3. Useful Command

ls (LiSt Directory's Contents)

ls  # list contents of (current) working directory - in short form

ls -l  # list - in long form *[1]

ls -a # list all, including hidden files begins with '.'

ls [OPTION]... [FILE]... # 显示某文件的信息.

* 这里须将 [OPTION] 置于 [FILE] 前。否则[OPTION] 会被识别作 [FILE]。

* 如果输入多个 [FILE] (以空格区分),则将分别显示多个file的信息。

 

Wildcard *

You can list selected files using wildcard *, which matches 0 or more (any) characters.

ls -l *.py  # List files ending with ".py" - in long format

ls -ld my*  # List files and directories beginning with "my",

# including those named with "my"

ls my*  # List files in each directory beginning with "my"

 

 

===============================================

 

mkdir (make directory) & rmdir (remove directory) &

cp (CoPy) & mv (MoVe) & rm (ReMove)

& touch & find

 

mkdir (make directory) & rmdir (remove directory)

mkdir [OPTION]... DIRECTORY...

# rmdir can only delete empty directory (no file / sub-directory in it)

rmdir [OPTION]... DIRECTORY...

 

cp (CoPy) & mv (MoVe) & rm (ReMove)

# copy SOURCE to DEST, change its name as DEST

cp [OPTION]... [-T] SOURCE DEST

# copy (potentially several) SOURCE(s) to a given DIRECTORY

  cp [OPTION]... SOURCE... DIRECTORY 

DEST is something like "/dir1/filename"; DIRECTORY /dir1/".

# mv can also be used to change file/directory name

mv [OPTION]... [-T] SOURCE DEST

mv [OPTION]... SOURCE... DIRECTORY
# 默认情况下,rm命令不删除文件夹。

rm [OPTION]... FILE...

# 使用 -r 选项来删除文件夹及其子目录的内容。

# -r, -R, --recursive 删除目录及子目录所有内容

rm -r FILE...

 

# note: 别把mv和rm敲混了。执行rm文件就没了。

 

OPTION:

-v, --verbose   解释做了的动作

special cases:

mkdir 'hello bash' # foldername with whitespaces

mkdir —- -v # foldername begining with '-'

 

touch & find

# touch creates a new file / change last modified date of an existing file

touch [-t [[CC]YY]MMDDhhmm[.SS]] file
# show all files & directories stored under PATH

# I mean, if you enter find ~, you may get a long list,

# including /Users/admin/c++/Catalan.cpp, /Users/admin/c++, those kind.

find PATH

# find a specific file. -name is case-sensitive; while -iname is not.

find PATH -name FILENAME

 

===============================================

 

cat

(/ tac “最后一行在上,其次倒二,…”地输出)

连接文件并打印到标准输出设备上,经常用来显示文件的内容。

当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。

more

more FILE

 

head & tail

head -n +N FILE # take first N rows

head -n -N FILE # take all except last N rows

tail -n -N FILE # take last N rows

tail -n +N FILE # take all except first N rows

 

===============================================

 

 

 

===============================================

附录

[1] 关于 long form 中各列的说明,从第一列开始:

1. 文件类型及权限。每个文件均以类型开头,接着指定访问权限:

常规文件(-)

特殊档案(c)

目录(d)

链接(l)

2. 存储块的数量

3. 文件的所有者/具有管理权限的超级用户

4. 所有者、超级用户组

5. 文件大小

6. 文件的最后修改时间

7. 文件/目录的名称

 

REF:

0 https://www.cnblogs.com/hihtml5/p/9272751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值