-
Notifications
You must be signed in to change notification settings - Fork 1
UNIX and the command line
UNIX was an operating system created by Dennis Ritchie and Ken Thompson, and many modern day operating systems are based off the designs and primitives first introduced by UNIX. One major idea they introduced was the idea of a programmer using the computer interactively via the command line, and the language and designs they used still exists today in Mac OS X and Linux-family operating systems (sorry Windows!). You can read the original paper introducing UNIX here!
In the programming environment you had setup at our last meeting, open up a terminal and enter this command:
ls
This will list all the current files and directories (folders) in your current directory (current working path). To see your current working directory, use the pwd
command ( print working directory).
Many commands typically take arguments. Type ls
again, pick a directory in your current working path (e.g. "Photos"), and then type ls Photos
. This will list all the files and directories in the Photos
directory.
Many commands typically takes options (also called flags) as well, and they are usually denoted with -
or --
prefixes. For example:
ls -l
will list all the files, and print out additional data.
ls -l -a
will list all the files (including hidden ones, which have a .
prefix), and print out additional data.
How did I know these options exist? What other options exist for ls
? To see documentation for a command, we can use the man
command:
man ls
This will open up a screen containing more info about ls
, and list any available options. You can scroll using the arrow keys, and press q
to quit. If you're interested, man
opens the documentation using less
, a program that allows you to navigate text files. For more information and hotkeys, see here, or type man less
!
In general, use man COMMAND
to open up the manual pages for the command, if they exist.
Here's a bunch of useful commands (see their man pages for more uses and options!):
-
ls
- list the contents of the current directory -
cd DIRECTORY
- change your current working directory -
pwd
- print working directory -
cat FILE
- prints out the contents of a file -
find
- finds files in a given directory. More on this later.
-
cp SRC DEST
- copies a file from SRC to DEST -
mv SRC DEST
- movies a file from SRC to DEST -
mkdir DIRECTORY
- makes a new directory -
rm FILE/DIRECTORY
- remove a file or directory -
chmod
- change the permission of a file. More on this later -
touch FILE
- create a file if it doesn't already exist
-
echo STUFF
- prints STUFF to the command line -
wc
- word count. Useful for counting lines, characters, etc. -
cut
- cut text. Useful for slicing strings and text. -
curl LINK
- grab the HTML from the specified LINK -
wget LINK
- download the file at the given LINK. Mac users will need to runbrew install wget
to get this command. -
sudo COMMAND
- some command require you run as a different user, or as the privilegedroot
user (e.g. for installing new software). -
sed
- stream editor. Useful for manipulating streams of text. More on this later. -
grep
- use regular expressions to pattern match on text. More on this later.
You may have noticed output like this when you ran the ls -l
command earlier:
-rw-r--r-- 1 kennyyu staff 154 Sep 23 01:14 README.md
drwxr-xr-x 2 kennyyu staff 68 Sep 23 02:08 foo
-rwxr-xr-x 1 kennyyu staff 394 Sep 23 01:01 scraper.sh
-rwxr-xr-x 1 kennyyu staff 1353 Sep 23 01:11 scraper_pretty.sh
The d
in front of foo
indicates that foo
is a directory. The r
, w
, x
indicate read, write, and execute permissions on the current file. The -
indicates that the permission is not granted. The three sets of rwx
indicate permissions in this order: User (me), Group (a user can belong to multiple user groups on a computer), and other (all other users on the computer).
Thus, I have read, write, and execute permissions on scraper.sh
, whereas everyone in my user group and all other users only have read and execute permissions.
To change permissions on a file, we can use the chmod
command. For example, let's create a file:
touch bar
ls -l
You should see output like this:
-rw-r--r-- 1 kennyyu staff 0 Sep 23 02:12 bar
Now let's add execute permissions:
chmod +x bar
ls -l
And you should see output like this:
-rwxr-xr-x 1 kennyyu staff 0 Sep 23 02:12 bar
If you wanted to add read or write permissions, you can also do chmod +r bar
or chmod +w bar
. To revoke permissions:
chmod -x bar
For more usages of chmod
, see man chmod
.
Go back to the main page here.