Join Scott Simpson for an in-depth discussion in this video Reviewing common Bash commands, part of Up and Running with Bash Scripting.
You may or may not be use to working at the command line. If you are not, that's fine. In this movie, I'll provide a quick look at some of the common commands that you'll use. First, it's important to understand how to move around the directory structure. Typing pwd, prints the working directory. That is, it tells you where you currently are, the active working directory. What that means, is if you run a command, this is where the system will execute it. If you say, delete all the files here, it'll delete all the files in this current working directory. And you can see I'm in my home directory, which is where my personal files live.
Yours will be different unless you're named Scott, and you're using a Linux machine. On Macs, your home folder will be inside the slash users folder at the root of the drive, and on Linux, it'll be in slash home. You can check out what's in a directory with the LS command, which lists the files and folders in the current working directory. Here I've got some folders called Trees, Fruit and Pets. To see what's in one of those folders, I can use the LS command, followed by a space and the name of that folder. And I can ask LS to show me the difference between files and folders, using the dash L option. So here I can see I have a directory called Cedar, and I know it's a directory, because it has a D at the beginning of this line here.
Then I have a file called maple.txt, and there's no indication that it's a directory. Then a directory called pine, and a file called pine. A directory called spruce, a directory called sycamore, and a file called tree.png. There's a whole lot of other stuff here,rwx, rwx, rwx. Those are the permissions for the file. And we don't have time to get into that now, but I encourage you to check out Kevin Skoglund's course, Unix for Mac OS X Users, to understand more about permissions and ownership, and groups. There's all kinds of options that you can use with LS and you can look those up using the MAN command, MAN LS.
What MAN does, is show you what are called the manual pages, and they provide a comprehensive guide to almost any command that you can think of. You just press the Spacebar to page through. If you're feeling adventurous, you can press H, which brings up the reference for navigating around. And when you're done, just press Q. So I can create a directory or folder using mkdir, for make directory. I'll make one called People. And I can check that out. And here it is, directory People. And of course I can delete a directory with rmdir. So let's get rid of that people directory. Now we'll use the CLEAR command here, just to clear the screen.
Everything that I've done isn't gone, it's just in the scroll back up here, but it makes my terminal window a little nicer to look at. So, let's move into that Trees directory. To do that, I'll use the CD command to change directory. And I can run LS and see the files in this directory. I can make a copy of the file with the CP command, followed first by the file I want to copy, and then the name of the file I want to copy it to. In this case, I've copied maple.txt to new_maple.txt. And here it is. Of course I can remove the file with the rm command. And as you can see, it's been deleted.
I'll clear the screen again. In the last set of commands that I want to show you in this quick introduction, let you see what's in a file. Let's take a look at what's inside of the pine.txt file with the cat command. Cat has some other functionality. It's designed both to concatenate or stick files together, and to print their contents to the screen. Line by line and dumps out everything. Keep in mind that what you see will only make sense if the file is a text file. If it's a binary file, like an image or an executable, for example, the file tree.png, you'll get what looks like a whole bunch of gibberish. Those noises heard weren't an error, they're actually a character called bell.
And I'll clear the screen again. You can use the more command to paginate a long file. That is, to pause printing out of the file page by page so you can read it a little easier. And here you can press Spacebar to move through the pages. And then Q when you're done. You can also peek at the beginning of a file with head. And that just shows you the first couple lines, make some space here. You can also look at the end of the file with tail. This shows you the last couple of lines of the file. This is really handy for log files. So, there's hundreds of commands that you can use at the dash prompt. And for more detail on these and others, I encourage you to check out Kevin Skoglund's course here on lynda.com called Unix for Mac OS X Users.
The first eight chapters cover bash commands that are relevant to both Mac users and Linux users.
- What is Bash?
- Managing output with grep, awk, and cut
- Understanding Bash script syntax
- Creating a basic Bash script
- Displaying text with "echo"
- Working with numbers, strings, and arrays
- Reading and writing text files
- Working with loops
- Using functions
- Getting user input during execution
- Ensuring a response