Terminal Commands
These are commands that I have found useful doing challenges (like those on overthewire or Hack the Box), or through personal projects. These are ordered by when I came across them, not by category.
For additional explanations, go to https://explainshell.com/
ssh into a system
ssh username@location -p n -i file_name
- ssh == secure shell, this is the keyword for accessing a remote system
- username == if you need a username to access a system
- @ == connects the username and location
- location == what system you’re accessing
- -p == optional, shows we’re specifying a port
- n == the port number
- -i == optional, shows we’re using a key to access the system
- file_name == file that holds the key
Open files with dash (-) for a name
vi ./-
- vi == opens with vim
- . == current directory
- / == path separator
Open files with spaces in their name
vi file\ name
- vi == opens with vim
- \ == signifies the following space is part of the filename
Show hidden files
ls -a
- ls == list files/directories
- -a == all… doesn’t ignore entries starting with a period (.)
Determine file type of those in this directory and defined amount of subdirectories
file */*
- file == lists file type
- */* == goes through current directory and the ones directly below
- Can go deeper (*/*/* etc.)
Find files of a certain size in current directory and all subdirectories
find . -size n[cwbkMG]
- find == search that goes through file hierarchy (will look at subdirectories)
- . == current working directory
- n == numerical size of file
- Suffixes used to notate what file size is being used:
- c == bytes
- w == two-byte words
- b == 512-byte blocks (default)
- k == kilobytes
- M == Megabytes
- G == Gigabytes
Lists long format of files in this directory and defined amount of subdirectories
ls -l */*
- ls == lists files/directories
- -l == long listing format
- Permissions (read, write, execute)
- Owner of file and group owner (root, specific user, etc.)
- File size
- Modification or creation time (whichever is most recent)
- File/directory name
- */* == goes through current directory and the ones directly below
- Can go deeper (*/*/* etc.)
- Not having anything will just go through current directory
Find something that exists somewhere in the entire server
find /
- find == search that will go through entire file hierarchy
- / == root directory
- Do whatever search criteria
Suppress error output (permission denied, etc.)
2>/dev/null
- 2 == redirects stderr
- 1 == redirects stdout (i.e. everything but the errors)
- > == redirects output to a file
- /dev/null == suppresses the output… throw-away bin
Print line in a textfile that contains a phrase/word/etc.
grep "word" file.txt
- grep == searches file for lines that match given pattern. Default returns entire line
- “word” == pattern you want grep to search for
- file.txt == file you want to search
Output text that is never repeated in a file
sort file.txt | uniq -u
- sort == puts elements in file in ascending order
- | == pipes the output of the first section to the second section
- uniq == removes repeats so only one remains (i.e. aaabcc becomes abc)
- Only if the repeat occurs directly after (why you sort first)
- -u == removes elements that had repeats
Find specific printable characters in a file
strings file.txt | grep 'x'
- strings == only returns printable characters
- file.txt == file name
- | == pipes output of first part to the second part
- grep == searches given file for lines that match patter
- ‘^xx’ == searches at the beginning of the line (with xx representing the pattern you’re searching for)
- ‘xx’ searches anywhere in the line (with xx representing the pattern you’re searching for)
- ‘xx$’ == searches at the end of the line (with xx representing the pattern you’re searching for)
Decode base64 encryption inside text file
base64 --decode file.txt
- base64 == keyword to encode/decode with base64 encryption
- --decode == selects the decode functionality
- file.txt == file name
Decode rot13 encryption inside text file for upper and lowercase letters
cat data.txt | tr '[n-za-m]' '[a-z]' | tr '[N-ZA-M]' '[A-Z]'
- cat == outputs contents of the file
- | == pipes output of the left into the right
- tr == translates characters (case sensitive)
- ’[ ]’ == first set of characters; those to be translated
- ’[ ]’ == second set of characters; those being translated
- So, n would translate to a and N would translate to A
Undo a hexdump
xxd -r original_file new_file
- xxd keyword to create a hexdump or reverse
- Reverse converts from hexadecimal to binary
- -r == reverses to binary
- original_file == file containing the hexdump
- new_file == file receiving the converted binary
Get data from file that has been compressed many times
This is multistep. We will first need to determine the type of compression used.
file file_name
- file == gets information about the file; here it will tell us what type of compression was used
- file_name == the compressed file
The following steps depend on what type of compression was used
If it was gzip:
zcat file_name | file -
- zcat == reads compressed data and writes decompressed data to standard output
- file_name == the compressed file
- | == pipes decompressed data to the right side
- file == will tell you if the file has another compression or is ASCII characters
- - == tells file to read standard input as a file name
If it was bzip2
bzcat file_name | file -
- bzcat == reads compressed data and writes decompressed data to standard output
- file_name == the compressed file
- | == pipes decompressed data to the right side
- file == will tell you if the file has another compression or is ASCII characters
- - == tells file to read standard input as a file name
If it was tar archive (GNU)
tar -x -O file_name | file -
- tar == Undoes layer of tar archive
- -x == extracts file from archive
- -O == outputs the extract to stdout (allows for piping)
- file_name == the compressed file
- | == pipes decompressed data to the right side
- file == will tell you if the file has another compression or is ASCII characters
- - == tells file to read standard input as a file name
Pipe the results together for multiple compressions
zcat file_name | bzcat | zcat | tar -x -O | file -
Add a new addition to piping based on the output from file -
. Continue to do this until ASCII is returned
Running the same command without file -
at the end will output the file content
Send a message to port
echo 'message' | nc address port
- echo == displays line of text
- ‘message’ == text you want displayed
- | == pipes displayed text to the right side
- nc == reads and writes tcp and udp
- destination == localhost, etc.
- port == port number you’re sending stuff to
Make a file executable
chmod +x filename
- chmod == Changes access permissions of a file
- +x == makes file an executable
Create an alias for a command
alias your_command="default_command"
- alias == Keyword that allows you to make a shortcut command
- your_command == the alias/shortcut command you’ve come up with
- default_command == the original command. Can be very long
- Example would be alias dls=”cd ~/Downloads”
Run an executable
./filename
- . == Current directory (ensures you are executing the program you mean to, not one of the same name in a different directory)
- / == Path separator