Skip to content

Key Commands

Key Commands

In this section, we review some of the basic commands for moving around directories, changing filenames, copying, and so forth.

Understanding and navigating directories

The first question someone might ask when they launch the terminal is: Where am I? – i.e. what is the working directory the terminal is in?  We figure that out with pwd (which is short for print working directory). Typing this command in my terminal I get the following:

/home/jupyter-user3

Try this out in your environment:

Firstly, figure out which directory you are currently in by using the pwd command. If you are in the home directory, great! If not, you can easily get there by typing in cd ~, the tilde represents home. Using the ls command, you can list the files in your directory. To create a new directory use the make directory command mkdir followed by the name of your new directory. Finally, change your working to the new one using cd.

pwd
cd ~
ls
mkdir mydir
cd ~/mydir

Basics: Moving around in directories

These are the basic commands needed to move around in directories.

pwd #gets the full path of the present working directory
ls #lists the files in the current working directory
ls -l #similar to ls, but it gives additional information about each file (permission, size etc)
ls -a #includes hidden files (those that begin with .name) in the list as well
ls -R #lists subdirectories recursively
ls -t #lists files in chronological order
cd #brings you to the highest level of your home directory
cd .. #moves one directory up
cd ../../ #moves two directories up
cd - #go back to the previous directory you were in
cd~ #go to the home directory

File Creation and Deletion Commands

mkdir dir_name #creates a new directory
rmdir dir_name #removes the empty directory
rm file_name #removes the file
rm -r dir_name #removes the directory, including its contents. It asks for confirmation before doing so. To stop, supply the <code>f</code> argument cp name path #copies the file/directory as specified in the path mv old_name new_name #moves the contents of the file/directory to the new location 

Special Characters: Wildcards (*), Tildas (~), and periods (..,.)

In the examples above, you would have noticed that certain characters are used to imply a special meaning. For example to move one directory up you would type cd ... This .. is special. Let’s look at the meaning of a few, pretty common, special characters.

On important character is the * wildcard character. It basically means anything including the characters surrounding it.  Thus ls m* would list all files start with m.  We could type ls *ls* to get a listing of all files that have the characters “ls” in them. It’s handy for finding types of scripts, such as all python scripts, which by convention end in .py, so we would type ls *.py.

Tilda or ~ is the next key character to remember. It is shorthand for your home directory or path. We’ll use directory and path interchangeably at times. Thus to cd ~ takes you to your home directory.

Two periods, or .. refers to the parent directory of your current directory, i.e. cd .. moves up a directory.

One period, or ., is shorthand for your current directory. For example cp ~/demo/jupyterlab-slides.pdf . copies jupyterlabs-slides.pdf into the current directory.

Pipe | and Redirect>

One of the most important features of Unix-based systems is the ability to manipulate when data is going to the screen, a program, or into another file.  Specifically, we’ll discuss piping and redirecting. Piping is done using the | symbol and sends the output from what’s left of the pipe, to the program on the right of the pipe. Redirect > puts the output of the left into the file in the right.

An example of a pipe that takes our history and pipes it to grep which only prints lines that match.

history | grep ls

Output:

An example of redirecting that output to a file

history > myhistory.txt

Output:

Core Unix-based command-line commands

Shown below are some common Unix-based command-line commands. Remember, if you are trying to do something and you don’t know how, you can always use Google and there will be more than one helpful resource. Also, at the bottom are several that I find useful within my cheatsheet.

Finding files, directories and applications

find -name "*pattern*"            # searches for *pattern* in and below current directory
find /usr/local -name "*blast*"   # finds file names *blast* in specfied directory
find /usr/local -iname "*blast*"  # same as above, but case insensitive
find ~ -type f -mtime -2   # finds all files you have modified in the last two days
which <application_name>   # location of application
whereis <application_name> # searches for executeables in set of directories
grep pattern file           # provides lines in 'file' where pattern 'appears',
grep 'pattern' file | wc    # pipes lines with pattern into word count wc

List directories and files

ls -al   # shows something like this for each file/dir: drwxrwxrwx
          # d: directory
          # rwx: read write execute
          # first triplet: user permissions (u)
          # second triplet: group permissions (g)
          # third triplet: world permissions (o)

More useful commands

df             # disk space
free -g        # memory info in Megabytes
uname -a       # shows tech info about machine
bc             # command-line calculator (to exit type 'quit')
wget <ftp>     # file download from web
ln -s original_file new_file # creates symbolic link to file or directory
du -sh         # displays disk space usage of current directory
du -sh *       # displays disk space usage of individual files/directories
du -s * | sort -nr # shows disk space used by different directories/files sorted by size
Process Management

Viewing text files

more <my_file>  # views text, use space bar to browse, hit 'q' to exit
less <my_file>  # a more versatile text viewer than 'more', 'q' exits, 'G' end of text, 'g' beginning, '/' find forward, '?' find backwards
cat  <my_file>  # concatenates files and prints content to standard output

Running and managing processes

top               # view top consumers of memory and CPU (press 1 to see per-CPU statistics)
who               # Shows who is logged into system
w                 # Shows which users are logged into system and what they are doing
ps                # Shows processes running by user
ps -e             # Shows all processes on system; try also '-a' and '-x' arguments
ps aux | grep <user_name> # Shows all processes of one user
ps ax --tree      # Shows the child-parent hierarchy of all processes
ps -o %t -p <pid> # Shows how long a particular process was running. # (E.g. 6-04:30:50 means 6 days 4 hours ...)
Ctrl z <enter>    # Suspend (put to sleep) a process
fg                # Resume (wake up) a suspended process and brings it into foreground
bg                # Resume (wake up) a suspended process but keeps it running in the background.
Ctrl c            # Kills the process that is currently running in the foreground
kill <process-ID> # Kills a specific process
kill -9 <process-ID> # NOTICE: "kill -9" is a very violent approach. It does not give the process any time to perform cleanup procedures.
kill -l           # List all of the signals that can be sent to a proccess
kill -s SIGSTOP <process-ID> # Suspend (put to sleep) a specific process
kill -s SIGCONT <process-ID> # Resume (wake up) a specific process
renice -n <priority_value> # Changes the priority value, which range from 1-19,the higher the value the lower the priority, default is 10.

Archiving

Dealing with archiving files is fundamental in bioinformatics. Most often files are zipped (compressed). If you want to put multiple files or directories into a single file, you use the program tar .  It’s important to know that tar alone does not zip without the -z options.  Here are a few approaches:

Creating

tar -cvf my_file.tar mydir/    # Builds tar archive of files or directories. For directories, execute command in parent directory. Don't use absolute path.     
tar -czvf my_file.tgz mydir/   # Builds tar archive with compression of files or directories. For dirs, execute command in parent directory. 
zip -r mydir.zip mydir/        # Command to archive a directory (here mydir) with zip.
tar -jcvf mydir.tar.bz2 mydir/ # Creates *.tar.bz2 archive

Viewing

tar -tvf my_file.tar
tar -tzvf my_file.tgz

Extracting

tar -xvf my_file.tar
tar -xzvf my_file.tgz
gunzip my_file.tar.gz # or unzip my_file.zip, uncompress my_file.Z,or bunzip2 for file.tar.bz2
find -name '*.zip' | xargs -n 1 unzip # this command usually works for unzipping many files that were compressed under Windows
tar -jxvf mydir.tar.bz2 # Extracts *.tar.bz2 archive

Key Commands