Notizen
Command line HOW TO USE
Inhalt der Notizen (1)
Seite 1
TO NAVIGATE THE FILESYSTEM
pwd outputs the name of the current working directory.
ls lists all files and directories in the working directory.
cd switches you into the directory you specify.
mkdir creates a new directory in the working directory.
touch creates a new file inside the working directory.
VIEWING AND CHANGING THE FILESYSTEM
Options modify the behavior of commands:
ls -a lists all contents of a directory, including hidden files and directories
ls -l lists all contents in long format
ls -t orders files and directories by the time they were last modified
Multiple options can be used together, likels -alt
From the command line, you can also copy, move, and remove files and directories:
cp copies files
mv moves and renames files
rm removes files
rm -r removes directories
Wildcards (*) are useful for selecting groups of files and directories
REDIRECTING INPUT & OUTPUT
Redirection reroutes standard input, standard output, and standard error.
The common redirection commands are:
> redirects standard output of a command to a file, overwriting previous content.
>> redirects standard output of a command to a file, appending new content to old content.
redirects standard input to a command.
| redirects standard output of a command to another command.
A number of other commands are powerful when combined with redirection commands:
sort: sorts lines of text alphabetically.
uniq: filters duplicate, adjacent lines of text.
grep: searches for a text pattern and outputs it.
sed : searches for a text pattern, modifies it, and outputs it.
With more details:
The echo command accepts the string "Hello" asstandard input, and echoes the string "Hello" back to the terminal as standard output.Let's learn more about standard input, standard output, and standard error:
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 reroutes standard input, standard output, and standard error to or from a different location.
> takes the standard output of the command on the left, and redirects it to the file on the right. Note that > overwrites all original content in the file where the output is redirected to.
>> takes the standard output of the command on the left and appends (adds) it to the file on the right. You can view the output data of the file with cat and the filename.
takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat command. The standard output appears in the terminal.
| 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. You can think of this as "command to command" redirection. Multiple |s can be chained together.
wc command outputs the number of lines, words, and characters in a .txt file, respectively.
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.
grep stands for "global regular expression print". It searches files for lines that match a pattern and returns the results. It is also case sensitive. -i enables the command to be case insensitive. grep -R searches all files in a directory and outputs filenames and lines containing matched results. -Rstands for "recursive". grep -Rl searches all files in a directory and outputs only 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".
Let's look at the expression 's/snow/rain/':
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.
NANO & ENVIRONMENT VARIABLES
The environment refers to the preferences and settings of the current user.
The nano editor is a command line text editor used to configure the environment.
~/.bash_profile is where environment settings are stored. You can edit this file with nano.
environment variables are variables that can be used across commands and programs and hold information about the environment.
export VARIABLE="Value" sets and exports an environment variable.
USER is the name of the current user.
PS1 is the command prompt.
HOME is the home directory. It is usually not customized.
PATH returns a colon separated list of file paths. It is customized in advanced cases.
env returns a list of environment variables.
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.
The command nano hello.txt opens a new text file namedhello.txt in the nano text editor.
"Hello, I am nano" is a text string entered in nano through the cursor.
The menu of keyboard commands at the bottom of the window allow us to save changes to hello.txt and exit nano. The ^ stands for the Ctrl key.
Ctrl + O saves a file. 'O' stands for output.
Ctrl + X exits the nano program. 'X' stands for exit.
Ctrl + G opens a help menu.
clear clears the terminal window, moving the command prompt to the top of the screen.
~/.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 command source ~/.bash_profileactivates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session,source makes the changes available right away in the session we are in.
alias pd="pwd"
The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.
Here alias pd="pwd" creates the alias pd for the pwd command, which is then saved in the bash profile. Each time you enter pd, the output will be the same as the pwdcommand.
The command source ~/.bash_profile makes the alias pd available in the current session.
Each time we open up the terminal, we can use the pd alias.
history command : the command line outputs a history of commands that were entered in the current session.
environment variables are variables that can be used across commands and programs and hold information about the environment.
The line USER="Jane Doe" sets the environment variable USER to a name "Jane Doe". Usually the USER variable is set to the name of the computer's owner.
The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
At the command line, the commandecho $USER returns the value of the variable. Note that $ is always used when returning a variable's value. Here, the commandecho $USER returns the name set for the variable.
PS1 is a variable that defines the makeup and style of the command prompt.
export PS1=">> " sets the command prompt variable and exports the variable. Here we change the default command prompt from $to >>.
After using the source command, the command line displays the new command prompt.
The HOME variable is an environment variable that displays the path of the home directory. Here by typing echo $HOME, the terminal displays the path /home/ccuser as output. You can customize the HOME variable if needed, but in most cases this is not necessary.PATH is an environment variable that stores a list of directories separated by a colon. Each directory contains scripts for the command line to execute. The PATH variable simply lists which directories contain scripts.
The env command stands for "environment", and returns a list of the environment variables for the current user.
Informationen
- Beschreibung
- Key Skills Programming Notiz am Command line HOW TO USE, erstellt von Alyona Sydorova am 13/12/2015.
- Erstellt von
- Alyona Sydorova
- Aufrufe
- 1
- Tags
- Bewertung