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
相关推荐
lpfasd12338 分钟前
node-sass 停止维护!老项目迁移 Dart Sass 完整教程
前端·rust·sass
不昀2 小时前
网口Link Up后仍然掉线,怎样区分物理链路和数据通路问题?
linux·以太网
Lhan.zzZ3 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
Data analyse4563 小时前
rnweb 回放数据量太大怎么办?分片存储与断点续传的实现思路
前端·网络·算法·数据分析
xiaogai_gai3 小时前
金蝶收料通知单查询策略实战教程:从源系统拉取到中间层落地的完整路径
java·前端·数据库
Fcy6484 小时前
Linux下 多路转接select详解
linux·select·多路转接
环境栈笔记4 小时前
指纹浏览器 WebRTC 怎么设置?从 ICE 候选、代理出口到功能验证
前端·人工智能·后端·自动化
无足鸟ICT4 小时前
【RHCA+】RPM包命名规则
linux·运维·服务器
赵大仁4 小时前
官网性能优化实战:LCP 3.2s → 2.1s、CLS 0.107 → 0,以及被我推翻的 6 个判断
前端·性能优化·cls·lcp·core web vitals
xiancai_xianyu4 小时前
Text2SQL落地生产库前,SQL要过哪三道闸
前端·数据库·sql·sql注入·text2sql·ai agent开发·企业级ai框架