Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
@mo_mack
A text-based command interpreter
The most common shell is called BASH
For OS X, use a shell via the “Terminal” app
Usually shows your username and computer name (hostname).
It indicates that the shell is ready for you to enter a command.
$ command -options argument
Don’t forget to hit enter at the end of each command.
echo
Type echo hello
to print hello to the screen
man
The man
command brings up the manual for the specified command. Use <space> or the arrow keys to page through and press q to exit.
$ man ls
Tab completion autocompletes commands and filenames.
$ cd P
pwd
Type it whenever you want to see what directory (folder) you’re in.
Also referred to as "folders".
Nested files and directories can be referenced using paths.
Each directory or file is separated by a forward slash "/".
Relative:
Desktop/dougthepug.jpg
Absolute:
/Users/anastasialanz/Desktop/dougthepug.jpg
In OS X, /home/you
becomes /Users/you
Varies between systems, but some common folders:
cd
Use the cd
command to change directories.
Expects a file path as an argument.
If no file path is given, it assumes your home directory (~) by default.
There are 2 other ways that we could have gotten to our home directory...what are they?
clear
The clear
command clears the contents of the terminal and issues a prompt.
$ clear
Option + L will also clear the terminal.
ls
Use the ls
command to list the contents of a directory.
Expects a file path as an argument.
If no file path is given, it assumes the current directory by default.
Flags are preceded by a hyphen, and are often a single character.
$ ls -a
$ ls -l
Notice that hidden file names begin with a "."
You can combine flags or list them separately separated by spaces. (e.g. “ls -a -l”)
Examples:
README .bashrc index.html index.html.old Bugs List.txt
Use the mkdir command to create a new empty directory.
Use the rmdir command to remove an empty directory.
Use rm -r to remove a non-empty directory
...be careful!
All 3 commands expect the name of a directory as an argument.
touch
Use the touch command to create a new file.
The touch command expects the name of your new file as an argument.
cp
Use the cp command to copy a file.
The cp command takes two arguments:
1st argument = the "origin" file
2nd argument = the "destination" file
$ cp resume.txt resume-copy.txt
$ cp orig dest
cp -R
Use the cp -R command to copy a directory.
The cp -R command takes two arguments:
1st argument = the "origin" directory
2nd argument = the "destination" directory
$ cp -R homework old-homework
$ cp -R orig dest
Use the mv command to move a file or directory.
The mv command takes two arguments:
1st argument = the "origin"
2nd argument = the "destination"
mv orig dest
mv orig dest
rm
Use the rm command to remove a file.
Expects the name of the file you are removing as an argument.
cat, more, less
Use the cat command to output (catenate) the contents of a file to the console.
Use the more or less commands to read a file:
will output the entire file
You can use various editors built into bash:
$ vi myfile.txt
$ emacs myfile.txt
$ pico myfile.txt
Or on a Mac, you can open with any desktop app:
$ open -a TextEdit myfile.txt
Or with the default editor:
$ open -t myfile.txt
grep
Use the grep command to search in files.
The grep command outputs only the lines in a file that match a given pattern.
The 1st argument is the pattern to match, and the 2nd, 3rd, and so on are the files to search within.
$ grep pattern file
Use the * (asterisk) symbol to match anything.
You can use this to search through all of a particular file type.
$ grep hello *.txt
The shell will build a list of all the files that match the non-asterisk part.
find
Use the find command to find files according to name/metadata.
Find all txt files under the current directory:
find . -name '*.txt' -print
By default, input comes from the screen and output goes to the screen.
You can use < and > to redirect input/output.
A practical example: Save results of search in a file:
grep hello *.txt > results.txt
Use the echo command to add a new line of text.
Use the > to replace the contents of a file.
Use the >> to add to the end of a file.
Lots more, and you can create your own.
Use the history command to see a list of all your previous commands.
Each command will be listed next to a line number.
A few history-related commands:
A command line tool for getting or sending files using URL syntax.
$ which curl
The command below will download a .txt file
$ curl -OL example.com/pugs.txt
Shows the first 10 lines of a file.
Shows the last 10 lines of a file.
This command shows newline count, word count and byte count.
The output of one command is the input of another command.
$ head cat.txt | wc
Use this command to chain multiple commands together.
$ curl -OL gdibtv.github.io/intro-to-command-line/cat.txt
Use Homebrew for MacOs.
Use apt-get for Ubuntu.
A program that generates ASCII pictures of cows with a message.
$ brew install cowsay
$ open http://girldevelopit.com
$ say i love the command line