|Ugh, words. I thought we were learning about using the command line.|
What is "bash"?I don't remember what it stands for. Bad-Ass Shell? Whoops, no, Bourne-again shell. Is Bourne a person? Apparently. A shell is just an interactive prompt, and before there was bash, there was just the regular "Borne shell" called sh. Bash is a unix thing, so it's used for the terminal in both OSX and Linux. On windows, you can get a unix emulator called Cygwin, but the normal windows command line is DOS. I met the guys who made Cygwin! One of them taught me to walk on stilts. Anyway.
If you've used the interactive mode of something like python, you know you can type one line or multi-line code and things run. Same exact thing in your terminal, but the language is bash!
Make your terminal sexy/pretty/aesthetically pleasing AND readable!Colors! I wanted to ramble about colors and just having fun customizing your terminal, but additionally, you should pay attention to the color of your cursor and highlight colors and make sure you can actually SEE your cursor all the time. Please don't handicap yourself by making your potentially most powerful tool hard to read. And make your scrollback hella big (in Preferences > Window on OSX).
Tab completion and a couple other shortcuts you should start using asap:Start typing and the hit tab to complete a file name or path or even an executable name! It'll complete as much as it can and then show you hints. Saves you from typing it all out yourself and is faster and less error-prone.
command-a (to go to beginning of line)
command-e (to go to the end of a line)
command-u (to delete everything you've typed on that line and start over)
up arrow to redo commands you did recently
Tell your friends to do these things. Superpowers++
"Where am I and what am I doing?" commandsThe command line can be disorienting. If I forget what I'm doing or need a twitchy thing to do while my brain processes something, I'll type ls and pwd
lsList the stuff in this directory! I usually tell where I am by what is in a directory... and to remind myself what files I might be trying to edit.
ls -l (to show details)
ls -lh (to show details in Human-readable format)
ls -lht (to sort things by Time modified)
ls *.jpg (list only files of a certain type)
list prefix_* (list only files starting with a certain prefix)
I actually put 'a's in my ls commands, like ls -lah but I don't remember what the a does. OH! It shows hidden files (see very end).
pwdPrint working directory!
cd <some other directory path>Change directory!
cd ../ to go up one level
cd ../../ to go up two levels
cd / to go to the root of your entire computer's directory structure
cd (with no argument) to go to your default home directory
cd ~ to also go to your home directory
cd ~/some_folder (to go from anywhere to a folder in your home directory)
mkdir <new_directory_name>Make a directory!
cp <file1> <file2>Copy a file!
mv <file1> <file2>Move (or rename) a file, without leaving a copy behind
rm <file>Remove/delete a file... and it's gone forever!
rm -r <directory_name> (to Recursively delete a directory and everything in it. BE CAREFUL WITH THIS ONE...)
touch <new_filename>This just makes a new file... or updates an existing file's last-modified time. Okay, touch is only kinda useful, but you can use it to make empty files to practice copying and moving around.
echo "some strings"Print out some strings or variables! Variables, mentioned below, have $ dollar signs in front of them.
cat <filename>Meow! The name comes from "concatenate" in that you can use it to print out multiple files at one in one long concatenated string, but you can also use it to print just one file. By "print" I mean show the contents of that file in the command line.
writing things to a file with > or append to end of file with >>For example...
echo "my cat is named MPEG" > catnames.txt
echo "my other cat is named JPEG" >> catnames.txt
!515 will rerun the 'cat catnames.txt' command.
!! will rerun the last command
!<letter> will rerun the last command that started with that letter
Also, the ! is called "bang"! Bang-five-one-five, Bang-bang!
grephistory | grep <part of command I ran recently but forgot>
cat <file> | grep <thing you're looking for on some line of a file>
grep helps you search through text file and history for lines containing certain words. Honest-to-god example of how I use grep (and history) every single day because I'm too lazy to remember how to get to my MySQL database server. I also use it to figure out which file contains some particular method that I'm in search of... And other things, but I will have to thing longer/harder about those other things.
|How I log into my database... I'm sure there's a more efficient way.|
Okay, we're finally getting to the heavier-duty bash commands I use a lot, but maybe I should just write a second post with fun bash commands encountered throughout the day on a day when I'm deeply immersed in it.
variablesThey're kind of awkward because you assign them one way (with an = sign and no spaces) and use them another way (with a dollar sign in front)
for loopsWhen I was making a web photo album of my photos when I was 15, I really wanted thumbnails of all the pictures. BUT I HAD NO IDEA HOW TO DO IT AUTOMATICALLY, so I did it by hand and it was a pain in the ass. These days, I would type
for i in *.jpg; do convert -resize 200x200 $i tiny_$i; done
You can also do it on multiple lines and not have the semi-colons. the "for / do / done" syntax is what is important. If you investigate bash for loops specifically, you'll get more guidance.
making pictures!See one of my very first blog posts on imagemagick and doing command line image fun times!!
a bash script .sh fileAnother real-life story. I write C++ code. Then I write Python code. The python code usually runs a bash script to run the C++ code, where the bash script does some setup and additional file mumbo jumbo that I don't want to do in C++.
|A bash script of mine that converts an image to a different "pgm" format, runs another program 'sift' to compute sift features on that image, save those features, and delete the pgm version of the image.|
You can run these bad boys by saying "sh myFunScript.sh"
.bashrc and .bash_profileThis is your personal bash configuration. It's basically a script that will run when your machine starts up to set everything up JUST THE WAY YOU LIKE IT. Like Goldilocks and those three bears. Unfortunately, I don't remember the difference between .bash_profile and .bashrc ... only that I seem to have both and sometimes one references the other.
|~/.bash_profile and ~/.bashrc|
One thing to know: the . in front means it's a seeecret hidden file and it wont show them in 'ls' unless you add the -a flag. It also wont show when you're browsing files. If you use git, it writes things in .git folders and files. Use ls -a to look for them!
flags and 'man'All these bash commands are powerful because of all the different options and flags they come with. To learn what flags are available, type 'man <command>' to get the manual or man page.
aliases, and how I made 'lady' and alias for 'man'If you look in my bash profile, you'll see some fine aliases I set up... including this tweet-inspired one:
.@kaflurbaleen That's it!! >$alias lady='man'
— Justine Sherry (@justinesherry) September 24, 2013
Okay, everything I wanted to say about bash off the top of my head is out there. Now it's time to touch (pet) some real cats.