Vote for us in Top 100 Security Sites
 |
|
|
Basic Unix commands to get you started |
|
|
|
Written by www.n0cmonkey.net
|
|
Wednesday, 05 January 2005 |
In an attempt to help the move from MS Windows, here is a list of basic Linux commands.
This is very basic, a little Linux oriented, but a decent start.
Getting help: man command - Almost always shows you the manual for the specified program. When told to 'RTFM', this is what you must do. man -k word - Kind of like a search engine for man. apropos word - This is the same as man -k.
Moving around the file system:
Navigating the Unix file system is very similar to DOS. Some of the commands are a little different, but they behave the same. cd dirname - Changes to the specified directory. cd .. - Changes to the parent of the current directory. pwd - Prints the current directory. Usefull if, for whatever reason, your prompt doesn't give this info. ls - Directory listing. use the -lh switch to get a detailed listing. Use the -a switch to show hidden files.
Dealing with archives: tar - This command is used to group multiple files together. It
stands for Tape ARchiver. There are several versions of tar, and not
all perform the same way. GNU tar appears to have the most features.
The typical extensions for tarballs are .tar, .tar.gz, and .tgz. The
extensions .tgz and .tar.gz are compressed tarballs using gzip. The -x
flag unpacks a tarball, the -v enables verbose unpacking, and -f
followed by a filename(s) tells tar which file(s) to work with. To
untar an uncompressed file (.tar): tar -xf filename.tar. To untar and uncompress a tarball (.tar.gz, .tgz): tar -zxf filename.tar.gz.
Notes:
-GNU tar (gtar) can handle bzip2 files as well as gzipped files. It
appears to be the only implimentation of tar that will handle bzip2
files. The flag for this is -j.
-SUN's tar cannot handle gzip compression or long filenames. To unpack
the Apache source code, gtar is available in Solaris packages. compress and uncompress - These commands will compress
and uncompress files. These commands are old and generally not used,
and are only mentioned for historical purposes. The extension for
compressed files is generally .Z. gzip and gunzip - This command will compress or uncompress a file. The syntax is simple: gzip filename or gunzip filename.tgz.
The compression can be increased or decreased by adding a -x flag to
the command, where x stands for a number between 0 and 9. The default
for this option is probably the best ratio to use for general purposes. bzip2 and bunzip2 - These commands will compress or
uncompress a file. The compression of bzip2 is generally better than
that of gzip, but is less standard. The extension for bzipped files is
generally .bz2. The syntax is quite simple: bzip2 filename and bunzip2 filename.bz2.
Viewing and editing files: cat - Outputs the contents of a text file. Only really useful on small files. more - A somewhat simple text file reader (called a pager). Hit a key to go to the next page. Works a lot like the one in DOS. vi file - A really nice text editor, albeit a bit tricky to use.
Basic understanding of vi is imperitive, since it (oe a clone) appears
on almost every Unix and Unix-like system. Here is a tutorial for vim, which is a common vi clone available on Linux distros. less and most - These are two other pagers that are often
used instead of more. Each adds features that more may or may not have,
including color. head - This command outputs the top few lines of a file. tail - This command outputs the bottom few lines of a file. The
-f flag will continue to display new lines appended to the file as they
are added.
Commands you should know: grep - Searches through a file for a string and outputs the contents of that line. The syntax: grep 'string' filename.
There are a number of flags that can be used with grep, and a thorough
reading of the man page is recommended. A couple of useful flags are:
-v to find everything NOT matching the string, and -i to search in a
case insensitive manner. find - Searches the filesystem for files based on criteria supplied by the user. Syntax: find / -name "passwd". There are a number of useful flags and options for find, which will be explained in the man page. file - Determines the file type of a file. ps - This command displays processes running on the system.
Running ps without any options or arguments will return all processes
for the current session. Depending on the version and distribution of
ps, different options are available. On SVR4 systems, ps -ef will display all processes running on the system. On a BSD based system, ps -aux will do the same. I like using ps -auxww to get the full commands running on the system. GNU ps can use either ps -ef or ps aux
(and the ww if you prefer that output). Notice there is no dash before
the aux though, this is supposed to be a POSIXism, and may cause issues
on some Linux distributions. There may be more on this later, but check
the manpage to get an idea of how to read the output. history - This command will list previously executed commands.
This should work with the following shells: ksh, pdksh, csh, tcsh, and
bash. It may work with other shells. Depending on how the history
function is setup, it may continue tracking the history through
different sessions. The recently released bash-3 can timestamp history
entries. Consult the manpage for your shell for more information on
setting this up to your liking. env - This command displays the environment variables set for the session. ln - Use this to create a link. Symlinks are what is typically used, the -s flag is necessary for this. chmod - This will change the permissions on a file. The user
must have appropriate ownership rights to change the permissions. The
syntax of the command is: chmod xoge filename. x can set the
user id, group id, or sticky bit of the file (optional). o is the
permissions for the owner. g is the group permissions, and e is for
everyone that is not the owner or in the proper group. The permissions
are simple: read, write, and execute. Each permission type is
represented by a number. Read is 4, write is 2, and execute is 1. To
change the permissions, add the numbers of the access to be given to
the user. If the owner wants to allow a file to be executed by only
him, he would use the command chmod 700 filename. There is a recursive option (-R) to easily change the permissions of a directory tree. chown - This can change the owner and/or group of a file. Syntax: chown user filename.
To change the group, either use the chgrp command or include the group
on the chown command line. The group can be included using : or .
between the username and the group name, depending on the system. There
is a recursive option (-R) to easily allow the change of ownership to a
directory tree. chgrp - This chan change the group of a file. Syntax: chgrp group filename. There is a recursive option (-R) to easily change the group for a directory tree. ifconfig - This can change or display the configuration of a network card. To display the configurations use: ifconfig -a. Check the manpage for information on bringing up a network card using ifconfig. df - This displays some disk information. The -h option
(available on BSD, Linux, and maybe newer versions of Solaris) will
show the disk usage in human readable format. If the -h option is not
available, use -k. du - This command shows the file size of any files included on
the command line. The -h option (available on BSD, Linux, and maybe
newer versions of Solaris) will show the disk usage in human readable
format. The command: du -hs * will give you the file sizes of all files in the current directory. If the -h option is not available, use -k. mount - This command will either mount, or show information on
mounted filesystems. Used without options, the output will have
information on the mounted filesystems. Read the manpage for
information on mounting filesystems, it can differ slightly between
systems. locate - This command will display all filenames with full paths
matching the text string given. This may not be available on all
systems, but should work fine in Linux, BSD, and solaris (atleast v7+).
locate uses a database to determine where files are stored. This
database updates at night. Review the manpage for locate to find out
how to update the database manually. which - This command searches your $PATH for a file with the name given on the command line. su - This is the Switch User command. It will
prompt for the password of the user being switched to. If no user is
listed, su will try to switch to the root user. A - (dash) simulates a
full login. This should change the PATH, HOME, SHELL, USER and other
environment variables.
IO manipulation: | - (The pipe, shares a key with the back slash.) This is one of
the most important commands on a unix system. The pipe takes the output
from a command on the left side and uses that as the input for a
command on the right side. For example: yes n | mv -i *.txt ../texts will move every file ending in .txt into the ../texts directory, except when there is a file with the same name.
` - (The back tic, shares a key with the tilde.) Any
commands inside of a set of back tics will be run before any other
commands.
> - This will put all output from a command into a file. This will over write anything in the file, so be careful.
>> - This will append the output from a command into a file.
< - This will use a file as the input for a command.
Environment variables:
To set an environment variable use VARIABLE=stuff;export VARIABLE for bourne based shells, and setenv VARIABLE stuff for c shells.
PATH - Tells the shell where to look for programs if you don't
use an absolute path. It works like the DOS PATH variable, but with
colons (:) instead of semicolons (;) to seperate the entries. For
example:
/bin:/usr/bin:/usr/local/bin:/opt/bin:/usr/X11R6/bin:/opt/mozilla DISPLAY - Tells the machine where to display graphical applications. PAGER - This environment variable is used to inform the shell of the prefered pager (more, less, or most). EDITOR - This environment variable is used to inform the shell of the prefered editor (vi, nano, emacs, etc.). HOME - The location of the home directory. SHELL - The shell in use. CC - The C compiler to be used on the system.
Key combinations: ctrl alt backspace - This will exit X. If a graphical login
manager is used (xdm, gdm, kdm), it should respawn and present a login
window. Using this key combination multiple times may kill the login
manager. Virtual terminals - On Linux and BSD systems a user can switch
between multiple virtual terminals. Virtual terminals are text based
terminals to the system. Accessing a virtual terminal is a bit
different between Linux and BSD (atleast OpenBSD, anyone feel free to
confirm or correct me). ctrl alt Fx where Fx is a function key will change virtual terminals in OpenBSD. In linux, it should be as simple as alt Fx
(again, someone correct me here if I'm mistaken :)). If X is running
and on the screen, the addition of the ctrl button should be necessary
. ctrl alt delete - This may or may not reboot your system.
Other programs that have proven useful: screen - Screen is a virtual terminal that fits in one window.
This is especially useful when working on a remote machine over an SSH
link, or at a Sun machine that lacks virtual terminals. It allows a
user to have multiple prompt when logged into a text terminal, and
switch between them with simple key combinations. ctrl a c will create a new window, and ctrl a x;
where x stands for n, p, or a number; will switch between the open
windows. There are many options, and the key combinations are user
configurable, so consult the manpage for further help.
One of the great things about unix is the ability to string commands along, using utilities like the pipe. For example: cat *.html | grep -v `date +%Y%m%d` | grep root > stuff.txt
One of the main philosophies of unix is small tools that do limited
things, but can be used together to perform complicated tasks.
Dan Bernstein has a few notes on Unix portability.
|
|
Last Updated ( Wednesday, 05 January 2005 )
|
|
|
2006 I-Hacked T-Shirts |
I-Hacked T-Shirts Have been released, Now with 3 versions! Check them out here
|
|