此文章主要记录Linux中的入门级基础操作方法,对基础的Linux操作语句进行了整理
文件查看
pwd 列出当前目录的路径,查看当前所在的目录
[root@localhost ~]# pwd
/root
~表示用户的家目录
ls 列出当前目录下的所有文件
[root@localhost ~]# ls
anaconda-ks.cfg
ll (ls -l缩写)列出当前目录下的文件(带文件信息)
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
[root@localhost ~]# ls -l
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
ll-a 列出当前目录下的所有文件(包括隐藏文件)
[root@localhost ~]# ll -a
total 28
dr-xr-x---. 2 root root 135 Sep 8 17:38 .
dr-xr-xr-x. 17 root root 224 Sep 7 18:05 ..
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-------. 1 root root 8 Sep 8 17:38 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[root@localhost ~]# ^C
[root@localhost ~]# ^C
[root@localhost ~]# ^C
[root@localhost ~]# ^C
[root@localhost ~]#
ll --help 查看ls用法,-help为帮助指令
[root@localhost ~]# ll --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
............
创建,重命名文件\文件夹
touch filename 创建空文件夹
创建空头文件hello.txt
[root@localhost ~]# touch hello.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
mkdir 创建目录
创建目录 abc
[root@localhost ~]# mkdir abc
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
mkdir -p(目标文件即使存在也不会报错
[root@localhost ~]# mkdir -p abc
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
[root@localhost ~]#
当我们创建新文件,并且不知道它是否存在时,可用-p参数,保证不会报错
mv 重命名文件\文件夹
修改abc名称为abc
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
[root@localhost ~]# mv abc abx
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
链接文件
硬链接(对原始文件的操作):
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
[root@localhost ~]# In hello.txt hlink
[root@localhost ~]# ln hello.txt hlink
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 2 root root 0 Sep 18 21:51 hello.txt
-rw-r--r--. 2 root root 0 Sep 18 21:51 hlink
软链接(相当于快捷方式,不能删除原文件) 在ln后添加-s
[root@localhost ~]# ln -s hello.txt hlink
ln: failed to create symbolic link ‘hlink’: File exists
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 2 root root 0 Sep 18 21:51 hello.t
切换目录
cd . 当前目录
一个 . 表示
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd .
[root@localhost ~]# pwd
/root
**cd . .**去上一级目录
两个 . . 表示上一级目录
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd ..
[root@localhost /]# pwd
/
在cd后指定目录,可切换到指定目录
[root@localhost /]# cd /
[root@localhost /]# pwd
/
[root@localhost /]# cd /bin/
[root@localhost bin]# pwd
/bin
[root@localhost bin]#
**cd ~**去当前户主目录
[root@localhost test]# cd ~
[root@localhost ~]# pwd
/root
cd xxx/xxx直接跳转到某个目录
[root@localhost ~]# cd abx/test/
[root@localhost test]# pwd
/root/abx/test
删除文件\文件夹(目录)
rm命令可删除文件或目录,也可将其下面的所有文件及其子目录均删除掉
对于链接文件,仅删除整个链接文件,而原有文件保持不变
常见用法:
rm 删除文件(会有提示对话,输入y表示确认删除)
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 19 15:28 abc.txt
-rw-r--r--. 1 root root 0 Sep 19 15:27 sbc.txt
[root@localhost test]# rm abc.txt
rm: remove regular empty file ‘abc.txt’? y
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 19 15:27 sbc.txt
rm -r 删除目录(需要确认)
删除目录需要指定参数r,否则提示不能删除
[root@localhost abx]# rm -r test
rm: remove directory ‘test’? y
[root@localhost abx]# ll
total 0
rm -f 强制删除(无询问语句)
[root@localhost abx]# touch a.txt
[root@localhost abx]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 19 15:40 a.txt
[root@localhost abx]# rm -f a.txt
[root@localhost abx]# ll
total 0
强制删除有一定的风险性,由于Linux中没有回收站,删除后文件无法找回
rm -rf 递归删除目录及文件
Linux中最为危险的操作,最具有破坏性
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 19 15:40 abx
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
[root@localhost ~]# mkdir -p abx/test/aaa
[root@localhost ~]# cd abx/test/aaa/
[root@localhost aaa]# touch a.txt
[root@localhost aaa]# cd ~
[root@localhost ~]# rm -rf abx
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
由于rf参数可递归删除任何数据,有极强的破坏力,所以需要慎用!!!
复制\粘贴\剪切
常用方法:
cp 复制&粘贴文件
复制hello.txt文件,复制后的文件名为hello~bak.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
[root@localhost ~]# cd hello.txt
-bash: cd: hello.txt: No such file or directory
[root@localhost ~]# touch hello.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt
[root@localhost ~]# cp hello.txt hello-bak.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt
-rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt
[root@localhost ~]#
cp -r 复制&粘贴文件或目录
复制目录,需指定参数r
[root@localhost ~]# mkdir abc
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 19 23:32 abc
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt
-rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt
[root@localhost ~]# cp -r abc xyz
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Sep 19 23:32 abc
-rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt
-rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt
drwxr-xr-x. 2 root root 6 Sep 19 23:32 xyz
mv 移动(剪切)文件或目录
将目录xyz移动到abc下
[root@localhost ~]# ll abc/
total 0
[root@localhost ~]# mv xyz abc
[root@localhost ~]# ll abc/
total 0
drwxr-xr-x. 2 root root 6 Sep 19 23:32 xyz
内容查看
cat 显示文本内容
[root@localhost ~]# cat anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
......
cat -b 显示行输出
[root@localhost ~]# cat -b anaconda-ks.cfg
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
4 # Use CDROM installation media
5 cdrom
.......
分屏显示more
根据屏幕大小显示部分内容
[root@localhost ~]# more anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
........
输出及显示
echo 类似与Java中的System.out.println()
echo:不解析转义字符
echo -e:解析转义字符
echo $PATH: 输出环境变量
[root@localhost ~]# echo "Hello\t\t world"
Hello\t\t world
[root@localhost ~]# echo -e "hello\t\t world"
hello world
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
磁盘使用情况
df 查看磁盘使用情况
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 8374272 1027072 7347200 13% /
devtmpfs 485828 0 485828 0% /dev
tmpfs 497944 0 497944 0% /dev/shm
tmpfs 497944 7748 490196 2% /run
tmpfs 497944 0 497944 0% /sys/fs/cgroup
/dev/sda1 1038336 135276 903060 14% /boot
tmpfs 99592 0 99592 0% /run/user/0
清屏小命令
clear 进行屏幕的清屏
使用后:
[root@localhost ~]#
查看内存使用情况
free
free -m : 显示单位为MB
free -h : 根据值的大小,显示易于识别的单位
[root@localhost ~]# ^C
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 972 121 712 7 138 690
Swap: 1023 0 1023
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 972M 121M 712M 7.6M 138M 690M
关机重启快捷命令
shut down -h now 关机
reboot -h now 重启
exit 退出当前登录状态