Shell Commands - kimschles/schlesinger-knowledge GitHub Wiki

Nifty Things You Can Do in the Shell

  • Create a file using echo and text redirection >

echo 'Hello World!' > say_hello.txt

  • Append lines to a file using echo and text redirection >>

echo 'How are you?' >> say_hello.txt

  • Use cat to look at the contents of one or more files

cat file1.txt file2.txt

  • Look at the last part of one or more files with tail

tail file1.txt file2.txt

  • Look at the last part of a file while it is being written

tail -f file1.txt

  • Count the number of words in one or more files with word count!

wc -w file1.txt

  • Count the number of lines in one or more files

wc -l file1.txt

  • Count the number of characters in one or more files

wc -m file1.txt

  • Count the number of bytes in one or more files

wc -c file1.txt

  • See all the information in one or more files

wc file1.txt

  • Look at your options (and add more!) for your shell

sudo nano /etc/shells

  • Change your default shell

chsh -s <path to your default shell>

echo -n 'kim schlesinger was here' | base64

  • result: a2ltIHNjaGxlc2luZ2VyIHdhcyBoZXJl

  • Decode

echo -n 'a2ltIHNjaGxlc2luZ2VyIHdhcyBoZXJl' | base64 -D

  • result: kim schlesinger was here

  • note: use -d for anything other than OSX

  • See the route tables on your machine

netstat -rn

  • SSH into an ubuntu server on port 2222 ssh -i <key> ubuntu@<address> -p 2222 -vvvv

  • add read permissions to all users (owner/user, group, others) chmod a+r

  • read only chmod 400

  • turn a group of commands into a single command that ssh can consume bash -c

  • Send a lot of requests to a url ab -n 30000 -c 100 http://<external_ip_here

  • This sends 30,000 requests, 100 at a time

  • Name server lookup nslookup <domain.com>

  • Copy something from your cli to your mac clipboard pbcopy echo $PWD | pbcopy

ls

  • list what is in a directory and their attributes

    • ls
  • list what is in the directory, and show directory ownership, permissions and sizes

    • - means it is a file
    • d means it is a directory
      • ls -l
  • show sizes in a human-readable format

    • ls -lh
  • show all files and directories, including hidden ones

    • ls -a
  • sort files by size with the biggest one at the top

    • ls -S
  • list directories recursively

    • ls -R
  • sort files by the last time they were modified

    • ls -t

Note: these flags can be combined. For example, ls -lt

  • whoami

    • shows the username of the user that is currently logged in
    • it displayes the $LOGNAME variable
  • substitute user command: su

    • su <user_name>
    • su - will change you to the root user
  • Reboot a linux system

    • reboot
  • Shutdown a linux system

    • halt or poweroff
  • Show your top running processes

    • top

Uname

Let's you see information about your kernel

  • Show the name of the kernel

    • uname
  • See the kernel's release number

    • uname -r
  • Build version

    • uname -v
  • Machine type

    • uname -m
  • Operating System

    • uname -o
    • echo $OSTYPE
  • All the info uname can show

    • uname -a
  • domain information groper (perform DNS lookups)

    • dig NS <domain_here>

Command History

  • You command history is stored in .bash_history
  • $HISTFILESIZE sets how many commands to keep in your history
  • $HISTCONTROL allows you to modify the behavior of your bash history
    • For example, ignoredups ignores duplicate commands
  • history prints out your command history
  • If you want to run a command again:
    • !<history_number> runs that command again

Shell Configuration Files

The order files are executed during login:

  1. The first file that is called during a login shell is /etc/profile
  • system-wide and startup scripts
  1. Next, the system looks for the following files and runs the first one it finds:
  • ~/.bashprofile
    • sets user specific shell envs
  • ~/.bash_login
  • ~/.profile
  1. Next, ~/.bashrc
  • non-login functions and aliases
  1. Then, /etc/bashrc
  • system-wide functions and aliases

Logout

  1. .bash_logout

Shell Variables

  • $PWD
  • $OLDPWD
  • Create a variable in your current shell:
    • NAME=Kim
  • Create a global variable that is accessible from any shell:
    • export NAME=Kim
  • Look at all your variables
    • env
  • Look at your variables in alphabetical order (this doesn't work on a mac)
    • set

Globbing

  • Use this when you don't know the exact name or location of a file
  • * is the wildcard.
    • ls *.txt searches the directory for all .txt files
    • ls test* searches for all files that have the word test
  • ? matches one character; ???? matches four characters
    • ls ????.txt searches for a text file with a name that is four characters in length
    • ls test?.txt seaches for a text file with test and one additional character
  • [xyz] matches any one of the characters in the list
    • ls [Ss]*.sh looks for files that have an s in their name
    • Find a file regarding test scores: ls [tT]est[Ss]cores20[0-9]*
  • [^abc] matches everything execpt these characters
  • [0-9] matches this range of numbers
    • 201[0-8]

Quoting

  • Double quotes " " allow $VARIABLES be evaluated
    • "My username is $USER"
    • The variable is evaluated like echo
  • Single quotes ' ' ensure the string is evaluated literally. No special behavior for $
  • \ disables special character functionality right after the \

Command Line Structure

  • kschlesinger@Kims-MacBook-Air:~ *user@computer-name:current-directory

Searching from the Command Line

  • locate searches a local database of files and folders
  • find searches the file system. Use this syntax:
    • find /path/to/directory -name file
  • whereis shows you the location of the binary and man pages (if available)

man

  • man <command_name> pulls up the manual page for that command

    • man pages are all formatted the same way

      The standard sections of the manual include:
      
      1      User Commands
      2      System Calls
      3      C Library Functions
      4      Devices and Special Files
      5      File Formats and Conventions
      6      Games et. al.
      7      Miscellanea
      8      System Administration tools and Daemons
      
      Distributions customize the manual section to their specifics,
      which often include additional sections.``` 
      
      
  • whatis <search_term> lists man pages related to the search term

    • man -f <search_term> does the same thing
  • apropos <search_term> searches the whatis database for the search term

    • shows a list of man pages that contain the search term man -k <search_term> does the same thing
  • reset

    • Reset your terminal window.
  • kubectl get pods | grep -v 3d

    • Exclude 3d from the search
  • Change into root user

    • sudo -i
  • Search for an executable (the example is openvpn)

    • sudo find / -type f -name "openvpn"
  • Find your IP address from the command line:

    • curl ifconfig.co

    Standard Output and Error

    • Standard Output, stdout, is the default. When in the command line, it is when thing are printed to the console
    • It is common to redirect to Standard Error, stderr. 2>
      • Example: ls 2> e
    • To send output to both stout and stderr use 2>&1
⚠️ **GitHub.com Fallback** ⚠️