You've already learned how to perform some basic Unix commands, but now let's run through a series of commands you'll use on a regular basis (we'll go into detail on several of these in later chapters).
To perform some basic commands:
1. | cd The cd command ( change directory ) produces no output. Used with no arguments, it tells your shell to set your "working directory" to your home directory ( /Users/ shortusername ) and "takes you home" from wherever you are. | ||
2. | pwd This command displays your present working directorywhere you "are" in the Unix filesystem. Figure 2.10 shows typical output from the pwd command. Figure 2.10. The pwd command shows your present working directorywhere you "are" in the Unix filesystem. user-vc8f9gd:~ vanilla$ pwd /Users/vanilla user-vc8f9gd:~ vanilla% | ||
3. | ls Figure 2.11 shows typical output from ls , which lists the names of files and directories. The actual output depends on what you have in your home directory. Figure 2.11. The ls command lists the names of files and directories. The actual output will depend on what you have in your home directory. user-vc8f9gd:~ vanilla$ ls Desktop Library Music Public Documents Movies Pictures Sites user-vc8f9gd:~ vanilla$ | ||
4. | echo "Hello there" The output from the echo command consists of its arguments (in this case, the words Hello there ) ( Figure 2.12 ). It also automatically adds a new line (try it with the -n option to not add the new line). Figure 2.12. The output from the echo command consists of two arguments (in this case, "Hello there" ). user-vc8f9gd:~ vanilla$ echo "Hello There " Hello There. user-vc8f9gd:~ vanilla$
| ||
5. | echo "Hello $USER, welcome to Unix." Figure 2.13 shows output from echo , using the $USER environment variable in an argument. $USER is replaced by your short user name (the $ usually indicates that the following term is a variable, and the shell substitutes the value of the variable before executing the command. Figure 2.13. This shows the output from echo , using the $USER environment variable in an argument. $USER will be replaced by your short user name. user-vc8f9gd:~ vanilla$ echo "Hello $USER, welcome to Unix." Hello vanilla, welcome to Unix. user-vc8f9gd:~ vanilla$ | ||
6. | echo "$USER created this" > file.txt In this case, the output from the echo command doesn't go to your screen, but rather it is redirected into the file named file.txt, either creating the file with this specific content or copying over anything within it. For more on redirection and output, see "About Standard Input and Output," later in this chapter. | ||
7. | ls Figure 2.14 shows the output from the ls command. The files listed now include file.txt, created in the previous step. Figure 2.14. This output from the ls command now includes file.txt. user-vc8f9gd:~ vanilla$ ls Desktop Library Music Public file.txt Documents Movies Pictures Sites user-vc8f9gd:~ vanilla$ | ||
8. | cat file.txt The cat command, derived from concatenate , displays the contents of the file ( Figure 2.15 ), again based on the command in step 6 ( concatenate actually means combine ; if you read the Unix manual section on cat with man cat , you will see how it can be used to combine several files). Figure 2.15. The cat command, derived from concatenate , displays the contents of a file. user-vc8f9gd:~ vanilla$ cat file.txt vanilla created this user-vc8f9gd:~ vanilla$
| ||
9. | cp file.txt filecopy.txt cp stands for copy . You have made a copy of file.txt called filecopy.txt. Run the ls command again to see it ( Figure 2.16 ). Figure 2.16. Running the ls command again shows that you have made a copy of file.txt called filecopy.txt. user-vc8f9gd:~ vanilla$ ls Desktop Library Music Public file.txt Documents Movies Pictures Sites filecopy.txt user-vc8f9gd:~ vanilla$ | ||
10. | rm filecopy.txt The rm command removes the file forever. The file is not moved to the Trash, and there is no undo. The rm command is serious business. Run the ls command again to confirm that it is gone. | ||
11. | mkdir testdir The mkdir command creates (or makes ) a new directory (that's what Unix calls folders), in this instance named testdir . | ||
12. | open . This opens the current directory in the Finder. You should see the file called file.txt and the directory testdir ( Figure 2.17 ). Figure 2.17. The Finder window now shows the new file and directory. | ||
13. | cd testdir You have told your shell to change from the current directory to the directory named testdir . Notice that your shell prompt has changed to reflect your new directory ( Figure 2.18 ). Figure 2.18. After you use the cd command, the shell prompt changes. user-vc8f9gd:~ vanilla$ cd testdir user-vc8f9gd:testdir vanilla$ | ||
14. | pwd This confirms that you are indeed in the new directory ( Figure 2.19 ). Figure 2.19. Running the pwd command again shows your new working directory. user-vc8f9gd:testdir vanilla$ pwd /Users/vanilla/testdir user-vc8f9gd:testdir vanilla$ | ||
15. | date The date command displays the current date and time ( Figure 2.20 ); unless you've perfected time travel, your output will be different. Figure 2.20. The date command displays the current date and time. user-vc8f9gd:testdir vanilla$ date Sun Mar6 17:29:27 PST 2005 user-vc8f9gd:testdir vanilla$ | ||
16. | date > dates.txt This redirects the output of the date command into a file named dates.txt. It is often useful to save the output of a command. | ||
17. | date >> dates.txt This time we redirect the output using the >> operator (this redirects the output and appends it to the file instead of replacing the contents). | ||
18. | cat dates.txt The file contains the results of both redirects from steps 16 and 17 ( Figure 2.21 ). Figure 2.21. Using redirection, you get a file that contains the results of both redirects.user-vc8f9gd:~/testdir vanilla$ date > dates.txt user-vc8f9gd:~/testdir vanilla$ date >> dates.txt user-vc8f9gd:~/testdir vanilla$ cat dates.txt Sun Mar6 17:30:30 PST 2005 Sun Mar6 17:30:41 PST 2005 user-vc8f9gd:~/testdir vanilla$ | ||
19. | mv dates.txt newname.txt The mv command renames (or moves ) a file. You can think of the command line as being mv oldname newname In Unix, a file's name is actually the name of its location, so the same command is used to rename and to move files. Run the ls command to see that the file dates.txt has been renamed to newname.txt ( Figure 2.22 ). Figure 2.22. Running the ls command again shows the renamed file.user-vc8f9gd:~/testdir vanilla$ mv dates.txt newname.txt user-vc8f9gd:~/testdir vanilla$ ls newname.txt user-vc8f9gd:~/testdir vanilla$
| ||
20. | date >> dates.txt The >> operator appends to an existing file and will create a file if it doesn't already exist ( Figure 2.23 ). Figure 2.23. Running the ls command again shows the file created with the >> operator. user-vc8f9gd:~/testdir vanilla$ ls dates.txt newname.txt user-vc8f9gd:~/testdir vanilla$ | ||
21. | rm *.txt The * operator is used in the command line as a wildcard to match all the files ending in .txt. As a result, the rm command actually receives two arguments dates.txt and newname.txt and acts on both of them. For more on wildcard operators, see "Wildcards," later in this chapter. Run the ls command to confirm that there are now no files in the current directory; you simply get a shell prompt back ( Figure 2.24 ). Figure 2.24. Note that when there is nothing to list, the ls command gives no output. user-vc8f9gd:~/testdir vanilla$ ls user-vc8f9gd:~/testdir vanilla$ | ||
22. | cd This takes you back to your home directory. Notice that your shell prompt changes ( Figure 2.25 ). Figure 2.25. Your shell prompt changes when you use the cd command. user-vc8f9gd:~/testdir vanilla$ cd user-vc8f9gd:~ vanilla$
|