Ubuntu系列---常用命令行

作者: sunnyaxin | 来源:发表于2016-12-05 17:44 被阅读74次

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

  • 文件/目录:cd,mkdir,cp,rm,mv,pwd,ls,tar,zip,find,grep,less,tail,tree
  • 帐号操作:su,sudo,whoami,passwd
  • 包管理:apt-get,添加软件源
  • 进程相关:ps,kill
  • 系统信息相关:top,df,uname
  • 网络相关:ping,telnet,curl,netstat,修改hosts

文件/目录

命令行 意义
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命令是用来删除非空的目录。
  • rmdir:remove directory
  • rm -rf <file>

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命令与more:都可以做到分页查看文件内容,区别在于less支持空格、上下键查看内容,并且支持向上翻页查看已经看过的内容,more却只能往下翻,不能往前翻
  • cat一次性显示全部内容
  • head前于前多少条内容
  • tail显示后几条内容,tail -f可以动态的显示文件的更新
  • 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

相关文章

  • Ubuntu系列---常用命令行

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

  • Android NDK开发之旅31--NDK-Linux入门之基

    常用的Linux命令 shell命令有多种,Ubuntu默认使用 B shell。 命令行相关的命令 命令自动补全...

  • tcpdump 使用

    tcpdump是linux命令行下常用的的一个抓包工具,记录一下平时常用的方式,测试机器系统是ubuntu 12....

  • 命令行

    用了差不多两周的 Ubuntu 了,也只是对一些常用的命令行进行了初步的了解。今天就来说说 Ubuntu 上的一些...

  • 设置 Ctrl+Alt+T 呼出命令行

    前言 用了很久的ubuntu,最近在使用kali时发现,常用的呼出命令行快捷键:Ctrl+Alt+T失效了,经过查...

  • docker 基础镜像

    dokcer 基础镜像选择 目前常用的Linux发行版主要包括Debian/Ubuntu系列和CentOS/Fed...

  • Ubuntu上的常用命令行

    本文主要讨论的是Ubuntu上常用的命令行。熟记这些命令,让你的手在键盘上飞起来吧~ Files Commands...

  • ubuntu常用命令行

    df -hl //#查看磁盘空间 cd /etc //进入文件夹 cd ~ //进入用户目录 ls //查看当前...

  • ubuntu安装golang

    Ubuntu安装goland ubuntu 本身就支持 golang 命令行安装 配置运行环境 打开bashrc:...

  • 开发者神器 Ubuntu Make 15.11.1 发布

    Ubuntu Make 原名 Ubuntu Developer Tools Center,是一款开源的命令行工具软...

网友评论

  • 屈鉴铭:这篇写的不错:+1:基本涵盖了常用的命令和操作

本文标题:Ubuntu系列---常用命令行

本文链接:https://www.haomeiwen.com/subject/bpakmttx.html