Logo 
Search:

Unix / Linux / Ubuntu Answers

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds
  Question Asked By: Mrityunjoy Chatterjee   on May 22 In Unix / Linux / Ubuntu Category.

  
Question Answered By: Abhishek Singh   on May 22

UNIX Commands
Below are brief descriptions of the UNIX commands you should know. Remember that you can get more information on a command via the UNIX Manual Pages.

The commands in the examples below are always the first word, while the rest of the words are arguments to the commands.

Task: Make a directory
Example:

mkdir hw1

Mkdir makes a directory that can hold files and other subdirectories. Above, we issued a command to make a directory called hw1 under the current directory.

Task: Change (to another) directory
Example:

cd hw1
OR
cd ..
OR
cd foo/bar

Cd allows you to go to another directory, and thus, change your current directory. Above, we first go into the directory named hw1, which must exist under the current directory. Then, we go to the directory above us (dot dot (..) is shorthand for the directory above). Finally, we go to a directory named bar, under a directory named foo under our current directory.

Task: Find out what is your current directory
Example:

pwd

Pwd always reports the full path of your current directory.

Task: List files, directories or their information
Example:

ls
OR
ls hw1
OR
ls -l prog1.cpp

Ls is used to list files (not their contents). For example, ls above list the files (and subdirectories) in the current directory. Similarly, ls hw1 lists what files are in the subdirectory hw1. In a slightly different usage, ls -l prog1.cpp lists information about the file prog1.cpp, such as how big it is etc. The -l (minus ell, not one) part of the command is a flag for the ls command.

Task: Look at the contents of a text file
Example:

cat hw1.cpp

Spits the contents of a text file on the screen.

Task: Look at the contents of a text file page by page
Example:

less hw1.cpp

To view a longer text file, use the less program followed by the name of the file you want to view, as in the example.

Less will allow you to page forward through the file using the <SPACE> bar and page backward using the b key.

When you are done looking at the file, type q to quit less.

Task: Copy a file
Example:

cp hw1.cpp hw1.cpp.bak
OR
cp testfile1 testfile2 ~/homeworks

Cp makes a copy of a file. This last argument specifies the destination name. The arguments preceding it are the source file(s).

When the destination is a directory, files with the same name as the source files are placed in that directory. In this case, any number of source files (to be copied) may precede the destination directory.

In both cases, since a copy is made, the original source file is left untouched.

When the command would cause a file to be copied over a file that already exists, you may be asked to confirm the copying command. To enforce this confirmation behavior, you may want to use the -i flag with the copy command:

cp -i source destination

In our first example, a backup copy of the file hw1.cpp is made--the copy's name is hw1.cpp.bak and will reside in the current directory.

In the second example, copies of the files testfile1 and testfile2 are made in the directory homeworks, which resides under our home directory (i.e., referred to by the ~).

Task: Move (or rename) a file
Example:

mv writeup.bak writeup
OR
mv hw3.cpp hw3.h ~/homeworks

Mv moves or renames files. The last argument specifies the destination. When the destination includes a path or is the name of a directory, files with the same name as the source files are placed in that path/directory. In this case, any number of source files (to be moved) may precede the destination.

In both cases, the original source file won't exist any more (except with their new names and possibly new locations).

When the command would cause a file to overwrite a file that already exists, you may be asked to confirm the move command. To ensure this confirmation behavior, You may want to use the -i flag with the move command:

mv -i source destination

In our first example, the file writeup is restored from a backup copy (which we could have made with cp) named writeup.bak. After, the file writeup.bak no longer exists. The new file writeup will end up in the current directory.

In the second example, the files hw3.cpp and hw3.h are moved into the directory homeworks, which resides under our home directory (i.e., referred to by the ~). There will no longer be files named hw3.cpp and hw3.h in the current directory.

Task: Remove a file
Example:

rm file1 file2

Removes the files. There is no easy way to undelete these files. They can be retrieved from a backup tape sometimes.

Task: Show the Manual Page (documentation) for a command (or function)
Example:

man ls
OR
man -s3m pow
OR
man -k pow

Man gives documentation for a command, such as what it does, what flags it takes, etc. Man pages are a bit technical, but you get used to using them over time.

You can also look up C(++) library functions. While looking up a library function, as in:

man printf

if you get a UNIX command by the same name instead, try looking in another section of the Manual Pages. You may use the -s flag with man to look up commands/functions in other sections, as in:

man -s3m pow

for math functions in section "3m".

If you are not sure what section a command or function is in, you can use the -k option to man. This will print out a short descriptions for each command/function that contains the text you supply after the -k, as in:

man -k pow
...
pow pow (3m) - power function
power power (9e) - power a device attached to the system
...

As you can see, it prints information about a lot of other things that contain "P-O-W", but it does show us that the pow() function is documented in section 3m.

Task: Compile with the make utility
Example:

make
OR
make -f hw1.mak

The make utility makes compiling more complicated programs easier, more consistent, and more efficient. You run the command make in the directory where your source code for a program exists.

By default, the make utility expects a file called a make file to be present in the same directory. This make file usually specifies how to compile your program. The make utility looks for a make file in a UNIX file named either Makefile or makefile in the directory where make is run. The -f option can be used to tell make to look for a different make file as in the second example.

Note the difference between a make file, which contains instructions on how to compile a program VS. the make utility, which is the program that does all the work (using a make file).

Make will display what commands it is using to compile, etc. as it goes along.

Task: Compile with the g++ compiler
Example:

g++ file1.cpp file2.cpp
OR
g++ -g -o hw1-bank hw1-bank.cpp -lX

g++ is the compiler we are using for C++ programming courses (CS111/112/113). For more complicated programs, you may want to use the make utility for compiling.

The easiest way to compile a program (although not always the best way) is to type g++ followed by the names of all .cpp files that make up the program (as in the first example). By default, g++ creates an executable named a.out.
Flags
With g++, you can use the following flags (others are listed in the Manual Pages for g++).

For example, -o (that's oh, not zero) followed by the name for the executable. In the second example, -o hw1-bank has been used to call the executable hw1-bank instead of a.out.

The -g flag must be used when compiling code that you want to be able to debug with gdb.

The -lX flag at the end of the command tells g++ to link a library with your executable (in the example, the X Windows library). You'll normally be told when you must use the -l (minus ell, not one) option to link in a library.

Remember that compilers give Warning, which you should attend to, but that may not prevent your program from running, and Errors, which will prevent the compiler from generating an executable. Messages often give you the file, line number, and a description of the error as below.

sum.cpp:39: unterminated string or character constant
sum.cpp:32: possible real start of unterminated constant

Resolve the first errors in the code first since they often cause later errors.

Task: Debug executable compiled with g++
Example:

gdb a.out

Gdb is the debugger that goes with the g++ compiler. You always run the debugger on the executable for a program.

To be able to debug an executable, its source code must have been compiled using the -g flag of g++.

The debugger will allow you to run the program, stop it at certain points, and examine/change variables. It also tells you what the offending line is when a program crashes in the debugger.

When you run the debugger on an executable, make sure that the source code files for the program are in the same directory.

Task: Edit or create files with Emacs
Example:

emacs hw1.cpp &
OR
emacs &

Emacs is an editor for text files. Thus, you'll probably want to use it to create and edit your source code files.

If you give a file name with the emacs command, as in the first example, it will load that file if it exists, or start a new one using that name if it does not. Since emacs brings up a window, you should run it with the ampersand (&).

When you are editing in emacs, you are editing a copy of the file, so remember to save edits to disk before you exit emacs.

Task: Submit homework electronically for a CS course
Example:

gsubmit cs112 hw1/

To submit homeworks, you'll typically use the gsubmit command as in the first example. The first argument is always the identifier for the course to which you are submitting. Note that you will usually submit an entire directory which contains files for the assignment.

The first time you submit something to a course, it will ask you for some information.

SEE YOUR INSTRUCTOR OR TEACHING ASSISTANT FOR EXACT INSTRUCTIONS ON SUBMITTING ASSIGNMENTS.

Task: Use & (running in background)
Example:

communicator &
OR
emacs hw1.cpp &

The ampersand (&) at the end of a UNIX command runs the command and gives you the prompt back right away. This way you can continue to type more UNIX commands.

It is useful to put the ampersand at the end of commands that bring up their own windows like a web browser or emacs.

The amperand (&) means something different when used immediately after a greater than (>) for output redirection or after the pipe symbol (|) for piping.

Task: Use < (input redirection)
Example:

a.out < data1

The less than (<) character makes an executable take input from a file instead of waiting for something to be typed at the keyboard. This is called input redirection.

For example, if data1 above contained the following:

2
3.4
4.5

Then, the program a.out would behave as if we typed 2, then hit <RETURN>, typed 3.4, then hit <RETURN>, and finally, typed 4.5 and hit <RETURN>.

Task: Use > (output redirection)
Example:

a.out > output1
OR
g++ file.cpp >& output_and_errors
OR
a.out >> output1

The greater than (>) character sends the output of an executable to a file instead of putting it on the screen. This is called output redirection.

For example, if the executable a.out above normally prints out "Hello, world!" on the screen, then the first example would cause nothing to appear on the screen, but instead, "Hello, world!" would be found in the file output1.

Some output from programs is meant as error messages. Sometimes these will not be redirected by greater than (>). To redirect both regular and error output to a file, use the ampersand after the greater than symbol (i.e., >&), as in the second example.

When you use output redirection, the first thing UNIX does is to create (or clear out, if it exists) the output file (e.g., output1 and output_and_errors in our examples). Be careful with this, since if you try to do something like:

cat a b c > a

You may run into problems since a gets cleared out (the "> a" part) before it is used (the "cat a b c" part).

When you don't want the output file cleared out first, but rather, want the output of the command to be appended to the end of the file, use 2 greater thans (>>), as in the third example. Using the ampersand to redirect both regular and error output also works with appending (i.e., >>&).

Task: Use | (piping)
Example:

a.out | sort
OR
g++ file.cpp |& less
OR
a.out | sort | less

The pipe symbol (|) sends the output of an executable as the input of the command that follows it, instead of putting the output on the screen. This is called piping.

For example, if the executable a.out above normally prints out a bunch of names on the screen, then the first example would cause those names to go as input to the sort command, and then sort would print out the names in sorted order on the screen.

Some output from programs is meant as error messages. Sometimes these will not be piped by |. To pipe both regular and error output to another program, use the ampersand after the pipe symbol (i.e., |&), as in the second example, where the output of the compiler g++ is paged with less.

You can string along several commands with pipes. In the third example, the output of the program a.out is first sorted and then paged with the program less.

Task: Use Up and Down arrows (command history)

On our system, you can use the up and down arrows to scroll through the commands that you typed during the same login session.

This is an easy way to retype or edit commands you've already typed in.

Share: 

 
 
Didn't find what you were looking for? Find more on shell programming Or get search suggestion and latest updates.


Tagged: