Unix Commands Flashcards
Clear
clear
-clears the current the screen and displays prompt at the top of the screen
pwd
pwd
-shows the absolute path (from the root) to the present working directory (i.e. the directory you are currently in)
LS
The output from ls contains many pieces of information.
Each line represents one directory or file. The first character of the line indicates
what it is.
If the first character is a “d”, the item is a directory.
If the first character is a dash “-“, then the item is simply a file.
The next set of characters indicate file permission bit
CD
to move between directories, we use the cd (change directory) command
If you’re moving to a directory in the current directory, you can use cd dirname, where “dirname” is the name of the directory (case sensitive)
mkdir
The mkdir command stands for \make directory”.
It is used to create a directory or folder.
Syntax: mkdir dirname
Here, “dirname” is the name of the new directory you want to create.
This will create a directory with the given name in the current directory.
rmdir
The rmdir command stands for \remove directory”.
It is used to delete the given (empty) directory.
Syntax: rmdir dirname
Here, \dirname” is the name of the new directory you want to delete.
This will delete a directory with the given name in the current directory, as
long as it is empty. It will fail if the directory contains other files or directories.
These files have to be deleted first, before deleting the directory.
cp
The cp command is used to copy files.
Syntax: cp sourcefile destinationfile
mv
The mv command is use to move a file.
Syntax: mv sourcefile destinationfile
rm
The rm command is used to remove (delete) a file.
Syntax: rm filename.
Here, “filename” is the name of the file you want to delete.
Once the file is deleted, it has been deleted forever, and cannot be
recovered.
using wild cards
In any unix commands that deal with paths and file names, you can use wild
cards.
The wild card characters are the question mark (?) and the asterisk (*).
The question mark ? can be substituted in a filename, and it will represent
any character in that position.
For example consider the command mv prog?.cpp ./temp
The ? in the above command says that the command applies to any filename
matching that pattern, in which the ? can represent any character (like a wild
card in a poker hand).
man
man refers to \manual”, and unix systems provide pages of the manual that
can be accessed from the command prompt.
You can get further information on any unix command or system call with the
command:
man <commandname></commandname>
file permissions - chmod
You have control over the permissions on your own directories and files.
You can limit permissions to just yourself, or you can allow other people to
access your les and run your programs.
You can do this on a file by file basis, if you like. To change your file
permissions, you use the chmod (change mode) command.
There are several ways to use the chmod command, but the easiest format
is:
chmod <permission> <file>
The permission list can be given with a numerical code. The basic format is
to use a 3 digit number (4 digits are possible, for more advanced options).</file></permission>
more/less
The “more” command in unix allows you to display a file one screenful at a
time.
After each screenful, you will see “-More-“ at the bottom.
You can advance the file one line at a time with the Return key, or you can
advance a page at a time with the Spacebar.
“less” is a command that is similar to “more” and is available in some unix
systems.
It allows the viewing of a file, like “more”, but it also allows backward
movement through the file while viewing, as well as forward movement.
diff
diff compares two text files ( can also be used on directories) and prints the lines for which the files differ. The
format is as follows:
diff [options] <original> <newfile>
Some options
* [-b] Treats groups of spaces as one
* [-i] Ignores case
* [-r] Includes directories in comparison
* [-w] Ignores all spaces and tabs</newfile></original>
<original> - Specifies one file to compare
<newfile> - Specifies other file to compare
Example:
diff -w testprog1.c testprog2.c
</newfile></original>
grep
grep is a very useful utility that searches files for a particular pattern.
The pattern can be a word, a string enclosed in single quotes, or a regular expression.
usage
●grep int .c (find all occurences of the pattern ‘int’ in all files with a .c extenstion)
●grep ‘main()’ testprog1.c (enclosing the pattern in quotes is useful when using special characters)
●grep ‘m.n’ myfile (the . matches a single character, the .* matches any number of characters; this
finds anything starting with an m and ending with an n)
The way that regular expressions can be described is somewhat complex in grep: see the following
tutorial for more help:
grep has many options; a few are noted below
-i ignore case -n display the line numbers
-l display only names of files and not actual lines