Command Line Basics
Sam Mansfield
Overview
The command line can be intimidating at first, but there are only a few basic commands that you need to know: cd, pwd, ls, mkdir, mv, cp, rm, and man. Each command is described briefly below.
Commands
The following are the commands that are essential for using the command line. Other additional information is ~ is your home directory, “.” is your current directory, and “..” is your current parent directory. Most operating systems place all your relevant files and folders that you, the user, use in your home directory. For example on a Mac, the Documents folder is located at ~/Documents and the Downloads folder is located at ~/Downloads. cd directory This command changes the current directory to directory. pwd This command displays the current directory. ls directory This command lists the files and directories in directory. mkdir directory This command creates a new directory with the name directory in the current directory. ls directory This command lists the files and directories in directory. mv old name new name This changes the name of the file or directory with the name old name to the name new name. cp original copy This copies the file with the name original to the file copy. This command can also be used to copy directories with the -r option. rm name This command deletes the file named name. This command can also be used on directories with the -r option. man cmd This command displays the manual page for the given cmd. The manual pages are not always easy to read, and often it is easier to understand the com- mand by Googling it instead.
1 Example
home
a.txt
b.pdf
c_dir
d.txt
Figure 1: An example file directory. The directory home is the top level directory, which contains two files, a.txt and b.pdf, and one directory, c dir, which contains one file, d.txt.
The example that is provided will use the example file directory in Figure 1 as the initial state of the directory. The example will also assume that ~ represents the home directory. A “>” represents the command prompt.
2 > cd ~ > pwd /home > ls a.txt b.pdf c dir > ls . a.txt b.pdf c dir > cd c dir > pwd /home/c dir > ls d.txt > ls .. a.txt b.pdf c dir > cd .. > pwd /home > mkdir dir > ls a.txt b.pdf c dir dir > mv dir c dir > ls a.txt b.pdf c dir > ls c dir d.txt dir > cp a.txt e.txt > ls a.txt b.pdf c dir e.txt > cp e.txt c dir/f.txt > ls a.txt b.pdf c dir e.txt > ls c dir d.txt dir f.txt > cp c dir new dir cp: c dir/ is a directory (not copied) > cp -r c dir new dir > ls a.txt b.pdf c dir e.txt new dir > rm e.txt > ls a.txt b.pdf c dir new dir > rm new dir rm: new dir/: is a directory > rm -r new dir > ls a.txt b.pdf c dir
3