Becoming Familiar with Unix Commands


Familiarity with common Unix commands makes the practice exercises easier to work through. If you’re already comfortable with Unix, skip ahead to the Initial Exercise. Otherwise, work through this short exercise first to learn the basics.



Changing Directories

The command for changing directories is cd.

  1. In Derecho, move to the practice_exercises directory in your scratch space:

    cd /glade/derecho/scratch/$USER/practice_exercises
    

    Use this command any time you need to move to a different directory.

  2. A useful shortcut for moving up one directory, regardless of your current location, is typing two periods in a row (..).

    cd ..
    

    You can also move up through multiple directories at once using this command. For example, move into one of the directories inside practice_exercises, then navigate back to where you started.

    cd practice_exercises/wrf/test/em_real
    cd ../../..
    

    You should now be back in the /glade/derecho/scratch/$USER/ directory.



Listing Directory Content

For a list of files and directories in your current directory, use ls, which includes various options.

  1. Running the following lists all files and directories in your current directory.

    ls
    

  2. To list files/directories in “long format”, which includes information like the file’s size, owner, permissions, etc., use the following.

    ls -ls
    

  3. To list file/directories in another directory, you could use something like this example, which prints the contents of your home directory.

    ls /glade/u/home/$USER
    

  4. Hidden files, such as environment files (e.g., .bashrc), are often found in your home directory. Change to your home directory, then list all files, including hidden ones.

    cd
    ls -a
    

    Notice that no directory was specified with cd in this case. This shortcut works only when navigating to your home directory — otherwise, you’ll always need to specify a directory.



Renaming, Moving, or Copying Files and Directories

To copy a file, use the cp command. Try copying a file into your /glade/derecho/scratch/$USER/ directory.

  1. First verify that you’re in the right place.

    cd /glade/derecho/scratch/$USER/
    cp practice_exercises/wrf/test/em_real/namelist.input .
    

    Notice the full path was needed to locate the file, followed by a “.” at the end. This command copies the namelist.input file from its current location (practice_exercises/wrf/test/em_real/) to the current directory - the (“.”) meaning “here.”

  2. You can also move the file to a location other than “here.”

    cp practice_exercises/wrf/test/em_real/namelist.input    practice_exercises
    

    There should now be a copy of namelist.input in the practice_exercises/ directory. Use ls to verify.

    ls practice_exercises
    

  3. To move or rename a file, use mv. Rename namelist.input in your current directory.

    mv namelist.input copied_namelist.input
    

    List the files in the directory (type ls) to the file name changed to copied_namelist.input.

  4. Now move that file to another directory.

    mv copied_namelist.input practice_exercises
    

  5. List the contents in the practice_exercises/ directory (ls practice_exercises). Along with other files/directories, you should see both namelist.input and copied_namelist.input.



Other Common Unix Commands

Command

Syntax

Usage

mkdir
rmdir

mkdir new_directory_name

To make (mkdir) or remove (rmdir) directories

rm

rm file

Remove files

more

more file

Prints the first part of a file, as much as will fit on one screen. Double-tap the space bar to see more or type “q” to quit.

cat

cat file

Prints the entire file to the screen

head

head file

Prints the file’s first few lines to the screen

tail

tail file

Prints the file’s last few lines to the screen

grep

grep word file
grep -i word file
grep -i word *

Find lines within the specified file that match the patterns used in the command
Add “-i” to make the search case–insensitive
Use “*” to search all files in the current (or specified) directory

pwd

pwd

Prints the path of your current directory

ln -sf

ln -sf path/file

Creates a symbolic (-s) link to a file. After, the file appears in both locations, but physically resides in only its original directory. –f ensures that if the target already exists, it is unlinked first so the new link can be created correctly.

For additional resources, check out some of these pages:



You can now go back to Case Studies to run the Initial Exercise.