Command Line

All references: https://www.codecademy.com/articles/command-line-commands

  • ls: the command line looks at the folder you are in, and then “lists” the files and folders inside it.
    • ls -a: -a modifies the behavior of the ls command to also list the files and directories starting with a dot (.). Files started with a dot are hidden, and don’t appear when using ls alone.
    • -a - lists all contents, including hidden files and directories
    • -l - lists all contents of a directory in long format
    • -t - order files and directories by the time they were last modified.
    • ls -alt: lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.
  • pwd: stands for “print working directory”. It outputs the name of the directory you are currently in, called the working directory.
  • cd: stands for “change directory”. cd changes the working directory.
  • cd ..: to move up one directory
  • mkdir: stands for “make directory”. It takes in a directory name as an argument, and then creates a new directory in the current working directory.
  • touch: creates a new file inside the working directory. It takes in a filename as an argument, and then creates an empty file in the current working directory.
  • cp: copies files or directories. Here, we copy the contents of frida.txt into lincoln.txt.
    • To copy a file into a directory, use cp with the source file as the first argument and the destination directory as the second argument. cp biopic/cleopatra.txt historical/
    • wildcards: In addition to using filenames as arguments, we can use special characters like * to select groups of files. These special characters are called wildcards. cp m*.txt scifi/
  • mv: To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument. mv wonderwoman.txt batman.txt superhero/
    • To rename a file, use mv with the old file as the first argument and the new file as the second argument. mv batman.txt spiderman.txt
  • rm: command deletes files and directories.
    • -r stands for “recursive,” and it’s used to delete a directory and all of its child directories. rm -r comedy
  • echo: can accept the string as standard input, and echoes the string back to the terminal as standard output.
    • standard input, abbreviated as stdin, is information inputted into the terminal through the keyboard or input device.
    • standard output, abbreviated as stdout, is the information outputted after a process is run.
    • standard error, abbreviated as stderr, is an error message outputted by a failed process.
  • redirection:
    • > command redirects the standard output to a file. echo "Hello" > hello.txt
    • >> takes the standard output of the command on the left and appends (adds) it to the file on the right.
    • < takes the standard input from the file on the right and inputs it into the program on the left.
    • | is a “pipe”. The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right.
  • cat: outputs the contents of a file to the terminal. cat oceans.txt > continents.txt
  • wc: outputs the number of lines, words, and characters of a txt.
  • sort: takes the standard input and orders it alphabetically for the standard output.
  • uniq: stands for “unique” and filters out adjacent, duplicate lines in a file.
    • A more effective way to call uniq is to call sort to alphabetize a file, and “pipe” the standard output to uniq. sort deserts.txt | uniq
  • grep: stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. grep Mount mountains.txt Here, grep searches for “Mount” in mountains.txt.
    • grep -i enables the command to be case insensitive. Here, grep searches for capital or lowercase strings that match Mount in mountains.txt. grep -i Mount mountains.txt
  • grep -R: searches all files in a directory and outputs filenames and lines containing matched results. -R stands for “recursive”.
    • grep -Rl Arctic /home/ccuser/workspace/geography l stands for “files with matches”. Here grep -Rl searches the /home/ccuser/workspace/geography directory for the string “Arctic” and outputs filenames with matched results
  • sed: stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find and replace”.
    • sed 's/snow/rain/' forests.txt
    • s: stands for “substitution”. it is always used when using sed for substitution.
    • snow: the search string, the text to find.
    • rain: the replacement string, the text to add in place.
    • In this case, sed searches forests.txt for the word “snow” and replaces it with “rain.” Importantly, the above command will only replace the first instance of “snow” on a line.
    • sed 's/snow/rain/g' forests.txt
    • The above command uses the g expression, meaning “global”. Here sed searches forests.txt for the word “snow” and replaces it with “rain”, globally. All instances of “snow” on a line will be turned to “rain”.
  • nano: is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.
    • nano hello.txt opens a new text file named hello.txt in the nano text editor.
  • Bash Profile: ~/.bash_profile is the name of file used to store environment settings. It is commonly called the “bash profile”. When a session starts, it will load the contents of the bash profile before executing commands.
    • The ~ represents the user’s home directory.
    • The . indicates a hidden file.
    • The name ~/.bash_profile is important, since this is how the command line recognizes the bash profile.
    • source to activate the changes to the bash profile for the current session. source to activate the changes to the bash profile for the current session.
  • alias: allows you to create keyboard shortcuts, or aliases, for commonly used commands. alias pd="pwd"
  • environment variables are variables that can be used across commands and programs and hold information about the environment.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章