Unix shell commands

Shell is the program that interacts with your keyboard. It accepts your command, interprets it and executes it. Popular shells are the tcsh, ksh, bash, etc. To change your shell program to a particular type, do the following, then logout and login again to start using the new shell:

  • On Mac or Unix, use the 'chsh' command, give your password, then give the path to your intended shell. Usually shells are in /bin directory, like /bin/tcsh, /bin/bash, etc.
  • On Cygwin under Windows, you need to change two places. The batch file at \cygwin\Cygwin.bat contains the shell command used if you start the cygwin terminal. Change that to the one you like, say, by commenting out bash and adding 'tcsh -l'. To change the shell used under X11/xterm, you need to modify the shell command listed under /etc/passwd file, which maps to the \cygwin\etc\passwd file.

Note that the shell program will interpret certain special characters before it passes the result to the command for execution. In particular, the * symbol will be expanded to all files in your current directory, and the ? symbol means any single character. Therefore, 'echo *' will show all files in your directory, just like 'ls' will do. Sometimes, you want to prevent this interpretation. Use the single or double quotes to enclose the * or ?, and then they will not be interpreted by the shell. The most notably example is when using the 'find' command, which DOES want to see the asterisk symbol directly.

 

Some common file and directory manipulation commands like ls, cd, rm, cp, mv, mkdir, rmdir are explained in the Unix command list document, so we will not repeat them here. To find out how to type their arguments, use the 'man' command to find out. Also try the 'man man' command to see how the man command explains itself.

 

Filesystem is the structuring of files into different directories to organize your data. Under Unix/Cygwin/Mac, all files start from the root indicated by the / symbol. Generally when you just login your computer, you are in your home directory, which can be different depending on your computer type:

  • /home/your_id for Unix. If you have a Desktop directory in there, it maps to your GUI desktop.
  • /Users/your_id for Mac. The Desktop directory there also maps to your Mac desktop.
  • /home/your_id for Cygwin, but it is actually mapped to \cygwin\home\your_id, while your normal windows files are stored in C:\Documents and Settings\your_id and your Desktop files are stored in C:\Documents and Settings\your_id\Desktop. To map that back to the cygiwn filesystem so you can refer to them under Cygwin terminal, change C: to /cygdrive/c, as in /cygdrive/c/Documents and Settings/your_id. As you can see, I use forward slash symbol / to mean Unix filesystem paths, while I use backslash \ symbol to indicate Windows filesystem paths. If you find it too much trouble to have to go between your home and your Desktop directory, just link the Desktop to your home direcotry
    ln -s /cygdrive/c/Documents\ and\ Settings\your_id\Desktop .
    Don't overlook the tiny dot at the end of the command. After typing this command, you will have a Desktop directory in your home directory that links to your actual desktop.

'find' is the command we used to locate files in some directory hierachy structure. A few examples:

  • find files containing Apple in their names
    find . -name '*Apple*'
  • find files created in the past 14 days
    find . -mtime -14
  • find files created in the past 10 minutes
    find . -mmin -10

You can also do something directly with the files you found. A few examples:

  • find and copy files to your Desktop which contains Apple in their names, you need to replace /home/your_id to your actual home directory. To find out, type the 'pwd' command
    find . -name '*Apple*' -exec cp '{}' /home/your_id/Desktop \;
  • find and remove all files begin with the core keyword in their names (this command is dangerous, so don't try it)
    find . -name 'core*' -exec rm '{}' \;

Under Unix, the input to a command can come from your keyboard, a file, or another command's output. First, to collect the output from a command to a file instead of sending them to your screen, use the output redirection symbol >, like "ls > output.txt' will collect the output from the 'ls' command to output.txt file. Similarly, to have a command read from your file instead of your keyboard, use the input redirection symbol <, like "cat < input.txt'. Finally, to have a command takes input from another command, use the pipe | symbol to pipe them together, as in 'date | cat'. There will be more examples of these kinds of Input/Output redirection uses in the future lectures.

 

The 'ssh' command allows you to remotely access a computer. For example, to connect to the CS class account, type 'ssh pyrite.cs.iastate.edu'. In case your login ID on your local machine and the remote machine are not the same, tell ssh explicitly which ID you are going to login on the remote machine like this 'ssh remote_id@pyrite.cs.iastate.edu'.

 

The companion 'scp' command allows you to copy files between your local computer and a remote machine. The first argument to scp is always the source, and the second argument is the destination. Thus, to copy some_file from your local machine to the CS account, you type 'scp some_file pyrite.cs.iastate.edu:.', and to copy it back, you reverse the arguments 'scp pyrite.cs.iastate.edu:some_file .'. Note that you need to think based on where you issue the scp command, not on where YOU are. Therefore, if you already ssh'ed to the remote machine, the local machine will then be the remote one! Think clearly from scp's perspective where should be source and destination.

 

Popular text editors on Unix include emacs and vi. The companion Unix command list document also mentioned pico. You may also use your familiar text editor in your platform, like Notepad for Windows, and TextEdit for Mac. However, do note that in different platforms, the line ending character is not the same:

  • Windows uses carriage-return and linefeed together to indicate the end of a line.
  • Mac uses carriage-return to indicate the end of a line, but also recognizes the other styles.
  • Linux uses linefeed alone to indicate the end of a line.

Most of the time, if copying files from one type of computers to another type, your editor can still recognize the line ending symbols and break the line up correctly. If somehow it does not recognize it, most likely when you copy Mac or Unix files to Windows, use the following Perl commands to convert them:

  • To convert Unix files to Windows
    perl -i.bak -p -e 's/\n/\r\n/' file_to_convert
  • To convert Windows files to Mac
    perl -i.bak -p -e 's/\r\n/\r/'
  • To convert Mac files to Unix
    perl -i.bak -p -e 's/\r/\n/'

In all cases, your original file will be backed up to .bak.

 

We have only touched base briefly on Unix shell environments and some basic commands. It is not possible that we cover all useful commands and their usage in one lecture. However, we will be working under the Unix environment for most of our projects, therefore we will have plenty of chances to revisit and/or to introduce new commands in subsequent lectures. Stay tuned.

 

Last modified July 13, 2007. All rights reserved.