UNIX Command Line

Learn Enough Command Line to be Dangerous

Codecademy list of command line commands

Tutorials Point UNIX

Some basics

  • echo: for printing
  • man: manual
  • clear (ctrl-L): for cleaning up text in the terminal.
  • exit (ctrl-D): for closing the terminal window.
  • ctrl-C: stop process.
  • ctrl-A: move to beginning of line
  • ctrl-E: move to end of line
  • ctrl-U: delete to beginning of line

Files

  • touch: Create a new file with specified name.
  • >: Output text to a new file (or overwrite an existing one). Ex: echo "something something something" > something.txt
  • >>: Appends a file with text. Will create a new file if one does not exist. Ex: echo "some more stuff" >> something.txt
  • cat: Outputs contents of file to terminal.
  • diff: Shows differences between two files. Ex. diff something1.txt something2.txt

Listing Files

ls is the basic command for listing files in a directory. Important options:

  • -l: "long form." Shows the following:
    • Access rights
    • "Hard links" -- the number of child and parent directories including the current directory
    • File owner's username
    • Name of the group that owns the file.
    • Size of the file in bytes
    • Datetime of last modification
    • The name of the file or directory
  • -a: Includes hidden files. Files that start with a dot are hidden.
  • -rtl: Used together, will show the long form of files, ranked by reverse time of modification (r reverses it, and tis for time).

Renaming, copying, deleting

  • mv: Moves a file from one location to another. Also used for renaming files. Ex: mv something.txt ./things/thing3.txt
  • cp: Works similarly to mv, but won't delete old file.
  • rm: Removes a file or directory. -r (recursive) option is useful to deleting entire tree of a directory. -f (force) will prevent a prompt from asking for confirmation of your desire to delete every file.

Inspection

  • which: shows location of application. Ex:which create-react-app ==> /usr/local/bin/create-react-app
  • !!: will repeat previous command
  • !<command>: will repeat previous call for specified application with all options and parameters supplied. Ex: !curl
  • ctrl-R: will allow you to fuzzy search for previously run commands.
  • head/tail: will show the first/last 10 lines of a file. Use -n option to specify number of lines to show.
  • wc: (word count) shows line count, word count, and byte size, in that order
  • less: more elegant way to read a file in the console than cat. Some commands:
    • up & down arrow keys -- move up and down
    • spacebar (ctrl-F) -- move forward one page
    • ctrl-B -- move back one page
    • G -- move to end of file
    • 1G -- move to beginning of file
    • /<string> -- search for string
    • n -- move to next search result
    • N -- move to previous search result
    • q -- quit
  • ps: shows active processes. Important options (don't include dashes in options for ps):
    • -a -- shows all processes, even for other uses
    • -u -- shows data for the list of usernames
    • -x -- include processes that do not have a controlling terminal
  • kill: eliminates a process (specified by pid). You're actually sending a signal to a process telling it to do something. Sending 15 will terminate it, but you can also tell it to do other things like quit (3), abort (6), or alarm clock (14). Ex: kill -15 1485
  • pkill: similar to kill, but will kill all processes with a similar name. Ex: pkill 15 -f spring
  • grep: for running regular expression searches in files. Useful options:
    • -i -- case insensitivity
    • -r -- recursive search through all files of a directory
    • -l -- searches for filenames

Pipes

Pipes are used to take the output from one command, and send it to another command.

Examples:

// Sends wc just the first 10 lines from sonnets
$ head sonnets.txt | wc

// Searches for the word "PATH" in the output from env and returns the whole line
$ env | grep PATH

Permissions

Just as in web development, you want to insure that a particular user can access and modify resources for which they're permitted, or else bad things will happen. A certain group (admins) may be allowed to view, run, and change almost any file, while other users may not, unless they're the owner.

The ls -l command shows the permissions for a file/directory/application. It show's read (r), write (w) and execute (e) permissions. The first three characters (2-4) show permissions for the file's owner, the second group of three (5-7) show permissions for the group that the file belongs to, and the last three characters (8-10) show permissions for everyone else. (Note: the first character indicates if the file is a directory, d, or not, -).

chmod

chmod changes the permissions for a file. chmod supports a symbolic mode for increasing, decreasing, or assigning permissions, and an absolute mode, which uses a number that maps to a specific set of permissions.

chown

chown changes the owner of a file. Ex: chown baronwilleford thing.txt

chgrp

chgrp is similar to chown, but for groups.

results matching ""

    No results matching ""