Linux Command Recap

<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
相关推荐
南川琼语2 分钟前
Linux——共享内存
linux·运维·服务器
2401_878454532 小时前
Themeleaf复用功能
前端·学习
葡萄城技术团队4 小时前
基于前端技术的QR码API开发实战:从原理到部署
前端
风路丞5 小时前
centos-stream-9上安装nvidia驱动和cuda-toolkit
linux·运维·centos
八了个戒5 小时前
「数据可视化 D3系列」入门第三章:深入理解 Update-Enter-Exit 模式
开发语言·前端·javascript·数据可视化
noravinsc6 小时前
html页面打开后中文乱码
前端·html
小满zs6 小时前
React-router v7 第四章(路由传参)
前端·react.js
Amelio_Ming6 小时前
linux 内核 static-key机制分析
linux
小陈同学呦6 小时前
聊聊双列瀑布流
前端·javascript·面试