Basic Unix (by Nelson Padua-Perez)
This document is a quick introduction of some Unix commands. Read
this document once and then practice the commands we described in
a Unix session. Additional Unix references can be found at the end.
Notice that in grace.umd.edu you can use the up/down arrow keys
to display previously executed commands (less typing :).)
- We will use the machine grace.umd.edu.
Students at UMCP can use their directory id and password to log on to
this machine. To connect (log on) you need an SSH Client
in your computer.
Unix uses a tree structure where you have files and folders (directories). In
the description below, we use % as command prompt. In grace.umd.edu you
could see something similar to grace6:~:
After logging in you will be in "your home directory".
To list (display) the contents of a directory use the ls command.
% ls /* Files that start with a period (e.g., .aliases) are not listed */
% ls -al /* Long listing that also displays hidden files (those starting with a period) */
% ls -F /* Adds forward slash to identify directories */
To change to a directory use the cd command. Three important
- . → Represents the current directory.
- .. → Represents the parent directory.
- ~ → Represents the home directory.
% cd data_dir
% cd /* Takes you back to your home directory; great when you are lost */
% cd / /* Takes you to the root directory (under which all files/folder reside) */
% cd .. /* Takes you to the parent directory */
% cd ~/216public /* Takes you to the 216public directory that resides in the home directory */
To create a directory use the mkdir command. For example:
% mkdir data_dir
To display the directory where you find yourself in use the pwd command. For example:
There are several text editors (emacs, vim, etc.) A very simple one is nano. Once
you start nano, look at the bottom of the window to see a summary of commands.
% nano my_prog.c
To copy files use the cp command. For example:
% cp my_prog.c my_copy.c
% cp my_prog.c data_dir /* Copies my_prog.c to the data_dir directory */
% cp my_prog.c .. /* Copies my_prog.c to the parent directory */
% cp /tmp/e.txt . /* Copies e.txt to current directory. Notice the period after e.txt */
% cp -r Week1 ~/216 /* Copies the directory Week1 to the folder 216 in the home directory */
To remove a file use the rm command. For example:
% rm my_prog.c
To remove a directory (it must not have any files) use the rmdir command. For example:
% rmdir data_dir
To remove a directory (with files) use the rm command. For example:
% rm -f -r Week1
To rename a file use the mv command. For example:
% mv my_prog.c my_prog_new_name.c
To move a file to a directory use the mv command. For example:
% mv my_prog.c data_dir
To display the contents of a file without opening with an editor use the less command. For example:
% less my_prog.c
There are several ways to change file permissions using the chmod. For example:
% chmod 400 data.txt /* Only the owner (you) can read it */
% chmod 600 data.txt /* Only the owner (you) can read and write it */
% chmod 700 data.txt /* Only the owner (you) can read, write, and execute it */
To log out use the logout command. For example:
To access Unix manual pages use the man command. For example to
find information about the ls command:
% man ls
To compare file use the diff command. For example:
% diff file1 file2
To create a compressed gzip archive file use the tar command.
For example, creating .tar.gz file for directory grep_example:
% tar czf grep_example.tar.gz grep_example
To uncompress a .tar.gz file use the tar command.
% tar xzf grep_example.tar.gz
If you want to transfer files from grace.umd.edu to your computer
you can use https://dav.terpconnect.umd.edu/.
Use your directory id and password to connect.
- Additional Commands
% date /* Current date time */
% who /* Current users */
% ps /* Shows processes */
% emacs my_prog.c /* Popular editor */
% ls -al | less /* Using | to pipe output of one command as input to another */
% cal /* Calendar */
% ls *.txt /* Lists all files ending in .txt */
% cat data.txt /* Displays contents of a file (in this case data.txt) */