Becoming Familiar with Unix Commands


To efficiently work through the practice exercises, it helps to be familiar with, and to understand the meaning of, some of the commonly–used Unix commands. If you are already familiar with Unix, you may skip this exercise and move on to the Initial Exercise. Otherwise, please work through this short exercise to learn more about Unix commands.



Changing Directories

The command used to change directories is cd.


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


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

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



  2. There’s a useful command that allows you to move up only one directory, regardless of where you are. This is specified by typing two dots/periods in a row ( .. ).


    cd ..
    

    You can also move up through several directories with this command. For e.g., first move into one of the directories inside the practice_exercises directory, and then go back to where you are now.


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

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




Listing Directory Content

If you need to know what files and directories exist in your current directory, you can use the ls command, which includes several different options.

  1. Issuing the following will show you all the files and directories in your current directory.


    ls
    


  2. To list the files/directories in “long format”, which includes information such as the size of the file, who owns the file, the file permissions, etc., use the following.


    ls -ls
    


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


    ls /glade/u/home/$USER
    


  4. There are sometimes hidden files, such as your environment files (e.g., .bashrc). You can find those files in the home directory. First change directories to get to your home directory, and then list all the files, including hidden files.


    cd
    
    ls -a
    

    Notice in this case, you didn’t have to specify a directory when you used the cd command. Only when you’re going to your home directory can you do this. Otherwise, you’ll always need to specify a directory after the command.




Renaming, Moving, or Copying Files and Directories

To copy (or make a replica of) a file, use the cp (or “copy”) command. Try copying a file into your /glade/derecho/scratch/$USER/ directory.

  1. First make sure you’re in the right place.


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

    Notice you had to use the full path to the file to locate it, and then take note of the “ . “ at the end. This command asks for the namelist.input file to be moved from where it’s currently located (practice_exercises/wrf/test/em_real/) to the current directory. The dot (” . “) specifies “here.” I.e., we want to move the namelist here.



  2. You can also request the file be moved to another location besides “here.”


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

    Now you’ve put another copy in the practice_exercises directory. You can see that it’s there by doing a listing of that directory.


    ls practice_exercises
    


  3. To move or rename a file, you can use the mv (or “move”) command. Rename the namelist.input file you put in your current directory.


    mv namelist.input copied_namelist.input
    

    Now if you do a listing of the directory (simply type ls), you’ll see the new file there, which used to be namelist.input.



  4. Now try moving that file to another directory.


    mv copied_namelist.input practice_exercises
    


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




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

Shows the first part of a file, as much as will fit on one screen. Just hit the space bar to see more or q to quit.

cat

cat file

Shows the entire file on the screen

head

head file

Shows the first couple of lines of a file on the screen

tail

tail file

Shows the last couple of lines of a file on screen

grep

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

Find lines that match patterns in files
Adding the “-i” makes the search case–insensitive
Use the “*” to search in all files in that directory

pwd

pwd

Shows the directory path you are currently in

ln -sf

ln -sf path/file

Makes a symbolic (-s) link (ln) of a file. The file will appear to be in two locations, but is only physically in one location. (The –f option ensures that if the target file already exists, then it will first be unlinked so that the link may occur correctly.)



For additional resources, check out some of these pages:


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