cmdchallenge.com提供了许多有趣的linux cmd训练题,通过一些问题来引导玩家熟悉linux命令行的各种命令和参数,本文为本人的一些总结,供参考。
Your first challenge is to print "hello world" on the terminal in a single command.
Hint: There are many ways to print text on the command line, one way is with the 'echo' command. Try it below and good luck!
echo "hello world"
Print the current working directory.
pwd
List names of all the files in the current directory, one file per line.
find . -type f -printf "%f\n"
There is a file named
access.log
in the current directory. Print the contents.
cat access.log
Print the last 5 lines of "access.log".
tail -n 5 access.log
Create an empty file named
take-the-command-challenge
in the current working directory.
touch take-the-command-challenge
Create a directory named tmp/files in the current working directory
mkdir -p tmp/{files,}
Copy the file named
take-the-command-challenge
to the directorytmp/files
cp take-the-command-challenge tmp/files/
Move the file named
take-the-command-challenge
to the directorytmp/files
mv take-the-command-challenge tmp/files
A symbolic link is a type of file that is a reference to another file.
Create a symbolic link named
take-the-command-challenge
that points to the filetmp/files/take-the-command-challenge
.
ln -s tmp/files/take-the-command-challenge take-the-command-challenge
Delete all of the files in this challenge directory including all subdirectories and their contents.
Hint: There are files and directories that start with a dot ".", "rm -rf *" won't work here!
find . -exec rm -rf {} \;
There are files in this challenge with different file extensions. Remove all files with the .doc extension recursively in the current working directory.
find . -type f -name "*.doc" -exec rm {} \;
There is a file named
access.log
in the current working directory. Print all lines in this file that contains the string "GET".
grep "GET" access.log
Print all files in the current directory, one per line (not the path, just the filename) that contain the string "500".
grep -l "500" *
Print the relative file paths, one path per line for all filenames that start with "access.log" in the current directory.
ls |grep "access.log"
Print all matching lines (without the filename or the file path) in all files under the current directory that start with "access.log" that contain the string "500".
Note that there are no files named
access.log
in the current directory, you will need to search recursively.
find . -type f -name "access.log*" -exec grep -h "500" {} \;
Extract all IP addresses from files that start with "access.log" printing one IP address per line.
find . -type f -name "access.log*" -exec grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' {} \;
Count the number of files in the current working directory. Print the number of files as a single integer.
find . -maxdepth 1 -type f | wc -l
Print the contents of access.log sorted.
cat access.log |sort
Print the number of lines in access.log that contain the string "GET".
grep -c "GET" access.log
The file split-me.txt contains a list of numbers separated by a
;
character.Split the numbers on the
;
character, one number per line.
1: tr ';' '\n' < split-me.txt
2: awk -F';' '{ for (i=1; i<=NF; i++) print $i }' split-me.txt
3: cut -d ';' -f 1- --output-delimiter=$'\n' split-me.txt
4: sed 's/;/\n/g' split-me.txt
This challenge has text files (with a .txt extension) that contain the phrase "challenges are difficult". Delete this phrase from all text files recursively.
Note that some files are in subdirectories so you will need to search for them.
find . -type f -iname "*.txt" -exec sed -i "s/challenges are difficult//g" {} \;
The file sum-me.txt has a list of numbers, one per line. Print the sum of these numbers.
sum=0;for i in `cat sum-me.txt`; do ((sum=$sum+$i)); done; echo $sum
Print all files in the current directory recursively without the leading directory path.
find . -type f -printf "%f\n"
Rename all files removing the extension from them in the current directory recursively.
for filename in `find . -type f -name '*.*'`; do mv "$filename" "${filename%.*}"; done
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 | sed 's/ /./g'
In this challenge there are some directories containing files with different extensions. Print all directories, one per line without duplicates that contain one or more files with a ".tf" extension.
find . -name "*.tf" -exec dirname {} \; | sort -u
There are a mix of files in this directory that start with letters and numbers. Print the filenames (just the filenames) of all files that start with a number recursively in the current directory.
find . -type f -name "[0-9]*" -printf "%f\n"
Print the 25th line of the file faces.txt
sed -n '25p' faces.txt
Print the lines of the file
reverse-me.txt
in this directory in reverse line order so that the last line is printed first and the first line is printed last.
tac reverse-me.txt
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 '!seen[$0]++' faces.txt
The file
random-numbers.txt
contains a list of 100 random integers. Print the number of unique prime numbers contained in the file.
cat random-numbers.txt | awk '
function is_prime(num) {
if (num < 2) {
return 0
}
for (i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0
}
}
return 1
}
{
if (is_prime($1)) {
print $1
}
}' | sort -u | wc -l
access.log.1
andaccess.log.2
are http server logs.Print the IP addresses common to both files, one per line.
comm -12 <(awk '{print $1}' access.log.1 | sort -u) <(awk '{print $1}' access.log.2 | sort -u)
Print all matching lines (without the filename or the file path) in all files under the current directory that start with "access.log", where the next line contains the string "404".
Note that you will need to search recursively.
卡住了,没有找到正确答案,未完待续…