Linux

Solution of cmdchallenge.com

I write the solution of cmd challenge to learn. 😉 i hope you will enjoy it. if you love this please feedback and share to your friends.

Question – hello_world/

Ans echo “hello world”

Question – current_working_directory/
Ans

pwd

Question – list_files/
 Ans 

ls

Question – print_file_contents/
 Ans 

cat access.log

Question – last_lines/
 Ans 

tail -n 5 access.log

Question – find_string_in_a_file/
 Ans 

cat access.log | grep “GET”

Question – search_for_files_containing_string/
 Ans 

grep -Ril “500” . | cut -c 3-

Question – search_for_files_by_extension/
 Ans 

find . -name “access.log*”

Question – search_for_string_in_files_recursive/
 Ans 

cd var/log/httpd;cat * | grep -i 500

Question – extract_ip_addresses/
 Ans 

cd var/log/httpd/;grep -o ‘[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}’ access.log*

Question – delete_files/
 Ans 

find . -mindepth 1 -delete

Question – count_files/
 Ans 

ls -1 | wc -l

Question – simple_sort/
 Ans 

cat access.log | sort

Question – count_string_in_line/
 Ans 

cat access.log | grep -i GET | wc -l

Question – split_on_a_char/
 Ans 

cat split-me.txt | grep -o ‘[^;]\+’

Question – print_number_sequence/
 Ans 

seq -s ‘ ‘ 1 100

Question – remove_files_with_extension/
 Ans 

find . -name “*.doc” -type f | xargs rm -f

Question – replace_text_in_files/
 Ans 

find . -type f -name “*.txt” -exec sed -i ‘s/challenges\ are\ difficult/bar/g’ {} +

Question – sum_all_numbers/
 Ans 

awk ‘{ sum += $1 } END { print sum }’ sum-me.txt

Question – just_the_files/
 Ans 

find . -type f | awk -F”/” ‘{print $NF}’

Question – remove_extensions_from_files/
 Ans 

find pwd -type f -exec bash -c ‘mv “$1” “${1%.*}”‘ – ‘{}’ \;

Question – replace_spaces_in_filenames/
 Ans 

ls | sed ‘s/ /./g’

Question – files_starting_with_a_number/
 Ans 

find . -type f -printf “%f\n” | grep “^[0-9]” | awk -F”/” ‘{print $NF}’

Question – print_nth_line/
 Ans 

sed -n ’25p’ faces.txt

Question – reverse_readme/
 Ans 

awk ‘{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j–] }’ README

Question – remove_duplicate_lines/
 Ans 

awk ‘!seen[$0]++’ faces.txt

Question – corrupted_text/
 Ans 

< war_and_peace.txt tr -s ‘!’ | sed ‘s/!\([a-z]\)/\1/g’ | sed ‘s/!\( [a-z]\)/\1/g’ | sed ‘s/!\.!/./g’ | sed ‘s/ !/ /g’

or

sed -e ‘s/!! !/! /g’ -e ‘s/sa!ve/save/g’ -e ‘s/gracious!/gracious/g’ -e ‘s/!.!/./g’ -e ‘s/!!thing/thing/g’ -e ‘s/he!/he/g’ -e ‘s/will!/will/g’ -e ‘s/in!/in/g
‘ -e ‘s/!will/will/g’ -e ‘s/villain../villain!/g’ -e ‘s/in Our/in! Our/g’ war_and_peace.txt
Previous Post Next Post

You Might Also Like

No Comments

Leave a Reply