Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
cd
This command name stands for "change directory".
It changes your current working directory to the specified location.
The last visited directory is referred to with a hyphen ("-").
# go to root directory of the system and print the working directory
cd /
pwd
# go to the home directory and print the working directory
cd ~
pwd
# change directory using the absolute path and print the working directory
cd /net/bmc-pub14/data/
pwd
pwd
This command name stands for "print working directory".
It displays on the terminal the absolute path name of the current (working) directory.
It is useful to locate where you are currently in the Unix tree.
# print working directory
pwd
ls
This command stands for "list".
It displays the content of a directory.
By default, the content is listed in lexicographic order.
# list the content of the current directory
ls
ls .
# list content of the parent directory
ls ..
# list the contens of your home directory from anywhere
ls ~
One very important utility is the shell.
Shell - user interface to the system, lets you communicate with a UNIX computer via the keyboard.
The shell interfaces you with the computer through three channels: standard input (STDIN), standard output (STDOUT), and standard error (STDERR). STDERR is the channel used for communicating program errors. We'll focus on STDIN and STDOUT.
STDIN is the channel, or stream, from which a program reads your input. When you type in your terminal, you're typing into the STDIN.
STDOUT is the channel, or stream, from which a program outputs data. When you run echo 'Hello, World!'
, the shell is printing the output into STDOUT. In this case, STDOUT is the terminal screen.
[asoberan@luria net]$ echo 'Hello, World!'
Hello, World!
STDIN and STDOUT are the ways that you and the shell communicate. But what is the shell doing when you tell it to run a command?
Let's say I want to run the echo
command, as we did above. We type echo
in the terminal, then press Enter
. What the shell does is read the PATH
environment variable, which is a system variable that lists the different paths that programs are stored in.
Using printenv
to print the PATH
environment variable. Notice how many paths in this environment variable are ones dictated by the FHS:
[asoberan@luria net]$ printenv PATH
/home/asoberan/.conda/envs/class/bin:/home/software/conda/miniconda3/condabin:/home/software/conda/miniconda3/bin:/home/asoberan/.local/bin:/opt/ohpc/pub/mpi/mvapich2-gnu/2.2/bin:/opt/ohpc/pub/compiler/gcc/5.4.0/bin:/opt/ohpc/pub/prun/1.1:/opt/ohpc/pub/autotools/bin:/opt/ohpc/pub/bin:/home/asoberan/.local/bin:/home/software/google-cloud-sdk/google-cloud-sdk-193.0.0/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
Then, it will go through these paths and look for the program you are trying to run. For example, the echo
command is stored in /usr/bin
, a directory we can see in the PATH
environment variable:
Using which
to find the location of a program:
[asoberan@luria net]$ which echo
/usr/bin/echo
Once it finds it, it will run the program in a new process, wait for the program to give an exit status, then print any output of the program into STDOUT, in this case our terminal screen. Now, the shell is ready to run another command.
The shell is used to interface with a UNIX computer, but we're not going to go to the server room, plug in a keyboard, and start typing commands for our shell. We're going to use the Secure Shell (SSH) to remotely connect to Luria.
SSH is a program that lets you open a remote shell to your UNIX system. It's secure because your connection to the system is kept encrypted. For instructions on using SSH to connect to Luria, refer to the page below:
find
Searches directories to find a directory or file.
grep
Searches a file for a specific pattern.
Can be used similarly to find, but it also searches file contents.
find ~ -name arrayDat*
grep -r "arrayDat*" .
Make sure to run all of the copy commands below, as we'll be using files from the Ostrom server later in the course.
mkdir
This command name stands for "make a directory".
It creates a new folder (or directory). If no path is specified, the new directory is created in the current directory.
# start in your home directory
cd ~
# create a directory named "unixclass"
# with a subdirectory named "testdir"
mkdir unixclass
mkdir unixclass/testdir
# change current directory directly to "testdir"
cd unixclass/testdir
# go to the parent directory (i.e. unixclass)
# and print the working directory
cd ..
pwd
touch
This command creates an empty file with the given name.
# Go to /home/<your username>/unixclass
cd ~/unixclass
# Create an empty file named "hello.txt"
touch hello.txt
# List the files in the directory to verify that worked
ls
cp
and mv
These commands stand for "copy" and "move," respectively.
They copy / move files and directories to the specified location.
Wildcards symbols such as "*" or "?" are commonly used to copy multiple files with a single command.
The symbol "*" stands for any number of alphanumeric characters.
The symbol "?" stands for a single alphanumeric character.
# Start in ~/unixclass
cd ~/unixclass
# copy the file named arrayDat.txt into your unix_class directory
cp /net/ostrom/data/dropbox/arrayDat.txt .
ls
# copy all the files with suffix "array”
# into the current directory
cp /net/ostrom/data/dropbox/array* .
ls
# copy any file whose extension is "txt"
cp /net/ostrom/data/dropbox/*.txt .
ls
# copy all files
cp /net/ostrom/data/dropbox/* .
ls
rmdir
and rm
rmdir
only removes empty directories, rm
removes both directories and files.
rm
needs -r
flag to remove directories.
# Start in ~/unixclass
cd ~/unixclass
# Create a temporary directory
mkdir trash
# create copies of arrayDat.txt in the temporary directory
cp arrayDat.txt trash/arrayDat1.txt
cp arrayDat.txt trash/arrayDat2.txt
cp arrayDat.txt trash/arrayDat3.txt
cp arrayDat.txt trash/arrayDat4.txt
ls trash
# Try to delete the directory with `rmdir`
rmdir trash
# Try to delete the directory with `rm -r`
rm -r trash
Unix comes with a preloaded documentation, also known as "man pages".
Each page is a self-contained document consisting of the following sections:
NAME: the name and a brief description of the command.
SYNOPSIS: how to use the command, including a listing of the various options and arguments you can use with the command. Square brackets ([ ]) are often used to indicate optional arguments. Any arguments or options that are not in square brackets are required.
DESCRIPTION: a more detailed description of the command including descriptions of each option.
SEE ALSO: References to other man pages that may be helpful in understanding how to use the command in question.
When you are not sure of the exact name of a command, you can use the apropos command to see all the commands with the given keyword on their man page.
apropos keyword
For example, to see which commands are relevant to the task of copying, type the following:
apropos copy
To view a manual page, use the command man.
man command_name
When viewing a man page, use the following keys to navigate the pages:
spacebar- view the next screen;
b (for "back") - view the previous screen;
arrow keys - navigate within the pages;
q (for "quit") - quit and return to the shell prompt.
For example, to view the man page associated with the command cp
, type the following:
man cp