Command line

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

what are -a , -l , and -t ?

"options" Or, modifiers of commands These options are options of the ls command

What does the s stand for in the 's/Hopsin/Kanye'?

"substitute"

How do you append files?

$ cat glaciers.txt >> rivers.txt * You will see both rivers and glaciers as output * This method of redirect will not erase the first file contents.

how to use pipe '|' to do a command in one line with a redirect?

$ cat lakes.txt | sort > sorted-lakes.txt Here, the command takes the standard output from cat lakes.txt and "pipes" it into sort. The standard output of sort is redirected to sorted-lakes.txt.

If you're in an environment, how do you exit?

$ clear

How do you do a redirect?

$ echo "Hello" > hello.txt $ cat hello.txt Hello What is happening? - "Hello" was entered as standard input (stdin) - The > command redirects the standard output (stdout) to a file. - The new file is hello.txt - The cat command outputs the contents of the file to the terminal

How would you use grep to search for "Mount" in mountains.txt?

$ grep -i Mount mountains.txt

How to open a new file in nano?

$ nano ~/.bash_profile

How to use sed for "find and replace", GLOBALLY?

$ sed 's/snow/rain/g' forests.txt the 'g' stands for "global" ** ALL instances of "snow" on a line will be turned to "rain"

list indices work the same as string indices...

- Any integer expression can be used as an index - If you try to read / write an elem that does not exist, you get an IndexError - If an index has a negative value, it counts backward from the end of the list.

What is the more effective way to use sort and uniq?

- Call sort to alphabetize a file - "pipe" the standard output to uniq This way, all the duplicate files become adjacent, so they can be filtered out. $ sort deserts.txt | uniq > uniq-deserts.txt (then, view with the cat command)

Things you can do with command line

- run programs - write scripts to automate tasks - combine simple commands for more complicated tasks

cp

Copy file or directory format: cp filet.txt file2.txt cp file1.txt folder/ (where the first filename is the file you want to copy, and the second filename is the destination--it can be a file or folder)

What does this command do? touch media/popular.txt

Creates a file named popular.txt in the media directory

standard input (stdin)

Information inputted into the terminal through the keyboard or input device

What happens when you type $ nano hello.txt

Opens up a text editor This is an ENVIRONMENT, which has uniqe things about it

grep -R

Option R stands for "recursive" This option allows you to search for a string, and it outputs filenames and lines with matched results.

grep -Rl

Option Rl stands for "recursive, files with matches" Here, grep -Rl searches the given directory for the string "Arctic" and outputs filenames with matching results.

What is "redirection"??

Redirection REROUTES standard input, standard output, and standard error to or from a different location

How do you set the bash profile?

The command nano ~/.bash_profile opens up ~/.bash_profile in nano. The text echo "Welcome, Jane Doe" creates a greeting in the bash profile, which is saved. It tells the command line to echo the string "Welcome, Jane Doe" when a terminal session begins. The command source ~/.bash_profile activates 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 command

This command allows you to create keyboard shortcuts, or aliases, for commonly used commands $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 pwd command Then, the command $ source ~/.bash_profile makes the alias pd available in the current session.

echo What is going on behind the scenes?

This command repeats what you said. $ echo "Hello" Hello Behind the scenes: The echo command accepts the string "Hello" as standard input, and echos the string "Hello" back to the terminal as standard output

| (a "pipe")

This command takes the std output on the left, and PIPES it as a std input to the command on the right "command to command" redirection multiples pipes can be chained together ex. $ cat volcanoes.txt | wc | cat > islands.txt

redirection of input and output (I/O)

Until now, we have run commands in the command line and received a stream of output in the terminal. Now, through redirection, you can: - direct the input and output of a command to and from other files and programs, and - chain commands together in a pipeline

>>

Used if you don't want to erase the contents of the output file. APPEND the stdout on the left to the file on the right Again, you can view the output data with $ cat filename

USER =

Usually this environment variable is set to the name of the computer's owner export USER='Olivia' * "export" makes the variable available to all child sessions initiated from the session you are in. echo $USER returns the value of the variable

Does > overwrite all original content in the stdout (output) file?

Yes When you view all the output data (using $cat), you will see only the contents you just redirected.

How do you use cp to move multiple files to the same new file?

You can also have more than one file to move, if you'd like to do it in one line... cp current_folder/file1.txt current_folder/file2.txt destination_folder/

What would happen if you took the /bin/ directory out of your $PATH?

You could no longer use commands in /bin/

Repeating a list a given number of times

[0] * 4 >> [0, 0, 0, 0]

nested list counts as a single element

[1, 2, [3, 4, 5]] # Has 3 elements not 5

*

a "wildcard" selects all files in the working directory If you just want the ones that start with m: m*.txt

How to concatenate lists

a = [1, 2, 3] b = [4, 5, 6] c = a + b c >> [1, 2, 3, 4, 5, 6]

What separates directories in your $PATH vairable?

a colon (:)

ls

a command lists files and directories that are in the folder you are in

cat $ cat hello.txt

a command that displays the contents of a file $ cat hello.txt

mv

a command that moves a file into the folder specified Syntax: $ mv file.txt folder/ (source file is first argument and destination is second argument) (like cp, you can have multiple filenames, too, separated by spaces)

sort

a command that sorts items in a file in alphabetical order. $ sort lakes.txt (The contents of the file are sorted)

PS1

a variable that defines the makeup and style of the command prompt export PS1=">> " sets the command prompt variable and exports the value (changes from default $ to >>)

filesystem

all the directories on your computer

what do you call a file, directory, or program that is passed to a command?

an argument

HOME

an envirnoment variable that displays the path of the home directory $ echo $HOME returns /home/ccuser

PATH

an environment variable that stores alist of directories separated by a colon Lists all those that contain scripts Can be customized to add scripts of your own!

standard error (stderr)

an error message outputted by a failed process

rm -r

an option for the remove command that stands for "recursive" It's used to delete a directory and all its child directories be careful! It's permanent

directive

another word for command

How would you change to one directory above the current working directory?

cd ..

What if you want to change directories to the grandparent, and then navigate down two more, so you're in a cousin?

cd ../ ../2014/dec

cd ..

change directories to the parent (go "up" in the filesystem)

cd ../feb

change directories to the parent's parent and beyond (just specify the foldr by name)

hard links

child directories and files This number includes the parent directory link (..) and current directory link(.)

the clear command

clears the terminal of text

cd

command - "change directories" changes to the directory you specify

pwd

command - "print working directory" outputs the name of the directory you are currently in

touch

command to create a new empty file inside the current working directory Syntax: $ touch filename.txt

ls-alt

command you can do it you want all three things: - list hidden files, - long format, - and ordered by last modified

(.)

current directory link

What is happening here? $ cat volcanoes.txt | wc | cat > islands.txt

each standard output of the command on the left is piped into the next command on the right

Append the string "Aesop Rock" to the file greatest.txt

echo "Aesop Rock" >> greatest.txt

how would you display the value of a single environment variable?

env | grep PATH Here, the standard output of env is Piped to the grep command. grep then searches for the value of the variable PATH, and outputs it to the terminal.

in operator for lists

ex. cheeses = ['Cheddar', 'Edam', 'Gouda'] 'Edam' in cheeses >> True 'B' in cheeses >> False

How to use grep to search within a directory?

grep -R Arctic/home/ccuser/workspace/geography Just use the directory. Option R will search the directory you gave.

What command searches all files recursively for Gambino?

grep -R Gambino.

How would you search the tile "greatest.txt" for "Earl" then send the results to the file "oddfuture.txt"

grep Earl greatest.txt >> oddfuture.txt

What are details about the nano environment?

he command nano hello.txt opens a new text file named hello.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.

Appendix of all commands

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

--help

if you write: $ cp --help It will list all the info you need about the command / arguments!

Bash Profile ~/.bash_profile

is the name of the file used to store environment settings 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

ls -l

lists all contents of a directory in a long format (7 columns, separated by spaces: - Access rights - no. of hard links (child directories and files) - owner - group name - size (bytes) - date & time modified - filename or directory name

mkdir media

make directory command that makes a new directory with the name specified

What does this command do? $ cp m*.txt scifi/

makes a copy of all text files starting with the letter m! (* is a wildcard)

Using export in your bash script...

makes the variable available to all sub programs of the current shell.

How would you rename a file as another file?

mv old_name.txt new_name.txt (in effect, using the move command makes the old filename become the new filename)

(..)

parent directory link

ls -t

puts files and directories in order, based on the time they were last modified

How do you print the directory you're currently in?

pwd

rm

remove this command removes what ever file is specified

Summary: Which four 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).

How do you use the source command to activate changes in the current session?

source ~/.bash_profile

How would you activate changes you made to your bash profile?

source ~/.bash_profile OR Start a new terminal session!

grep |

stands for "global regular expression point" This command searches files for lines that match a pattern, and returns the results * CaSe SeNsiTive

uniq

stands for "unique" This command filters out adjacent, duplicate lines in a file

The echo command takes a string and prints that string as

stdin, stout

<

takes the stdin input from the right and inputs it into the program on the left! reverse $ cat < lakes.txt Here, lakes.txt is the std input for the cat command. The std output appears in the terminal.

directories

terminology that means folders

what does grep -i do?

the "option i" enables the command to be case insensitive

ls -a

the -a modifies the behavior of ls command to also list the files and directories starting with a dot (.) Normally, files starting with a dot are hidden

What keyboard key helps you autocomplete commands and names?

the Tab key

What is the ~ short for?

the home directory

standard output (stdout)

the information outputted after a process has run

root directory

the parent of all the directories and files in the filesystem each directory has a parent/child relationship

environment

the settings and preferences that make up the command line environment Can be configured to support the commands & programs we create. Thus, we can customize greetings, command aliases, and create variables

What keys on the keyboard allow you to navigate through previous commands?

the up and down arrow keys

sed aka "stream editor"

this command accepts std input, modifies it based on an expression, then displays it as output data. Similar to "find and replace" $ sed 's/snow/rain/' forests.txt This will search the for "snow", replace with "rain" *s stands for "substitution" and is always used with using sed for substitution ** The above command will only replace the FIRST instance of "snow" on a line

env

this command stands for "environment" Returns a list of the environment variables

environment variables

variables that can be used across commands and programs They hold info about the environment

when is $ used?

when returning a variable's value ex. $ echo $USER name

Full method to redirect a file

1. list files in current directory $ ls -l 2. Decide what stdin you want to redirect to stdout $ cat oceans.txt > continents.txt 3. Use cat to view contents of the stdout $ cat continents.txt

What does this command do? $ touch shrek.txt $ cp * satire/

1. makes a new file 2. copies that file into the directory mentioned

Summary: The four 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


संबंधित स्टडी सेट्स

Saunders Ch 29 41 & 52 Practice Questions

View Set

Ch. 9: Managing Groups and Teams

View Set

Data Analysis for Managers: Chapters 9-10

View Set

Principles of Managerial Accounting - Chapter 24 Test

View Set

Quiz A : Threats, Attacks, and Vulnerabilities

View Set

Ethics Identities Science and Technology

View Set