CMD-challenge

This repository contians my solutions to the Commandline Challenge, a project to solve common tasks in one line bash commands. Since possible solutions are already available on the official GitHub repository there is no harm in disclosing mine.

It's a great project and I encourage everyone to solve the tasks without looking at the solutions! If you do look, I hope you learn something.

Interface

hello_world:

$ echo "hello world"

current_working_directory:

$ pwd

list_files:

$ dir

list_files_adv:

$ for f in $(ls -a); do if [ -d $f ] ; then echo $f/ ; elif [ -L $f ] ; then echo $f@ ; elif [ -x $f ] ; then echo $f"*"; else echo $f ; fi; done

nested_dirs:

$ cat .../\ \ /.\ .the\ flag.txt

print_file_contents:

$ cat access.log

last_lines:

$ tail -5 access.log

find_string_in_a_file:

$ grep "GET" access.log

find_tabs_in_a_file:

$ grep -P -c '\t' file-with-tabs.txt

search_for_files_containing_string:

$ grep -l 500 * 

search_for_files_by_extension:

$ find . -iname "access.log*"

search_for_string_in_files_recursive:

$ find . -iname "access.log*" -exec grep 500 {} \;

extract_ip_addresses:

$ find . -iname "access.log*" -exec grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' {} \; 

delete_files:

$ rm -rfv * .*

count_files:

$ ls | wc -l

simple_sort:

$ cat access.log | sort

count_string_in_line:

$ grep GET access.log | wc -l

split_on_a_char:

$ cat split-me.txt | sed 's/;/\n/g'

print_number_sequence:

$ echo $(seq 1 100)

remove_files_with_a_dash:

$ find . -type f -iname "-*" -exec rm {} \;

print_sorted_by_key:

$ cat ps-ef* | sort -u -k2 -n

IPv4_listening_ports:

$ cat netstat.out | grep tcp | grep -v tcp6   | cut -d ':' -f 2 | cut -f 1 -d ' ' | sort -unr

remove_files_with_extension:

$ find . -type f -iname "*.doc" -exec rm {} \;

remove_files_without_extension:

$ find . -type f ! \( -name '*.exe' -o -name '*.txt' \) -exec rm {} \;

replace_text_in_files:

$ find . -type f -iname "*.txt" -exec sed -i 's/challenges are difficult//g' {} +

sum_all_numbers:

$ echo $(cat sum-me.txt) | sed 's/ /+/g' | bc

just_the_files:

$ find . -type f  -exec basename {} \;

remove_extensions_from_files:

$ for f in $(find . -type f); do mv $f ${f%*.*}; done

replace_spaces_in_filenames:

$ ls  | sed 's/ /./g'

files_starting_with_a_number

$ find . -type f -iname "[0-9]*" -exec basename {} \; 

print_nth_line:

$ head -25 faces.txt | tail -1

reverse_readme:

$ cat README | tac

remove_duplicate_lines:

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

corrupted_text:

$ cat war_and_peace.txt | sed 's/! !/ /g' | sed 's/sa!ve/save/g' | sed 's/ous!/ous/g' | sed 's/!.!/./g' | sed 's/!!thing/thing/g' | sed 's/he!/he/g' | sed 's/ll!/ll/g' | sed 's/ain..$/ain!/g'  | sed 's/!wi/wi/g'

print_common_lines:

$ for ip in $(grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' access.log.1); do grep $ip access.log.2| cut -d ' ' -f1; done   

print_line_before:

$ find . -iname "access.log*" | xargs grep -h -B 1 --no-group-separator 404  | grep -v 404

print_files_if_different:

$ for f in $(find . -iname "*.bin"); do if ! diff base.bin $f >/dev/null; then basename $f; fi; done