查看帮助文档
man 命令,help 命令,或者某个命令的 --help 参数
man ls ## 用 man 命令查看 ls 命令的帮助文档
help ls ## 用 help 命令查看 ls 命令的帮助文档
ls --help ## 用 --help 参数查看 ls 命令的帮助文档
ls 命令
列出目录文件情况:
ls ## 列出当前目录的文件
ls ./ ## 同上,‘.’号代表当前目录
ls ./*txt ## 列出当前目录下以 txt 结尾的文件
ls ../ ## 列出上层目录的文件
ls -a ## 列出当前目录下的所有文件,包括隐藏文件
ls -l ## 列出当前目录下文件的详细信息
ll ## ls -la 的简写
ls -lh ## 加上 -h 参数,以 K、M、G 的形式显示文件大小
ls -lh / ## 列出根目录下文件的详细信息
cd 命令
切换工作目录
cd .. ## 切换到上层目录,相对路径
cd / ## 切换到根目录
cd /teach/ ## 切换到根目录下的teach,绝对路径
cd - ## 返回上一次的工作目录
cd ~ ## 回到用户家目录
cd ## 同上,回到用户家目录
mkdir
# 创建目录
mkdir dir0
ls
mkdir dir0/sub1/sub2
ls
ls dir0
mkdir -p dir0/sub1/sub2 #一次性创建完整路径
ls dir0
ls dir0/sub1/
mkdir -p test{1..3}/test{1..3} #此命令将创建名为test1到test3的三个目录,并在每个目录中创建test1到test3的另外9个子目录。
touch
ls
touch file.txt new.txt # The following command creates two new empty files named file.txt and new.txt in the current directory.
touch file.txt new.txt
ls
touch file{1..5} # Create 5 empty files starting from file1 to file5
ls
rm
rm -i file.txt # Delete file.txt, and prompt before every removal
ls file* # List all files in the current directory that start with "file"
rm file* # Remove all files in the current directory that start with "file" without prompting
rm -r test1 # Remove the directory named test1 including its contents recursively
mv
mv file1 Data/file2 #moves the file named file1 from the current directory to the Data sub-directory and when moving, it is renamed as file2.
cp
cp readme.txt Data/ #This command copies the file readme.txt to directory Data/.
mkdir dir0 #creates a new directory named dir0 in the current working directory.
cp -r dir0 Data/ #recursively copy (including all of its subdirectories and file contents)the dir0 directory to Data/.
ln
ln -s /teach/software/Miniconda3-latest-Linux-x86_64.sh ./ #creates a symlink of the file "Miniconda3-latest-Linux-x86_64.sh" from the "/teach/software/" directory in the current directory.
tar
## 解压
tar -zxvf Data.tar.gz
## 压缩
tar -zcvf Data.tar.gz Data ...
cat
cat readme.txt #prints the content of readme.txt.
cat -n readme.txt #prints the content of readme.txt with line numbers on the left.
## 写入文件,creates a new file named file and allows the user to input strings via the keyboard. After typing in any texts, the user needs to type Ctrl+C to quit the inputting process.
cat >file
Welcome to Biotrainee() !
^C ## 这里是按Crtl C
## 查看
cat file
Welcome to Biotrainee() !
head、tail
head -n 20 Data/example.fq
## 查看 .bashrc 的最后 10 行
tail ~/.bashrc
## 查看第20行
head -n 20 Data/example.fq | tail -1
less
按 q 退出
less Data/example.fq #opens the file located at Data/example.fq for viewing within less. You can scroll up/down through the file with arrow keys.
less -S Data/example.fq #chops lines that are too long to fit in the screen horizontally. This is useful when viewing files with long lines like FASTA/Q files.
less -N Data/example.fq #
zless -N Data/reads.1.fq.gz #view compressed files without having to decompress them manually. In this case, the option -N will display the line numbers and the rows from the file Data/reads.1.fq.gz
wc
cat -n readme.txt
cat readme.txt | wc #counts the number of lines, words, and characters and returns the result to the console.
wc -l readme.txt #return only the number of lines in the readme.txt file.
cut
less -S Data/example.gtf | cut -f 1,3-5
less -S Data/example.gtf | cut -d 'h' -f 1
##-d 'h': 指定参数'delimiter',即指定了分隔符为'h'
##-f 1: 指定参数'fields', 即选择要输出的字段数量,默认是选择全部字段输出,选项-f后面跟着数字表示选择的字段位置。
sort
less -S Data/example.gtf | sort -k 4 | less -S
less -S Data/example.gtf | sort -n -k 4 | less -S
uniq
less -S Data/example.gtf | cut -f 3 | sort | uniq -c #打开文件,剪切第三列,排序,输出每个类别出现的次数
paste
less -S Data/example.fq | paste - - - | less -S #将 Data/example.fq 文件中每四行合并为一行,进行屏幕分页显示。
paste file1 file2
tr
cat readme.txt | tr 'e' 'E' #将其中所有 "e" 改为 "E"(tr命令) 即实现替换功能
cat readme.txt | tr '\n' '\t' #将每一行结尾的\n(换行符)替换成\t(制表符).这样可以把多行文本合并一行,可以方便文件数据处理。
cat readme.txt | tr -d 'e' #删除所有的 "e" (tr命令)
grep
grep Biotrainee -r ./ #在当前目录下(包括子目录),搜索所有文件内容,过滤出包含Biotrainee的行
less -S Data/example.fq | grep 'gene'
less -S Data/example.fq | grep -w 'gene' #-w flag ensures that only complete words are matched.
less -S Data/example.fq | grep -v -w 'gene' #搜索不包含gene(注意-v选项)且单独构成一个单词(通过使用-w选项)的行
正则表达式
cat readme.txt | grep '^T' #查找文件开头有字母“T”的文本行
cat readme.txt | grep ')$' #查找文件末尾是“)”符号的文本行
cat readme.txt | grep 'f.ee' #查找单词中间是任意一个字符的字符串“fee”
cat readme.txt | grep 'f\?ee' #查找单词中,一个字符"f"出现零次或者一次,“ee”出现一次的字符串"fee"或者"ee"
cat readme.txt | grep 're\+' #匹配类似与正则表达式“ree、reee”的等1个或多个"e"组成的字符串
cat readme.txt | grep [bB] #打印所有含有大写字母"B"和小写字母 "b" 的文本行
sed
cat readme.txt | sed '1i Welcome to Biotrainee() ' #在文件readme.txt首行前加入"Welcome to Biotrainee() "
cat readme.txt | sed '1a Welcome to Biotrainee() ' #在文件readme.txt首行后加入"Welcome to Biotrainee() "
cat readme.txt | sed '1c Welcome to Biotrainee() ' #首行替换
cat readme.txt | sed 's/is/IS/g' #其中"s"表示替代操作的开始, "/is/" 表示待替换的字符串,"/IS/" 表示要替换成的字符串,g表示全局匹配模式,即替换所有出现过的"is"。
cat readme.txt | sed '/^$/d' #pattern ^$ searchs the blank line or empty space and -d option is to delete such lines.
cat readme.txt | sed 'y/abc/ABC/' #/y/选项用于指定字符替换
awk
less -S Data/example.gtf | awk '{print $9}' | less -S #以空格为分隔符,取出每行第九列的内容并打印输出。
less -S Data/example.gtf | awk '{print $9,$10}' | less -S #输出Data/example.gtf每行第9和10个字段。两个字段之间默认以空格分隔。
less -S Data/example.gtf | awk -F '\t' '{print $9}' | less -S #以tab为分隔符,取出每行第九列的内容并打印输出。
less -S Data/example.gtf | awk '{if($3=="gene") print $0}' | less -S #如果$3所在的列(也就是第三列)的值为“gene”,则输出整条记录。
less -S Data/example.gtf | awk '{if($3=="gene") {print $0} else{print $3 " is not gene "}}' | less -S #如果该行的第三列元素(即GTF注释中的特征)等于“gene”,则打印该行,否则将字符串“is not gene”与第三列元素拼接起来并打印输出。
less -S Data/example.gtf | awk '/gene/{print $0}' | less -S
less -S Data/example.gtf | awk 'BEGIN{print "find UTR feature"} /UTR/{print $0} END{print "end"}'
less -S Data/example.gtf | awk 'BEGIN{FS="\t"} {print $9}' | less -S
less -S Data/example.gtf | awk 'BEGIN{FS="\t";OFS="\t"} {gsub("gene","Gene",$3);print $0}' | less -S #如果该行的第三列元素(即GTF注释中的特征)等于“gene”,则打印该行,否则将字符串“is not gene”与第三列元素拼接起来并打印输出。