github命令行挑战

官网:https://cmdchallenge.com/
本网站不是教你如何使用某些命令,而是给你一些任务,按照它的说明来完成相应的操作,借此来巩固和熟悉你命令行的技巧。

1. Print “hello world”.

echo "hello world"

2. Print the current working directory.

echo $PWD

3. List names of all the files in the current directory, one file per line.

ls
ls -aF

其中参数:

-a 显示所有文件及目录 (ls内定将文件名或目录名称开头为”.”的视为隐藏档,不会列出)

-F 在列出的文件名称后加一符号;例如可执行档则加 “*”, 目录则加 “/”

5. There is a file: ./…/ /. .the flag.txt. Show its contents on the screen.

cat "./.../  /. .the flag.txt"

注意这里要加上双引号!

6. There is a file named “access.log” in the current directory. Print the contents.

cat access.log

7. Print the last 5 lines of “access.log”.

tail -n 5 access.log

其中参数:

tail -n k          # 打印最后k行

tail -n +k         # 从k行开始打印

如果要输出最前面的几行,则可以用head命令:

head -n k          # 打印前k行

head -n -k         # 打印除最后k行外的所有内容

8. There is a file named “access.log” in the current working directory. Print all lines in this file that contains the string “GET”.

cat access.log | grep GET

其中grep命令用于查找文件里符合条件的字符串。

9. How many lines contain tab characters in the file named “file-with-tabs.txt” in the current directory.

tr -cd '\t' < file-with-tabs.txt | wc -c

Linux tr 命令用于转换或删除文件中的字符。

tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。

其中参数:

-c, –complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换

-d, –delete:删除指令字符

所以上面命令的操作是删除不含有tab的所有行,并将结果输出到原文件中

另外,Linux wc命令用于计算字数。参数如下:

-c或–bytes或–chars 只显示Bytes数。

-l或–lines 只显示列数。

-w或–words 只显示字数。

10. Print all files in the current directory, one per line (not the path, just the filename) that contain the string “500”.

grep -Ril "500" ./ 

不知咋的,这条突然就不可以了。

11. Print the relative file paths, one path per line for all filenames that start with “access.log” in the current directory.

ls $PWD | grep "access.log"

14. Delete all of the files in this challenge directory including all subdirectories and their contents.

ls -A | xargs rm -rf

xargs:将stdin转换为其他命令的参数并执行.

这个命令的作用就是将stdout的内容,作为其他命令的参数进行调用。基本格式是:

command | xargs ...

这样的结果是ls输出的每一行都被执行了rm -rf 操作。其中参数:

-a 显示所有文件及目录 (ls内定将文件名或目录名称开头为”.”的视为隐藏档,不会列出),

-A 同-a, 但不列出 “.” (目前目录) 及 “..” (父目录)

不知道为什么,这里用rm -rf *一直过不了…总是显示”Test failed, files or directories remain.”…

15. Count the number of files in the current working directory. Print the number of files as a single integer.

ls ./ | wc -l

16. Print the contents of access.log sorted.

cat access.log | sort

17. Print the number of lines in access.log that contain the string “GET”.

grep "GET" access.log | wc -l

18. The file split-me.txt contains a list of numbers separated by a ‘;’ character. Split the numbers on the ‘;’ character, one number per line.

tr ";" '\n' < split-me.txt

替换 ; 号为换行符,实现一行一个数字。

19. Print the numbers 1 to 100 separated by spaces.

seq -s " " 1 100

其中seq用于产生一系列的整数,参数如下:

-f:用来格式化输出,默认是%g

-s:用来指定分隔符号,默认是回车

-w:输出同宽数列,不足的位数用0补齐

20. There are some files in this directory that start with a dash in the filename. Remove those files.

find -name "-*" | xargs rm -rf

或者

find -name "-*" -exec rm -rf {} \;

其中”-*”用来匹配所有以‘-‘符号开头的文件。

21. There are files in this challenge with different file extensions. Remove all files with the .doc extension recursively in the current working directory.

find -name "*.doc" | xargs rm -rf

或者

find . -name "*.doc" -exec rm -rf {} \;  

24. The file sum-me.txt has a list of numbers, one per line. Print the sum of these numbers.

paste -s -d+ sum-me.txt | bc

Linux paste命令用于合并文件的列。
paste指令会把每个文件以列对列的方式,一列列地加以合并。

其中参数:

-d<间隔字符>或–delimiters=<间隔字符>  用指定的间隔字符取代跳格字符。

-s或–serial  串列进行而非平行处理。

bc 命令是用于命令行计算器。 它类似基本的计算器。 使用这个计算器可以做基本的数学运算。

所以上面的操作是把所有的数字放在同一行,并用+分隔开,最后调用bc命令进行计算,得出所有数字的总和。

27. The files in this challenge contain spaces. List all of the files (filenames only) in the current directory but replace all spaces with a ‘.’ character.

ls ./ | tr " " "."

29. Print the 25th line of the file faces.txt

sed -n 25p faces.txt

sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。其中参数:

p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

30. Print the file faces.txt, but only print the first instance of each duplicate line, even if the duplicates don’t appear next to each other. Note that order matters so don’t sort the lines before removing duplicates.

awk '!a[$0]++' faces.txt 

如果是第一次出现, a[$0]++的值为0(假),而!a[$0]++的值就为1(真),之后就执行print $0

第二次或者两次以上的出现a[$0]++的值就为大于0的整数值(真),例如1,2,3…,而!a[$0]++的值就为0(假),之后就不执行print $0操作

这里不能用uniq faces.txt命令。因为在没排序的前提下uniq不能实现隔行去重,而任务要求是重复内容的第一次出现才输出。

PS: linux的命令真是博大精深……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值