定位
pwd show current location
ls list all files in the current level
ls -R shows every file and directory in the current level, then everything in each sub-directory
ls -F -R prints a / after the name of every directory and a * after the name of every runnable program.
cd + new path move to the new path
.. the directory above the one you're currently in
If you are in /home/repl/seasonal, then cd .. moves you up to /home/repl
. the current directory
cd . has no effect to the current location
~ path of the user's home directory
ls ~ will list the contents of your home directory
cd ~ give you home location
if you are in /home/repl/seasonal, then cd ~/../. will take you to /home/
复制文件
cp make a copy of the original file
cp original.txt duplicate.txt creates a copy of original.txt called duplicate.txt. If there already was a file called duplicate.txt, it is overwritten.
cp seasonal/autumn.csv seasonal/winter.csv backup copies all of the files into backup directory.
移动或重命名文件
mv
move the files to another path
mv autumn.csv winter.csv moves the files autumn.csv and winter.csv from the current working directory up one level to its parent directory
rename files ( overwriting if file name are the same)
mv course.txt old-course.txt then course.txt in the current working directory is "moved" to the file old-course.txt
删除文件、路径
rm : remove as many as files you want by specifying their paths
rmdir: remove directories
mkdir :create a new directory
查看文件内容
cat print out the file content on to the screen
If file is large, then use less :
When you less a file, one page is displayed at a time; you can press spacebar to page down or type q to quit.
If you give less the names of several files, you can type :n (colon and a lower-case 'n') to move to the next file, :p to go back to the previous one, or :q to quit.
head if not specified, print out the first 10 lines of a file
head -n 100 display the first 100 lines
tail -n if not specify n , print out the last 10 lines of a file
tail -n +[Num] output the starting with [Num]
cut -f + columns no. -d , file name: select columns
cut -f 2-5,8 -d , values.csv which means select -f columns 2 through 5 and columns 8 , using (-d) comma , as the separator
cut doesn't understand quoted strings
在文件中寻找匹配
grep: selects lines according to what they contain
grep bicuspid seasonal/winter.csv prints lines from winter.csv that contain "bicuspid"
flag
usage
-c
print a count of matching lines rather than the lines themselves
-h
do not print the names of files when searching multiple files
-i
ignore case (e.g., treat "Regression" and "regression" as matches)
-l
print the names of files that contain matches, not the matches
-n
print line numbers for matching lines
-v
invert the match, i.e., only show lines that don't match
对文件进行单词计数
wc : word count
if print the number of
flag
characters
-c
words
-w
lines
-l
排序
sort: puts data in order
flag
meaning
-n
sort numerically
-r
reverse the order of its output
-b
ignore leading blanks
-f
tells it to fold case (i.e., be case-insensitive)
去重
uniq: only removes adjacent duplicated lines
uniq -c: display unique lines with a count of how often each occurs
重复上之前的指令 !
history: print a list of commands you have run recently
! command_name: re-run the most recent use of that command
! 55 : re-run the 55th command in your history
储存指令的结果 >
>: store output symbol
head's output is put in a new file called top.csv has the following two ways
head -n 5 seasonal/summer.csv > top.csv
> top.csv head -n 5 seasonal/summer.csv
多个指令的Pipline |
|: The pipe symbol, which tells the shell to use the output of the command on the left as the input to the command on the right.
head -n 5 seasonal/summer.csv | tail -n 3:
通配符
Wildcards
match
example
*
match zero or more characters
s* will match spring.csv or summer.csv
?
matches a single character
201?.txt will match 2017.txt or 2018.txt
[...]
matches any one of the characters inside the square brackets
201[78].txt matches 2017.txt or 2018.txt
{...}
matches any of the comma-separated patterns inside the curly brackets
{*.txt, *.csv} matches any file whose name ends with .txt or .csv
变量
Type
Def
environment variables
those that are available all the time, in UPPER CASE
shell variables
assign by = , called by adding $ to it
常见环境变量
Variable Name
Purpose
HOME
User's home directory
PWD
Present working directory
SHELL
Which shell program is being used
USER
User's ID
To get a complete list (which is quite long), you can type set in the shell.
打印变量的值
echo prints its arguments
echo hello gives hello
echo $USER gives repl ( the value of environment variable value )
创建新文件
touch : create a new file
修改文件
vi filename
after the file is opened, the default mode is command mode
command
Purpose
dd
delete a line
yy
copy a line
p
paste a line
u
undo
gg
move to the head of file
G
move to the tail of file
/**
search for **
执行文件
可以用的指令有: source , sh , bash, ./ , fork , source, exec
各个指令的区别
记录之前历史指令
Run history.
Pipe its output to tail -n 10 (or however many recent steps you want to save).
Redirect that to a file called something like figure-5.history.
储存指令在新文件以便重复执行
Use touch to create a file to store commands, called it dates.sh
Use sh to run the commands in dates.sh
Store output in new file dates.out
touch dates.sh
vi dates.sh
add cut -d , -f 1 seasonal/*.csv to dates.sh
then click ^O + ^X to save and exit the file
sh dates.sh > dates.out
自定义参数
$@ all of the command-line parameters given to the script
$1,$2,... specific command-line parameters
unique-lines.sh contains sort $@ | uniq,
when you run:
sh unique-lines.sh seasonal/summer.csv
then:
the shell replaces $@ with seasonal/summer.csv and processes one file.
column.sh contains cut -d , -f $2 $1
and then run it using:
sh column.sh seasonal/autumn.csv 1
then:
the shell replaces $1 with seasonal/autumn.csv,
$2 with 1
自定义函数
for loop
for ...variable... in ...list.../wildcards ; do ...body... ; done
Input
for filetype in gif jpg png; do echo $filetype; done
it produces
gif jpg png
Input
for f in seasonal/*.csv; do echo $f ; head -n 2 $f | tail -n 1; done
it produces
seasonal/autumn.csv 2017-01-05,canine seasonal/spring.csv 2017-01-25,wisdom
定义函数
function_name () {
list of commands
}
hello() {
echo "Hello $1"
}
Call function, set param as "world" and output_
hello world
自动补全
tab auto-completion of the file name, if multiple files exist with the same first letter, press tab twice
获得帮助
man see the manual of a command
man, help, info 区别