Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
The most common shell (and default on OS X) is 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 targets
Don’t forget to hit enter at the end of each command.
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/Photos/grumpycat.jpg
Absolute: /Users/paigecrum/Desktop/Photos/grumpycat.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.
Feel free to use this whenever things get too cluttered.
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”)
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 cd
Tab completion autocompletes commands and filenames.
Examples:
README .bashrc index.html 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 on a Mac:
$ 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: