Linux简单命令行:文件/目录,sudo,包管理linux学习编程

Ubuntu系列---常用命令行

2016-12-05  本文已影响74人  sunnyaxin

Ubuntu系统为开发人员提供了丰富的命令行进行操作,本文只针对初级入门,学习常用的命令行,以便高效完成各种日常操作。Ubuntu上的各种常用命令行:

文件/目录

命令行 意义
cd <dir> 更改当前工作目录
mkdir <dir> 创建一个新目录
cp <sou-file> <des-file> 将sou-file文件复制到des-file文件夹下
cp -r <sou-folder> <des-folder> 将sou-folder文件夹复制到des-folder文件夹下
rm <file> 删除指定文件
rm -r <dir> 删除指定指定目录
rm -f <file> 强制删除指定文件
rm -rf <dir> 强制删除指定目录
mv <sou> <des> 移动文件;或者重命名
pwd 显示当前工作目录的路径
ls -l 以详细列表形式显示当前工作目录下的所有普通文件
ls -a 显示当前工作目录下的所有文件,包括隐藏文件
tar -cvf <archive-file.tar> <files> 文件(可以多个)添加进压缩包
tar -tvf <archive-file.tar> 查看压缩包内容
tar -xvf <archive-file.tar> 解压压缩包内容
gzip <file> 压缩file文件至file.gz压缩包中
gzip -d <file.gz> 解压gz文件至当前目录
unzip <archive-file.zip> 解压zip文件
unzip -l <archive-file.zip> 查看zip文件内容
find <dir> -name <file> 在dir目录下查找名字为file的文件
find <dir> -iname <file> 同上,忽略大小写
grep "<string>" <file> 在文件file中搜寻字符串string
less <file> 打印文件内容,可翻页
tail <file> 打印文件内容,主要用于显示后几页
tree <file> 树形结构显示目录file下文件结构,需要安装tree

账号操作

命令行 意义
su <user> 切换用户
sudo 允许普通用户执行一些或全部root命令
whoami 查询当前登录用户
who 显示所有在线用户
w 详细显示所有在线用户
passwd 更改当前用户登录密码

包管理

命令行 意义
apt-get install <package> 安装一个新软件包
apt-get remove <package> 卸载软件包(保留配置文档)
apt-get remove --purge <package> 卸载软件包(删除配置文档)

进程相关

命令行 意义
ps 查询当前系统运行进程的信息
kill <pid> 终止指定pid的进程
shutdown 关闭Ubuntu系统
shutdown -r 重启Ubuntu系统

网络相关

命令行 意义
ping <address> 检测网络连接与服务器状态
telnet <host> [port] 远程登录host主机,可通知指定端口port登录
curl <URL> 将指定url返回的数据打印在默认终端上
netstat [options] 显示与IP,TCP,UDP和ICMP协议相关的统计数据,检验本机各端口网络连接情况

其他命令

  1. 关于mkdir:make directory
$ mkdir temp
$ mkdir temp/stuff
$ mkdir temp/stuff/things
$ mkdir -p temp/stuff/things/orange/apple/pear/grape
  1. 关于cd:change directory
$ cd temp
$ pwd
~/temp
$ cd stuff
$ pwd
~/temp/stuff
$ cd things
$ pwd
~/temp/stuff/things
$ cd orange/
$ pwd
~/temp/stuff/things/orange
$ cd apple/
$ pwd
~/temp/stuff/things/orange/apple
$ cd pear/
$ pwd
~/temp/stuff/things/orange/apple/pear
$ cd grape/
$ pwd
~/temp/stuff/things/orange/apple/pear/grape
$ cd ..
$ cd ..
$ pwd
~/temp/stuff/things/orange/apple
$ cd ..
$ cd ..
$ pwd
~/temp/stuff/things
$ cd ../../..
$ pwd
~/
$ cd temp/stuff/things/orange/apple/pear/grape
$ pwd
~/temp/stuff/things/orange/apple/pear/grape
$ cd ../../../../../../../
$ pwd
~/
  1. 关于删除文件
    rmdir命令是用来删除空的目录,rm命令是用来删除非空的目录。

If you try to do rmdir on Mac OSX and it refuses to remove the directory even though you are positive it's empty, then there is actually a file in there called .DS_Store. In that case, type rm -rf <dir> instead

  1. pushd, popd (具体见这里)

In this exercise you learn how to save your current location and go to a new location with pushd. You then learn how to return to the saved location with popd.

$ cd temp
$ mkdir -p i/like/icecream
$ pushd i/like/icecream
~/temp/i/like/icecream ~/temp
$ popd
~/temp
$ pwd
~/temp
$ pushd i/like
~/temp/i/like ~/temp
$ pwd
~/temp/i/like
$ pushd icecream
~/temp/i/like/icecream ~/temp/i/like ~/temp
$ pwd
~/temp/i/like/icecream
$ popd
~/temp/i/like ~/temp
$ pwd
~/temp/i/like
$ popd
~/temp
$ pushd i/like/icecream
~/temp/i/like/icecream ~/temp
$ pushd
~/temp ~/temp/i/like/icecream
$ pwd
~/temp
$ pushd
~/temp/i/like/icecream ~/temp
$ pwd
~/temp/i/like/icecream
  1. cd - :回到前一个目录
    cd : 回到home目录
  2. 关于touch:make an empty file

注意

  1. 关于rm:
    通常情况下rm不会删除目录,你必须通过指定参数-r或-R来删除目录。另外rm通常可以将该文件或目录恢复(注意,rm删除文件其实只是将指向数据块的索引点(information nodes)释放,只要不被覆盖,数据其实还在硬盘上。如果想要保证文件的内容无法复原,可以使用命令shred 。 另外一般还是要慎用rm -rf *这样的命令。
  2. 关于查看文件内容:less more cat head tail string
$ less test2.txt
[displays file here]
$ cat test2.txt
I am a fun guy.
Don't you know why?
Because I make poems,
that make babies cry.
$ cat test.txt
Hi there this is cool.

小结

commands definition
pwd print working directory
hostname my computer's network name
mkdir make directory
cd change directory
ls list directory
rmdir remove directory
pushd push directory
popd pop directory
cp copy a file or directory
mv move a file or diretory
less page through a file
cat print the whole file
xargs execute arguments
find find files
grep find things inside files
man read a manual page
apropos find what man page is appropriate
env look at your environment
echo print some arguments
export export/set a new environment variable
exit exit the shell
sudo DANGER! become super user root DANGER!

参考资料

常用命令行介绍
常用命令行cheet sheet
29个你必须知道的Linux命令
Linux mkdir、tar 和 kill 命令的 4 个有用小技巧
慕课网 《Linux达人养成计划 I》
慕课网 《Linux达人养成计划 II》
慕课网上更多Linux相关课程
Ubuntu各种技巧
Ubuntu常用命令行教程
书籍《鸟哥的Linux私房菜》
Ubuntu上的常用命令行
https://learnpythonthehardway.org/book/appendixa.html

上一篇 下一篇

猜你喜欢

热点阅读