Basic Unix

From CS 61A Wiki
(Redirected from Unix)
Jump to: navigation, search

This is a list of useful Unix commands.

Directories

A directory is a folder.

Current working directory

The current working directory is printed just before the command prompt (usually # or $):

~/test $

Alternatively, use the command pwd:

~/test $ pwd
~/test

Changing directory

The cd command changes the working directory.

When used with an argument, it changes to that directory:

~ $ cd test
~/test $ cd .. # .. is shorthand for "the parent directory"
~ $

When used without an argument, it changes to the home directory (~):

~/test $ cd
~ $

ls

ls lists the files in the current working directory. Alternatively, ls -a lists all files in the current working directory (including hidden files).

Creation

To create a directory in the current directory, use the command mkdir:

~ $ ls # nothing in the directory right now
~ $ mkdir test
~ $ ls
test
~ $ mkdir test2
~ $ ls
test  test2

Deletion

To delete a directory (along with all its contents), use the command rm -r:

~ $ ls
test  test2
~ $ rm -r test
~ $ ls
test2

Files

Creation

To create an empty file, use the touch command:

~ $ ls
test
~ $ touch my_file
~ $ ls
my_file  test

To create a file with predetermined text, use echo text > file:

~ $ echo testing > my_file
~ $ cat my_file
testing

This is called output redirection because the output of the command echo testing is sent to a file instead of being displayed on the screen.

cat

To view the contents of a file, use the cat command. See example above.

Deletion

To delete a file, use the rm command:

~ $ ls
my_file  test
~ $ rm my_file
~ $ ls
test

Copying

To copy a file, use the cp command:

~ $ ls
my_file  test
~ $ cat my_file
testing
~ $ cp my_file new_file
~ $ ls
my_file  new_file  test
~ $ cat new_file
testing

An example of copying a file from another directory:

~ $ ls
my_file  test
~ $ cp ~cs61a/lib/shakespeare.txt . # '.' is shorthand for "the current directory"
~ $ ls
my_file  shakespeare.txt  test

Renaming

To rename a file, use the mv command, where the first argument is the file you want to rename and the second argument is the new file name:

~ $ ls
my_file  test
~ $ mv my_file new_name
~ $ ls
new_name  test

Moving

To move a file to another directory, use the mv command, where the first argument is the file you want to move and the second argument is the destination directory:

~ $ ls
my_file  test
~ $ mv my_file test
~ $ ls
test
~ $ cd test
~ $ ls
my_file

Manual

The man command tells you how to use a Unix command. For example, man cp will bring up a page inside of the terminal. The NAME field will give a brief description of what the command does, and the DESCRIPTION will have a host of extra options you can run the command with.

You can navigate forward through the man page with the Enter/Return key and you can quit with q.

Connecting

A list of available instructional servers can be found here: http://inst.eecs.berkeley.edu/cgi-bin/clients.cgi?choice=servers. Commonly used servers include nova, star, cory, and hive1 - hive28.

Remote login (ssh)

To remotely connect to your instructional account, use the ssh (secure shell) command. Use this command with your cs61a login and the server of your choice:

ssh cs61a-login@server.cs.berkeley.edu

To connect to the cs61a-aa account and start a terminal session:

ssh cs61a-aa@nova.cs.berkeley.edu

You can also execute single commands via ssh, instead of opening an entire session:

ssh cs61a-aa@nova.cs.berkeley.edu glookup

Transferring files (scp)

To copy files remotely to or from an instruction account, use the scp (secure copy) command.

To copy a file from an instructional machine to a local directory:

scp cs61a-login@server.cs.berkeley.edu:target_file local_directory

To copy a file from a local directory to an instructional machine:

scp local_directory/local_file cs61a-login@server.cs.berkeley.edu:target_directory

To copy an entire directory from a local directory to an instructional machine:

scp -r local_directory cs61a-login@server.cs.berkeley.edu:target_directory

See also

Sources