适合初学者的10个linux命令
转http://devopscube.com/list-of-linux-commands-every-developer-should-know/
At some point in you developer career , you will have to work with Linux Systems and you will be looking for information regarding Linux commands. In this post I will explain the use of 10 Linux commands you should know as a developer.
10 Linux Commands For Developers
Following are the list of ten commands you will learn about in this post
man
touch, cat and less
sort and grep
cut
sed
tar
find
diff
uniq
chmod
Lets get started.
1. Man
The first command you should learn in Linux is “man”. Using this command you can get the usage and description of all Linux commands. For example, if you want to know about “ls” command and its options, just execute “man ls” command in the terminal to list its usage and description.
Syntax: man <command name>
man ls
root@devopscube:~# man ls
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speciâ
fied.
Mandatory arguments to long options are mandatory for short options
too.
-a, --all
do not ignore entries starting with .
Recommended: Learn Linux in 5 Days and Level Up Your Career Udemy Course
2. Touch, Cat And Less
Touch command is used to create any type of file in Linux systems with “0” size. As a developer , when working with Linux you might want to create files in the server. You can make use of touch command to do that.
Syntax: touch <filename>
touch demo.txt root@devopscube:~# touch demo.txt
root@devopscube:~# ls
demo.txt
Cat command is used to view the contents of a file. You cannot edit the contents of the file using cat. It just gives a view of the file. cat doesn’t support scrolling using keyboard.
Note: To work the commands given below, copy any content to the demo.txt file using your favorite vi or nano editor. For our examples, i have copied the boot.log file contents on to demo.txt file using ” cat /var/log/boot.log >> demo.txt” command. You can also do the same.
Syntax: cat <filename>
cat demo.txt
Less command also gives the view of a file. less is very fast and you can use the arrow keys to scroll up and down to know the start and end of the file. There is also “more” command, which is used to view the file but it allows only forward scrolling using “enter” key. It doesn’t support backward scrolling.
Syntax: less <filename>
more <filename>
less demo.txt
more demo.txt
3. Sort And Grep
Sort is used to sort the contents of a file. Create a file named test.txt and copy the following contents on to the file to test the sort command.
1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
In the above example, second column has the names. So if you want to sort the names alphabetically use “-k” flag with the column location. It would be “-k2”.
Syntax: sort sort -k2 test.txt
root@devopscube:~# sort -k2 test.txt
45 Dave level expert dec
4 dennis start beginner jul
10 lucy level beginer mar
58 Mathew Head CEO nov
7 Megan employee trainee feb
1 mike level intermediate jan
The first column has numbers. If you want to sort numbers, use “-h” flag. If the numbers are in different column, you can use the “-k” flag along with “-h” flag.
root@devopscube:~# sort -h test.txt
1 mike level intermediate jan
4 dennis start beginner jul
7 Megan employee trainee feb
10 lucy level beginer mar
45 Dave level expert dec
58 Mathew Head CEO nov
The last column has months. You can sort a file based on month using “-M” flag.
root@devopscube:~# sort -k5 -M test.txt
1 mike level intermediate jan
7 Megan employee trainee feb
10 lucy level beginer mar
4 dennis start beginner jul
58 Mathew Head CEO nov
45 Dave level expert dec
Note: If you want to eliminate the duplicate lines , you can use “-u” flag along with the sort command.
To sort the file in descending order, use “-r” flag.
root@devopscube:~# sort -h -r test.txt
58 Mathew Head CEO nov
45 Dave level expert dec
10 lucy level beginer mar
7 Megan employee trainee feb
4 dennis start beginner jul
1 mike level intermediate jan
Grep:
Grep is a powerful command and will be used by sysadmins quite often. Grep command is used for searching specific string patterns in a file as well as the standard output (STDIN). We will look into few file based operations . Other uses of grep is out of scope of this post.
Syntax: grep "<search string>" <filename>
grep "Mathew" test.txt
root@devopscube:~# grep "dennis" test.txt
4 dennis start beginner jul
The above command gives the output including the sub-string. If you want to search for individual words, you need to add “-i” flag to the grep command. Also you can search for a string or a pattern in multiple files using a single grep command. For example,
grep "dennis" test1.txt test2.txt test3.txt
You can also use regular expressions for matching the string.
4. Cut
Cut command is used for extracting a portion of a file using columns and delimiters. If you want to list everything in a selected column, use the “-c” flag with cut command. For example, lets select the first two columns from our test.txt file.
cut -c1-2 test.txt
root@devopscube:~# cut -c1-2 test.txt
1
10
45
4
7
58
If you want to extract specific strings from a file, you can used the delimiter “-d” flag and “-f” flag to select the field. For example, if you wan to extract all the names from our test.txt file you can use the following command.
cut -d' ' -f2 test.txt
root@devopscube:~# cut -d' ' -f2 test.txt
mike
lucy
Dave
dennis
Megan
Mathew
The following example extracts the users from /etc/passd file using ‘:” delimiter.
cut -d':' -f1 /etc/passwd
5. Sed
sed is a text-editor which can perform editing operations in a non-interactive way. Sed command gets its input from a standard input or a file to perform the editing operation on a file. Sed is a very powerful utility and you can do a lot of file manipulations using sed. I will explain the important operation you might want to do with text file.
I you want to replace a text in a file by searching it in a file, you can use the sed command with substitute “s” flag to search for the specific pattern and change it.
Syntax: sed 's/<old-word>/<new-word>/' test.txt
For example, lets replace “mike” in test.txt file to “michael
sed 's/mike/michael/' test.txt
root@devopscube:~# sed 's/mike/michael/' test.txt
1 michael level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
In the above example we used “/” as a delimiter for string substitution. You can use any character as a delimiter for substitution. For example, if you want to make changes to a url, you need to have a different delimiter because the url already have slashes. So you can substitute like the following.
echo "http://www.example.uk/main.html" | sed 's_uk/main_com/index_'
root@devopscube:~# echo "http://www.example.uk/main.html" | sed 's_uk/main_com/index_'
http://www.example.com/index.html
You can also replace a line by matching a string pattern in the line. “-c” flag is used for replacing text using sed. Lets replace the first line in our test.txt file using the following command.
sed '/1 mike/c 1 michael start beginner mar' test.txt
root@devopscube:~# sed '/1 mike/c 1 michael start beginner mar' test.txt
1 michael start beginner mar
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
6. Tar
tar command is used to create and extract archive files. “-cf” and “-xf” flags are used for creating and extracting archives.
Syntax: tar <options> <archive-name> <file/folder name>
Lets create a tar archive out of test.txt file
tar -cf test.tar test.txt
root@devopscube:~# tar -cf test.tar test.txt
root@devopscube:~# ls
test.tar test.txt
Lets extract the test.tar archive to the destination folder “demo” using “-C” flag.
tar -xf test.tar -C /root/demo/
root@devopscube:~# tar -xf test.tar -C /root/demo/
root@devopscube:~# cd demo/
root@devopscube:~/demo# ls
test.txt
7. Find
find command is used for finding files. You can find the files using its name with “-name” flag.
find -name find -name test.txt
root@devopscube:/home/ubuntu# cd ~
root@devopscube:~# find -name test.txt
./demo/test.txt
./test.txt
You can also find folder using its name by using”/ -name” flag.
find / -name passwd
root@devopscube:~# find / -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd
/usr/share/lintian/overrides/passwd
8. Diff
diff command is used to find the difference between two files. Diff command analyses the files and prints the lines which are different. Lets say we have two files test and test1. you can find the difference between the two files using the following command.
Syntax: diff <filename1> <filename2>
diff test.txt test1.txt
root@devopscube:~# diff test.txt test1.txt
7c7
< 59 sdfsd
---
> 59 sdfsd CTO dec
9. Uniq
uniq command is used for filtering out the duplicate line in a file.
Syntax: uniq
uniq test.txt
root@devopscube:~# uniq test.txt
1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
10. Chmod
chmod command is used for changing the read/write/execute permissions of a file. Permissions are represented in numbers as follows.
4 - read permission
2 - write permission
1 - execute permission
0 - no permission
To give all permissions on test.txt file, you can use the following chmod command.
chmod 755 test.txt
适合初学者的10个linux命令的更多相关文章
- 10个 Linux 命令,让你的操作更有效率
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 根据老九大师兄口头阐述,Linux是最适合开发的操作系统 ...
- 每一个程序员需要了解的10个Linux命令
作为一个程序员,在软件开发职业生涯中或多或少会用到Linux系统,并且可能会使用Linux命令来检索需要的信息.本文将为各位开发者分享10个有用的Linux命令,希望对你会有所帮助. 以下就是今天我们 ...
- 【转】每一个程序员需要了解的10个Linux命令
作为一个程序员,在软件开发职业生涯中或多或少会用到Linux系统,并且可能会使用Linux命令来检索需要的信息.本文将为各位开发者分享10个有用的Linux命令,希望对你会有所帮助. 以下就是今天我们 ...
- 10条Linux 命令了解服务器当前性能
参考:http://www.infoq.com/cn/news/2015/12/linux-performance 1. uptime 如果电脑运行缓慢,执行 uptime 可以大致查看Linux服务 ...
- 详解Linux命令行下常用svn命令
1.Linux命令行下将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/do ...
- Linux命令——pgrep
参考:Linux pgrep Command Tutorial for Beginners (10 Examples) Linux命令——ps.pstree bash基础——grep.基本正则表达式. ...
- Linux命令行下常用svn命令
1.Linux命令行下将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/do ...
- 再见Xshell、Xftp!Python执行Linux命令、上传下载远程文件
相信大家应该都接触过Linux操作系统(Ubuntu.Centos等),那么在使用的Linux操作系统需要使用一些远程ssh工具,尤其是公网服务器. 常用的ssh工具主要有:Xshell.MobaXt ...
- 初学者常用的LINUX命令
测试初学者常用的LINUX命令 一.常用管理命令:1. shutdown -h now 关机命令2. shutdown -r now (reboot) 立即重启命令 3. clear 清屏命令 4. ...
随机推荐
- NOIP2017tg【逛公园】 题解
先说点别的 emmm--,这是本蒟蒻的第一篇题解,大佬们勿喷QwQ(要不是看到写题解可以加贡献,我才--) 可以看到标签,是2017年提高的题目,好像是Day1T3,感觉提高考这样的题目挺好的,至少考 ...
- ES6转换ES5
各大浏览器的最新版本,对 ES6 的支持可以查看kangax.github.io/es5-compat-table/es6/.随着时间的推移,支持度已经越来越高了,超过 90%的 ES6 语法特性都实 ...
- BUU pwn cn
自己不细心,人家别的博客上写的明明没有那个冒号的,把linux命令好好学一学吧! nc后 ls 发现flag文件 cat就得到flag了
- NO31 配置网卡--主机名--网络故障排查面试题--DNS
修改网卡配置信息: 修改主机名规范的三个步骤: 配置默认网关: DNS解析过程,用命令看: DNS相关命令: 口述DNS解析过程: 客户端(电脑)通过浏览器输入域名,先找hosts文件及本地dns缓 ...
- 利用uboot下载引导Kernel(TFTP)以及挂载网络Rootfs(NFS)
背景: 在嵌入式开发中,经常需要对系统的各个部分进行修改.倘若每次修改都烧写到板子中,一来浪费时间,其次影响存储介质寿命. 所以,需要一些手段来避免此类问题. 概览: 编译uboot 将uboot写入 ...
- 《动手学深度学习》系列笔记 —— 语言模型(n元语法、随机采样、连续采样)
目录 1. 语言模型 2. n元语法 3. 语言模型数据集 4. 时序数据的采样 4.1 随机采样 4.2 相邻采样 一段自然语言文本可以看作是一个离散时间序列,给定一个长度为\(T\)的词的序列\( ...
- gulp和npm等安装
提前安装了node.js, https://nodejs.org/zh-cn/download/ 跟着提示安装就行,然后执行一下命令cdm看下版本号如下图就说明安装成功了 安装包里面集成了npm,然后 ...
- apache端口修改为80
apache端口莫名改变为443,访问网址失败,修改Apache端口: 1.打开目录(实际而定): C:\xampp\apache\conf 编辑httpd.conf 2.ctrl + f 搜索li ...
- Fréchet Inception Distance(FID)
计算 IS 时只考虑了生成样本,没有考虑真实数据,即 IS 无法反映真实数据和样本之间的距离,IS 判断数据真实性的依据,源于 Inception V3 的训练集 ------ ImageNet,在 ...
- 十九、React UI框架Antd(Ant Design)的使用——及react Antd的使用 button组件 Icon组件 Layout组件 DatePicker日期组件
一.Antd(Ant Design)的使用:引入全部Css样式 1.1 antd官网: https://ant.design/docs/react/introduce-cn 1.2 React中使用A ...