linux commands Flashcards
How do you connect to bandit.labs.overthewire.org on port 2220 with the username bandit0?
ssh bandit0@bandit.labs.overthewire.org -p 2220
Standard Unix/Linux command used to check the information of disk usage of files and directories on a machine?
du
What are two ways to read a file named ‘-‘ ?
- cat
How do you read a file that has spaces in the filename?
place the filename in single quotes
EX: cat ‘spaces in the filename’
How do you display hidden files in a directory?
ls -a
How do you connect to bandit.labs.overthewire.org on port 2220 with the username bandit0, and using a password stored in a file named “bandit0”?
sshpass -p ‘cat bandit0’ ssh bandit0@bandit.labs.ovedrthewire.org -p 2220
How would you determine the types of files in a directory named “inhere” that all start with “-“?
file ./*
How would you read all the contents of every file within a directory named “inhere” that all start with “-“?
strings ./*
How would you search all subdirectories of the current directory for a file that matches the following characteristics:
- human-readable
- 1033 bytes in size
- not executable
find ! -executable -size 1033c
How would you search the entire server for a file that matches the following characteristics, and do not show any permission denied errors (should only return one file):
- owned by user bandit7
- owned by group bandit6
- 33 bytes in size
find / -size 33c -user bandit7 -group bandit6 2>/dev/null
Search the file “data.txt” for the line with the word “millionth”.
cat data.txt | grep millionth
How do you search the file “data.txt” for the line of text that appears only once.
cat data.txt | sort | uniq -c
After using this above command (cat data.txt | sort | uniq -c) to sort the file “data.txt” for unique lines, you have a bunch of lines that occur 10 times (shows 10 at the start of the line) and one line that occurs once. How can you remove all of the lines that start with ‘10’ to only leave the line that starts with ‘1’?
cat data.txt | sort | uniq -c | grep -v “10”
Search the file “data.txt” for the few human-readable strings that begin with several ‘=’ characters.
strings data.txt | grep “=”
What is the sign that something is Base64 encoded?
The string ends in 0, 1, 2, or 3 “=” sign
How do you decode the string of data within the file “data.txt” that is Base64 encoded?
cat data.txt | base64 -d
How do you get a password from the file data.txt, with a string where all lowercase (a-z) and uppercase (A-Z) letters have been rotated by 13 positions?
cat data.txt
– displays the text “Gur cnffjbeq vf 5Gr8L4qetPEsPk8htqjhRK8XSP6x2RHh”–
echo “Gur cnffjbeq vf 5Gr8L4qetPEsPk8htqjhRK8XSP6x2RHh” | rot13
How do you take a hexdump file and reverse the hexdump process to a readable file?
xxd -reverse data.txt > newfile
How do you uncompress the file “data.gz”?
gunzip data.gz
How do you uncompress the file “data.bz2”?
bunzip2 data.bz2
How do you uncompress a file “data.txt” that is a POSIX tar archive file type?
tar xf data.txt
How do you ssh to ‘localhost’ with user bandit14 using the key “sshkey.private” located in the current directory?
ssh -i sshkey.private bandit14@localhost
How do you netcat a password that is in /etc/bandit_pass/bandit14 to port 30000 on localhost?
cat /etc/bandit_pass/bandit14 | nc localhost 30000
How to list all contents, including permissions, and hidden files of the current directory?
ls -la
How do you make a python file named file.py executable?
chmod +x file.py
How would you make “foobar” provide the same results in the command line as “ls -la”?
Where would you make the change in order for it to persist after reboot?
alias foobar=’ls -la’
make ~/.bashrc to make it persist after reboot
How do you write text to a file?
echo Hello World > peanuts.txt
How do you write text to a file, without deleting any existing text (append new text to the end of the existing file)?
echo Hello World»_space; peanuts.txt
What is the file descriptor for stdin, stdout, and stderr?
- stdin = 0
- stdout = 1
- stderr = 2
How would you redirect the standard error of “ls /fake/directory” to a special file that will discard any input?
ls /fake/directory 2> /dev/null
What does the pipe operator “|” do?
allows us to get the stdout of a command and make that the stdin to another process.
How would you write the output of “ls -la /etc” to a file and to the screen?
ls -la /etc | tee file.txt
How do you display the first 10 lines of file.txt?
head file.txt
How do you see the last ten lines of file.txt?
tail file.txt
How do you get the unique values of a file?
uniq -u file.txt
How would get the number of times a line appears in a file?
uniq -c file.txt
How would you get lines that are duplicated in a file?
uniq -d file.txt
What information about a file does “wc” provide?
- number of lines
- number of words
- number of bytes
How would you fine all of the instances of the word “Hello” in file.txt?
grep Hello file.txt
How would you list all of the files in the /etc directory with “.conf” extension?
ls /etc | grep .conf
How would you print all of the lines of file.txt that begin with “Hello”?
grep ^Hello file.txt
How would you print all of the lines that end with “seahorse”?
grep seahorse$ file.txt
How would you print all of the lines that start with the letter “H” in file.txt?
grep H. file.txt
How would you print all of the lines that start with either “dig”, “dug”, or “dog” in file.txt?
grep d[iou]g file.txt
How would you print the lines that start with “dog” and “dug”, but not “dig”?
grep d[^i]g file.txt
Using the vim editor, how do you find all instances of the word “Hello”, and how do you advance through each instance of “Hello” found in the file?
- /Hello
- press ‘n’ to advance forward
- press ‘N’ to go backwards
Print the relative file paths, one path per line for all filenames that start with “access.log” in the current directory.
ls acc*
How would you find all files in the current directory and all subdirectories that are of the type “file”?
ls . -type f
How do you print all the files in the current and subdirectories that have the word “access” in the filename and the text “500” in them?
find -name “access*” -exec grep -h ‘500’ { } \;
Extract all IP addresses from files (recursively, all subdirectories) that start with “access.log” printing one IP address per line.
find . -type f -name “access.log*” -exec grep -oE
Regex for IP address?
([0-9]{1,3}.)[0-9]{1,3}
Extract all IP addresses from files that start with “access.log” printing one IP address per line.
find . -type f -iname “access.log*” -exec grep -Eo “([0-9]{1,3}.){3}[0-9]{1,3}” {} \;
How would you print the files in the current directory that have the text “John Williams” but NOT “John Williamson” in all text files?
grep -w “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files?
grep -wi “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on?
grep -win “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines before the searched text?
grep -win -B 4 “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines after the searched text?
grep -win -A 4 “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines BEFORE and AFTER the searched text?
grep -win -C 4 “John Williams” *.txt
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in every file in the current directory, and what line the text appears on?
grep -win “John Williams” ./*
How would you print the files in the current directory and all subdirectories (recursively) that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines BEFORE and AFTER the searched text?
grep -winr “John Williams” .
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines BEFORE and AFTER the searched text?
Just print the path and file name and not the text in the file with a a match?
grep -wirl “John Williams” .
How would you print the files in the current directory that have the text “John Williams” and “john williams”, but NOT “John Williamson” in all text files, and what line the text appears on, AND all the text that appears 4 lines BEFORE and AFTER the searched text?
Just print the path and file name and number of matches in each file, and not the text in the file with a a match?
grep -wirc “John Williams” .
Delete all of the files in the current directory including all subdirectories and their contents?
find . -mindepth 1 -delete
How do you count the number of files in the current working directory?
ls -l | wc -l
Print all files in the current directory, one per line (not the path, just the filename) that contain the string “500”.
grep 500 -lhr
Delete all of the files in this challenge directory including all subdirectories and their contents.
find . -delete
Count the number of files in the current working directory. Print the number of files as a single integer.
ls -l | wc -l
Print the number of lines in access.log that contain the string “GET”.
cat access.log | grep “GET” | wc -l
The file split-me.txt contains a list of numbers separated by a ‘;’ character.
Split the numbers on the ‘;’ character, one number per line.
cat access.log | tr
Print the numbers 1 to 100 separated by spaces.
echo {1..100}
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” -delete
The file sum-me.txt has a list of numbers, one per line. Print the sum of these numbers.
awk ‘BEGIN { sum=0 } { sum+=$1 } END { print sum }’ sum-me.txt
Print all files in the current directory recursively without the leading directory path.
ls -R | grep [A-z]
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”