<1> ls
bash
ls -l # 以长格式显示当前目录中的文件和目录
ls -a # 显示当前目录中的所有文件和目录,包括隐藏文件
ls -lh # 以人类可读的方式显示当前目录中的文件和目录大小
ls -t # 按照修改时间排序显示当前目录中的文件和目录
ls -R # 递归显示当前目录中的所有文件和子目录
ls -l /etc/passwd # 显示/etc/passwd文件的详细信息
<2> alias / unalias
bash
alias ls="ls --color=auto"
<3> pwd
<4> cd
bash
# Go to the home folder
cd
# Move a level up
cd ..
# Return to the previous directory
cd -
<5> cp
bash
cp file_to_copy.txt new_file.txt
# You can also copy entire directories by using the recursive flag:
cp -r dir_to_copy/ new_copy_dir/
<6> rm
bash
# To delete a regular file
rm file_to_copy.txt
# If you want to delete an empty directory
rm -r dir_to_remove/
# To remove a directory with content inside of it
rm -rf dir_with_content_to_remove/
<7> mv
bash
mv source_file.txt destination_foler/
# You also can use mv to rename files
mv old_file.txt new_named_file.txt
<8> mkdir
bash
#To make a directory
mkdir images/
# To create subdirectories with a simple command
mkdir -p movies/images
<9> touch
The touch command allows you to update the access and modification times of the specified files.
bash
touch -m old_file
Nonetheless, most of the time, you won't use touch to modify file dates, but rather to create new empty files
bash
touch new_file_name
<10> chmod
- r (read)
- w (write)
- x (execute)
<11> tail
bash
# To view only the last four lines
tail -n 4 long.txt
# 要跟踪名为 notes.log 的文件的增长情况
tail -f notes.log
# 显示 notes.log 文件的最后 10 行
tail notes.log # 默认显示最后 10 行
# 显示文件 notes.log 的最后 10 个字符
tail -c 10 notes.log
<12> cat
bash
# 显示文件 filename 的内容
cat filename
# 将标准输入重定向到文件 filename,覆盖该文件的内容
cat > filename
# 追加内容到文件:将标准输入追加到文件 filename 的末尾
cat >> filename
# 将 file1 和 file2 的内容合并到 file3 中
cat file1 file2 > file3
# 同时显示 file1 和 file2 的内容
cat file1 file2
# 显示文件 filename 的内容,并在每行的前面加上行号
cat -n filename
# 把 textfile1 和 textfile2 的文档内容加上行号(空白行不加)之后将内容附加到 textfile3 文档里
cat -b textFile1 textFile2 >> textFile3
<13> head
bash
# 显示 runoob_notes.log 文件的开头 10 行
head long.txt # 默认显示开始 10 行
# 显示 notes.log 文件的开头 5 行
head -5 long.txt
# 显示文件 notes.log 的最后 20 个字符
tail -c 20 notes.log
<14> grep
shell
# 在文件 file.txt 中查找字符串 "hello",并打印匹配的行
grep hello file.txt
# 在文件夹 dir 中递归查找所有文件中匹配正则表达式 "pattern" 的行,并打印匹配行所在的文件名和行号
grep -r -n pattern dir/
# 在标准输入中查找字符串 "world",并只打印匹配的行数
echo "hello world" | grep -c world
# 当前目录中,查找后缀有 file 字样的文件中包含 test 字符串的文件,并打印出该字符串的行
grep test *file
# 以递归的方式查找符合条件的文件。例如,查找指定目录/etc/acpi 及其子目录(如果存在子目录的话)下所有文件中包含字符串"update"的文件,并打印出该字符串所在行的内容
grep -r update /etc/acpi
# 查找文件名中包含 test 的文件中不包含test 的行
grep -v test *test*
<15>wc
bash
wc testfile testfile_1 testfile_2 #统计三个文件的信息
3 92 598 testfile #第一个文件行数为3、单词数92、字节数598
9 18 78 testfile_1 #第二个文件的行数为9、单词数18、字节数78
3 6 32 testfile_2 #第三个文件的行数为3、单词数6、字节数32
15 116 708 total #三个文件总共的行数为15、单词数116、字节数708
<16> diff
yaml
# compare the two files and show diff
diff log2014.log log2013.log
3c3
< 2014-03
---
> 2013-03
8c8
< 2013-07
---
> 2013-08
11,12d10
< 2013-11
< 2013-12
# compare and output in side-by-side mode
diff log2014.log log2013.log -y -W 50. #-y is same as --side-by-side; -W is for width
2013-01 2013-01
2013-02 2013-02
2013-03 | 2014-03
2013-04 2013-04
2013-05 2013-05
2013-06 2013-06
2013-07 2013-07
2013-08 | 2013-07
2013-09 2013-09
2013-10 2013-10
> 2013-11
> 2013-12
<17> find
bash
wc testfile testfile_1 testfile_2 #统计三个文件的信息
3 92 598 testfile #第一个文件行数为3、单词数92、字节数598
9 18 78 testfile_1 #第二个文件的行数为9、单词数18、字节数78
3 6 32 testfile_2 #第三个文件的行数为3、单词数6、字节数32
15 116 708 total #三个文件总共的行数为15、单词数116、字节数708