Post

Commandline Cheatsheet

Improve your Commandline usage with these Dns and Navigational terminal commands and functions.

DNS commandline functions

NSlookup

To query the domain example.com, type:

nslookup example.com This will return all the domain information, such as

  • The IP address,
  • Name servers,
  • other related information.

Specific to see the Nameservers

You can use the nslookup command in the command line to check where a domain’s name servers are pointing. Here’s how you can do it:

1
nslookup -type=NS example.com

Dig

The dig command is a powerful tool used to query DNS servers for information about a domain name. It can be used to find out the IP address of a domain, the mail server associated with a domain, and other information.

To use the dig command, open a command line window and type in the following command:

dig example.com

You can also use the dig command to query specific types of information. For example, to query the mail server associated with a domain, type in the following command:

1
2
3
dig example.com MX
dig example.com A
dig example.com NS

This will return the mail server associated with the domain.

You can also use the dig command to query other types of information, such as the nameservers associated with a domain. For more information, type in the following command:

1
dig -h

Host

The DNS host command is used to look up the IP address associated with a domain name. To use the DNS host command on the command line, open a terminal window and type the following command:

1
host https://example.com

Replace example.com with the domain name you want to look up.

The command will return the IP address associated with the domain name.

Whois

The whois command is used to look up information about a domain name or IP address.

To use the whois command, open a command line interface (CLI) such as the Windows Command Prompt or the Mac OS X Terminal.

At the prompt, type the command whois followed by the domain name or IP address you want to look up.

For example, to look up information about the domain name example.com, type the following command:

1
whois example.com

Find out who the DNS registrar is.

The who is command is also good to find out who the DNS register is. This will provide the name of the registrar and the person who registered the domain name.

1
whois example.com

Navigation terminal commands

Go to start of line

Cursor to the start of the line

Ctrl a

Go to end of line

Cursor to the end of the line

Ctrl e

Clear prompt

Ctrl + U: Clears the entire line before the cursor.

Change directory

  • cd: change directory
    • cd a/: change directory to a (which is a directory in my current directory)
    • cd /opt/: change directory to opt (full path start with /)
    • cd ../:one directory up
    • cd ./: this directory
    • cd: return home directory for your user

Ctrl + K: Clears the entire line after the cursor. Ctrl + W: Deletes the word before the cursor. Ctrl + A: Moves the cursor to the beginning of the line. Ctrl + E: Moves the cursor to the end of the line.

Repeat last command as sudo

If you forget to use your sudo prefix. Repeat the last command with sudo.

1
sudo !!

Recursively search through history of commands

ctr r

Type history then enter to see a history of commands

Built in commandline functions to help you work with text files

  • cat: concatenate files and print on the standard output
    • cat > a.txt: start a file creation
    • cat a.txt: show file content
    • cat a.txt b.txt: show files content respectively
    • cat a.txt b.txt > c.txt: create a file which contains a.txt and b.txt

Working with system Users and file permissions

  • chmod: change file mode bits
    • chmod +x a.txt: All users and groups have the execute permission
    • chmod $u$g$o a.txt: Give specific user permission
      • $u -> current user permission number
      • $g -> current user’s group permission number
      • $u -> others permission number
      • permission number can calculate with:
        • 0 -> no permission
        • 1 -> x (execute)
        • 2 -> w (write)
        • 4 -> r (read)
    • chmod 651 a.txt: it means user can write and read, group can read and execute, others can only execute.
  • cp: copy files and directories
    • cp $source $destination: copy the file from source to destionation
    • cp -R a/ b/: copy directory(folder) to other folder. (-R = -r = –recursive)
  • date: print or set the system date and time
  • echo: display a line of text, args etc.
    • echo "string": it prints out “string”
    • echo $PATH: it prints $PATH variable’s value
  • grep: print lines matching a pattern
    • cat b.txt | grep q: the lines which contains q in b.txt.
    • cat b.txt | grep q$: the lines which ends with q in b.txt.
    • cat b.txt | grep ^q: the lines which starts with q in b.txt.
    • ! for regex usage you can only specify the -E(or -e): cat b.txt | grep -E q$.
  • head: output the first part of files
    • head b.txt: first 10 lines of the b.txt (default)
    • head b.txt -n 4: first 4 lines of the b.txt
  • ls: list directory contents
    • ls: list current directory contents (only general files ignore hidden files(which starts with .))
    • ls a/: list directory a’s contents
    • ls -l: list current directory contents (long list format)
    • ls -a: list current directory all contents (do not ignore hidden files)
    • ls -la: all file in long list format
  • more: file perusal filter for crt viewing
    • more b.txt: paginate the contents of the b.txt
    • cat b.txt | more: this also paginates
  • less: opposite of more
  • mkdir: make directories
    • mkdir NEWFOLDERNAME: create a folder(directory) name NEWFOLDERNAME
  • mv: move (rename) files
    • mv $source $destination: move folder and files from source to destination (you can use it to rename a folder or file)
  • pwd: print name of current/working directory
  • rm: remove files or directories
    • rm b.txt: delete file named b.txt
    • rm -r a/: delete folder named a
  • rmdir: remove empty directories
    • rmdir a/: delete directory if and only if the directory is empty
  • export: set an environment variable
    • export VARIABLE_NAME="VALUE OF VARIABLE": it creates a variable named VARIABLE_NAME. (access name $VARIABLE_NAME)
  • sort: sort lines of text filesq
  • tail: output the last part of files
    • tail b.txt: last 10 lines of the b.txt (default)
    • tail -n 4 b.txt: last 4 lines of the b.txt
  • tar: The GNU version of the tar archiving utility (tape archive)
    • tar -tvzf foo.tar.gz: list all files name in foo.tar.gz
    • tar -zcvf archive-name.tar.gz directory-name: create a tar.gz file from directory-name
    • tar -xvzf foo.tar.gz: uncompress all files in foo.tar.gz
      • -z: using gzip program
      • -x: extract (uncompress)
      • -c: create archive
      • -v: verbose (display progress while creating archive)
      • -f: archive file name
  • ssh: OpenSSH SSH client (remote login program)
    • ssh eng1.mu.edu.tr: connect the eng1.mu.edu.tr server with SSH (it get your current username as a username in the server).
    • ssh mo.tercanli12@eng1.mu.edu.tr: connect the server with username: mo.tercanli12
  • wc: print newline, word, and byte counts for each file
    • wc b.txt: prints out the number of Lines, Words, Characters
    • wc -l b.txt: only number of lines
    • wc -w b.txt: only number of words
    • wc -c b.txt: only number of character
  • scp: secure copy (remote file copy program)
    • scp your_username@remotehost.edu:foobar.txt /some/local/directory: copy file from remote to local
    • scp foobar.txt your_username@remotehost.edu:/some/remote/directory: copy file from local to remote
  • wget: The non-interactive network downloader.
    • wget $URL: download url content from $URL
    • wget $URL -O a.txt: download url content from $URL and save it as a.txt
  • curl: transfer a URL
  • find: search for files in a directory hierarchy
  • man: an interface to the on-line reference manuals
    • man tar: manual page for tar
  • sed: stream editor for filtering and transforming text
    • sed s/one/ONE/ < a.txt: it process the file a.txt and all one become ONE. (a.txt does not change)
  • awk: pattern scanning and processing language
This post is licensed under CC BY 4.0 by the author.