当前位置:首页>Linux>Linux常用命令(一)

Linux常用命令(一)

  • 2026-10-11 07:59:31
Linux常用命令(一)
1.列出目录内容(ls)
ls -l  # 详细列出目录内容【包括权限、所有者、大小等】ls -a  # 显示隐藏文件
2.改变目录(cd)
cd ..  # 返回上一层目录cd ~  # 切换到用户主目录cd /  # 切换到根目录cd Documents/Projects  # 切换到当前目录下的Documents/Projects
3.显示当前目录(pwd)
pwd  # 显示当前完整路径
4.创建目录(mkdir)
mkdir new_folder  # 创建一个新目录mkdir -p /path/to/new/folder  # 递归创建多级目录mkdir dir1 dir2 dir3  # 一次创建多个目录
5. 删除空目录(rmdir)
rmdir empty_dir  # 删除一个空目录rmdir -v empty_dir  # 删除目录并显示详细信息
6.复制文件或目录(cp)
cp file.txt /path/to/destination  # 复制文件到指定目录cp -r dir1 /path/to/destination  # 复制目录及其内容cp file1 file2  # 复制并重命名文件cp -a /source/* /destination/  # 保留属性复制所有文件,但不包含隐藏文件
7.移动或重命名文件/目录(mv)
mv file.txt newfile.txt  # 重命名文件mv file.txt /path/to/newlocation  # 移动文件到新位置mv dir1 dir2  # 移动目录并重命名mv -i file.txt existingfile.txt  # 移动时提示覆盖
8.删除文件或目录(rm)
rm file.txt  # 删除文件rm -r directory  # 递归删除目录及其内容rm -f file.txt  # 强制删除文件(不提示)rm -i file.txt  # 删除时提示确认
9. 查看文件内容(cat)
cat file.txt  # 显示文件内容cat file1 file2 > combined.txt  # 合并文件内容cat -n file.txt  # 显示行号cat /dev/null > file.txt  # 清空文件内容
 10.分页查看文件(less)
less file.txt  # 分页显示文件内容less -N file.txt  # 显示行号less file.txt | grep 'keyword'  # 在分页中搜索关键词
 11.分页查看文件(more)
more file.txt  # 分页显示文件内容more file.txt | head -n 10  # 显示前10行more -d file.txt  # 显示进度+操作提示
12. 查看文件开头(head)
head -n 10 file.txt  # 显示文件前10行head -c 100 file.txt  # 显示文件前100个字节head file.txt  # 默认显示前10行
13. 查看文件结尾(tail )
tail -f file.log  # 实时查看日志文件tail -n 20 file.txt  # 显示文件最后20行
 14.搜索文本(grep)
grep 'error' log.txt  # 在文件中搜索'error'grep -i 'error' log.txt  # 忽略大小写搜索grep -r 'error' /var/log  # 递归搜索目录中的文件grep -E 'error|warning' log.txt  # 搜索多个关键词
15.查找文件(find)
find / -name "file.txt"  # 查找名为file.txt的文件find . -type d -name "dir*"  # 查找以dir开头的目录find /home -user user1  # 查找属于user1的文件find . -mtime -1  # 查找1天内修改的文件
16. 修改文件权限(chmod)
chmod 755 script.sh  # 设置文件权限为755chmod -R 755 directory  # 递归设置目录权限chmod u+x file.sh  # 增加文件的执行权限chmod o-rwx file.txt  # 删除其他用户的读写执行权限

最新文章

随机文章