These are basic Unix commands that I use with Terminal – this is not a complete set of Unix commands! Note – a folder is the same thing as a directory.
Selected Unix Commands
- cd — change directory
- cp — copy files
- ls — see a list of files and folders in your current (working) directory
- mv — move and rename files
- pwd — see your current (working) directory [print working directory]
- rm — remove (delete) a file
- rmdir — remove (delete) a directory
Use cd (change directory) to change directories; use pwd to determine your current (working) directory.
$ cd photos $ pwd $ /Users/kathy/Documents/photos $ ls motorcycles 365project $ cd motorcycles $ pwd $ /Users/kathy/Documents/photos/motorcycles $ ls bmw ducati hd transalp $ cd .. $ pwd $ /Users/kathy/Documents/photos $ cd $ /Users/
Kathy began in her home directory, then went to her photos subdirectory; she found what was in this directory (two sub-directories) using ls. She cd’ed to the directory motorcycles and saw that each bike had its own directory. Then she used cd .. to jump back one level. Finally she used cd with no argument to jump back to the home directory.
Use cp (copy) to copy files or directories.
Syntax : cp [options] Source_file Target_file cp [options] Source_file(s) Target_Folder $ cp foo foo.2
This makes a copy of the file foo and names it foo.2
$ cp *.jpg ~/Documents/photos
This copies all jpg files from the current (working) directory [use pwd to determine the current directory] to the Documents/photos directory
$ cp -r Documents "Documents backup"
This copies the “Documents” folder to “Documents backup”. The quotes are needed because of the space in the directory name.
- Add “-r” [cp -r] to copy a directory and its sub-directories (recursive).
- Add -n to avoid over-writing existing files.
- Add -p to preserve attributes like access time, modification date.
You can drag and drop the file or the directory (source or target) into Terminal rather than type the full name and path.
Use ls (list) to see a list of directories and files, relative to your current (working) directory.
$ ls foo letter2.doc foobar report.txt letter1.doc $
Note that you have three files and two directories. There are some useful tweaks to the ls command. Adding the argument “l*” [letter plus wildcard] yields:
$ ls l* letter1.doc letter2.doc $
Note what happened: ls now lists all files that begin with “l”. The asterisk (*) is the ” wildcard” character; it matches any string.
Use mv (move or rename) to change the name of files and directories. Moving and renaming are the same thing.
Syntax : mv [options] source target $ mv foo.txt foo2.txt
The file that was named foo.txt is now named named foo2.txt
Use pwd [print working directory] when you don’t know your current directory. See cd
$ pwd /Users/kathy/Documents/photos $
Use rm to remove (delete) files from your directory.
Syntax: rm [options] file $ rm letter1.doc remove letter1.doc? y $ rm -i letter* remove letter1.doc? y remove letter2.doc? y remove letter3.txt? n $
The first command removed a single file. The second command was intended to remove all files beginning with the string “letter.” However, decided not to remove letter3. You can remove all files in a directory by using two wildcards: *.*
$ rm -r photos
To delete a protected folder:
$ sudo rm -r NAME_OF_DIRECTORY_TO_DELETE
You will be prompted for your password; type it and press return.
- Add -f to attempt to remove files without confirmation, regardless of permissions
- Add -i to add confirmation before removing each file
- Add -r to remove all directory contents and the directory
Use rmdir to remove (delete) a directory that is empty.
Syntax: rmdir directory $ rmdir pix $
However, a directory must be empty before it can be removed. To empty a directory, use rm.