Basic Unix Commands

From SHellium Wiki

(Redirected from Index:MAN)
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

This is a list of common Unix commands along with examples.

Contents

Basic Unix Commands

Unix commands you need to know.

First of all, for every command there are more options, which can be investigated by typing <command> --help or man <command>.

Basic Unix commands: File Commands

ls

The list command. This command lists files in a directory. Usage:

ls -a

The list command followed by -a. This command lists all files in the directory.

ls -l

The list command followed by -l. This command lists all files followed by file information and the last modified date.

The list command followed by -i. This command lists all files in the directory with their inode.

ls -i

ln

ln <src> <dest>

The link command creates a hard link. This allows two filenames to point to the same file.

ln -s <src> <dest>

The link command followed by -s. This creates a symbolic link, which is similar to a shortcut in Windows.


See full man page at: http://unixhelp.ed.ac.uk/CGI/man-cgi?ln

rm

The remove command. The remove command followed by the file name will remove/delete a file. Usage:

rm my_file

The remove recursivly command. This command will remove an entire directory, including all files and directories inside it (be careful using this command).

rm -rf

mv

mv

The move command can move a file to a new directory or rename the file.

Move to directory:     $mv file_name /newdirectory/
Rename the file:       $mv file_name new_file_name
Rename the file with overwrite protection:   $mv -i file_name new_file_name
Move and rename file:  $mv file_name /path/to/new_dir/new_filename
Move multiple files:   $mv {file1,file2,file3} /path/to/dir/ 
Move file.1,2,3,4,5,6,7,8,9: $mv file.{1..9} /path/to/dir/ 
Move all of the contents in home/downloads directory:   $mv /home/downloads/* /home/backup

cp

cp

The copy command. The copy command will copy a file.

cp my_file /newdirectory/

tar

To archive files together

tar -czf <archive name> <file1> <file2>

To extract .gz archives

tar -zxvf <archive_filename>

chown

To change the owner of a file

chown <user> <file>

chmod

To change the modes of a file (man chmod is suggested to find out the modes)

chmod <modes> <file>

dd

dd - convert and copy a file dd [OPTION]... dd if=(infile) of=(outfile) bs=(blocksize) count=(number of blocks)

SOURCE: me and http://linux.about.com/od/commands/l/blcmdl1_dd.htm

find

find - search for files recursively within a directory.

find -name {name}               # search current directory for name (can use wildcards)
find -name *up.sh               # find files with suffix 'up.sh' in the current dir
find -name *.o                  # find files with extenension '.o' in the current dir
find -name *.sh -o -name *.log  # find files with extension '.sh' or '.log' in the current dir
find / | grep cvs               # find files in the root directory with 'cvs' in their path

Full MAN listing: http://www.manpagez.com/man/1/find

gzip

gzip / gunzip - GNU Compress files into a smaller space, or decompress .Z or .gz files. (Similar to Windows' .zip or .rar)

gzip file.fits          # compresses file.fits into file.fits.gz
gunzip file.fits.gz     # recovers original file.fits
gzip *.dat              # compresses all .dat files into .dat.gz
gunzip *.dat.gz         # decompresses all .dat.gz files into .dat
program | gzip > out.gz # compresses program output into out.gz
program | gunzip > out  # decompresses compressed program output

Full MAN listing: http://www.manpagez.com/man/1/gzip/


tar

tar - Combine files into one larger archive file, or extract files from that archive.

tar -tvf foo.tar                    # list contents of foo.tar
tar -xvf foo.tar                    # extract contents of foo.tar
tar -xzvf foo.tgz                   # extract contents of gzip compressed foo.tgz (also commonly seen as foo.tar.gz)
tar -xjvf foo.tar.bz2               # extract contents of bz2 compressed foo.tar.bz2
tar -cvf foo.tar {file_list}        # tar files in file_list (can include directories) into foo.tar
tar -czvf foo.tgz {file_list}       # tar and compress (gzip) files in file_list into foo.tgz
tar -cjvf foo.tar.bz2 {file_list}   # as above, but with bzip2 compression

Full MAN listing: http://amath.colorado.edu/computing/software/man/tar.html


nano

nano - nano command line text editor

nano <filename>  # opens <filename> in a basic command line text editor
nano <newfile>   # opens a new text file where <newfile> is the file name

Some controls within nano:

Full MAN listing: http://www.manpagez.com/man/1/nano/

sed

sed - stream editor for filtering and transforming text

sed (stream editor) is a Unix utility that (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.
Usage:
sed -e 's/oldstuff/newstuff/g' FileName > OutputFile
generate_data | sed -e 's/x/y/g'
Delete lines that are blank, or only contain spaces:
sed -e '/^ *$/d' FileName

touch

touch - Made to modify access and modification times and dates on a file. Commonly used to create a file.

touch <file>                         # creates a file named <file>
touch myfile.txt                     # creates a blank file named myfile.txt

Full MAN listing: http://linux.die.net/man/1/touch

unrar

Uncompresses .rar files.

Usage:     unrar <command> -<switch 1> -<switch N> <archive> <files...> <@listfiles...> <path_to_extract\>

==Commands==
  e             Extract files to current directory
  l[t,b]        List archive [technical, bare]
  p             Print file to stdout
  t             Test archive files
  v[t,b]        Verbosely list archive [technical,bare]
  x             Extract files with full path

Full help listing: http://pastebin.ca/1306861

vim

vim - vim command line text editor

vim <filename>  # opens <filename> in vim

There are different modes. Press i to go to insert mode (thats what normal text editors are always in). Press esc to go back to standard mode. In standard mode you can press the shift+: "command" IE shift+: q! .

Some common vim commands (in standard mode):

:q!           -- to exit without save
:wq           -- to save and exit
i (or) insert -- to insert/edit text
Shift D       -- to delete line
yy            -- to copy line
p             -- to paste line
Page Up       -- to scroll page up
Page Down     -- to scroll page down
Home          -- to go to beginning of line
End           -- to go to end of line
Standard mode :n --Replace n with a number and you will go to that line number 
Standard mode :/string --will search for "string" can be anything but is case sensitive

Full MAN listing: http://www.csb.yale.edu/userguides/wordprocess/vi_descrip.html

Extra tip for pasting: If you want to paste a long text or code into vi, f.e. when connecting from Windows with putty, first use :set paste before pasting, and you will avoid messing up your line breaks.

wget

wget - download files to directory

wget <url of file>                                             # download file at <url> to the current directory
wget http://www.psybnc.at/download/beta/psyBNC-2.3.2-7.tar.gz  # will download psyBNC-2.3.2-7.tar.gz
wget -c <url>                                                  # will resume downloading file <url> if possible
wget -r <url>                                                  # will download a directory recursively

See: http://wiki.shellium.org/w/Wget Full MAN listing: http://www.manpagez.com/man/1/wget/

Viewing files

less

A scrolling text file viewer

head --will give you the first 10 lines of the file.

$ head -n # file -- will give # number of lines example $head -n 4 example.txt

tail --will give you the last 10 lines of a file.

$ tail -n # file -- will give # number of lines example $tail -n 4 example.txt $ tail -f html.log -- will keep displaying new lines written to the file (very useful for watching log files)

grep

Search for lines in a file. Use grep <pattern> [file] to list all lines in file containing the pattern provided. If file is omitted, grep searches stdin.

cat

Concatenates file and print it on standard output.

To create a new file use an editor such as nano.

nano my_new_file_name

will create a new file named my_new_file_name. Many editors are available for you to choose from.

Basic Unix commands: Directories

To run a file that isn't in a bin/ folder simply type a path to the file

./<filename>

or

/home/user101/progfile

cd

The change directory command. This command followed by a directory name will bring you to the specified directory. Usage:

cd The_directory_I_Wish_To_GOTO

Will take You to The_directory_I_Wish_To_GOTO

cd ..

The change directory command followed by ... This will bring you up one directory.

cd ~

The change directory command followed by ~. This command will bring you to your home directory. This is the same as simply 'cd'.

cd -

The change directory command followed by -. This command will bring you to the previous directory (the last directory you used before 'cd'ing to the current one.

mkdir

The make directory command. The make directory command must be followed by the new directory name. Usage:

mkdir my_new_directory

rmdir

The remove directory command. The remove directory command must be followed by the directory you wish to remove. Usage:

rmdir my_new_directory

Pwd

This will print the present working directory.

Basic Unix commands: Processes

ps

The processes command will print your current background and foreground processes. As well as process ID. Usage:

ps -u your_username

The processes command followed by -u and your username. This will show all processes running under your username as well as their process ID.

kill

The kill command. The kill command followed by the process ID will end a process running in the background or foreground. Usage:

kill -9

The kill command followed by -9 and the process ID is how many administrators say is the proper way to end a process but it is reccomended that you try the kill the process without -9 originally.

kill -9 12345

Kills process with pid id 12345

killall

The killall command will stop all instances of a process running with your username. If you wish to kill all instances of foobar, type

killall foobar

nohup <process> &

Will run your process (program) in the background with SIGHUP trapped. This enables you to exit the shell and the process will continue running. Example:

nohup perl myscript.pl &

suspend

To suspend the foreground process in your terminal, press SUSPEND, which in most cases is CTRL+Z. This will put your process in the background. Example:

vi myscript.pl
Here you use vi to edit myscript.pl  You decide you want to take a quick look at your files.
You press <CTRL+Z>
output:
[1]+  Stopped                 vi
user@shellium:~$

You have now suspended process vi to job 1 and will be placed at your shell prompt.

SIGINT

Sending this signal will normally kill the process in the forground. In most cases, this is CTRL+C. This will send an interrupt to the process. If the process doesn't catch the SIGINT, the process will be terminated.

fg <job id>

Will bring a background job back to the foreground: fg <job id>Example:

fg 1

bg

Will list jobs you have in the background.

jobs

Will list jobs, as well as their current state (running, stopped, etc)

Basic Unix Commands: Miscellaneous Commands

watch <command>

To run command every few seconds, and check its output. Example:

watch ls -l

can be used to check the filesizes of all files in the current directory every few seconds.

finger <user>

Will obtain obtain information about a user like their real name, when they last logged in, and whether or not their terminal is writable.

When you add your .project, .plan and .pgpkey files, this is what people will see when they 'finger' you:

$ finger testuser

Login: testuser                         Name: test
Directory: /home/testuser               Shell: /bin/bash
No mail.
PGP key:
Hi, I'm TestUser and this is my PGP key.
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.0.6 (Darwin)
Comment: For info see http://www.gnupg.org

(...) long PGP key block

-----END PGP PUBLIC KEY BLOCK-----
Project:
Currently working on an open source project at http://sourceforge.net
Plan:
1. Create my public_html folder
2. Fix the IRC bot

Create the files.

$ cd ~
$ touch .project .plan .pgpkey

Now all you need to do is fill the files with stuff.

$ nano .project
$ nano .plan
$ nano .pgpkey

write <user> [tty]

If a user's terminal is writable, this open a pipe to their terminal. Anything you type after running the write command will appear on their terminal. If a tty is omitted, the last used terminal will be automatically be used. End the write session with an EOF (CTRL+D).

passwd

Change your current password. This will prompt you for your current password, as well as a new password to be used.

apropos

useful to help find commands using a keyword search apropos - search the manual page names and descriptions

apropos search terms   # searches the man pages for the given term
apropos --help         # prints all the other options for use with apropos

echo

echo - display a line of text

       echo [OPTION]... [STRING]...

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

       -E     disable interpretation of backslash escapes (default)

       --help display this help and exit

       --version
              output version information and exit


history

history - show a list of recently used commands. Up and down arrows will allow you to scroll through your list on the command line.


"history" command will list your history.

!! will append the last command to the current line. 
$ ls 
$ !! -l
$ !! | egrep '.emp'
 
!$ will take the last argument
$ ls /usr/share/doc
$ cd !$

logout

logout - Closes the current shell. Also try exit.

Ctrl+d also closes the current shell.

mail

mail - send and receive mail

mail [-iInv ] [-s subject ] [-c cc-addr ] [-b bcc-addr ] to-addr...
mail [-iInNv -f ] [name ]
mail [-iInNv [-u user ] ]

More info: http://linux.about.com/od/commands/l/blcmdl1_Mail.htm


man

man - an interface to the on-line reference manuals

man [-c|-w|-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-adhu7V] [-i|-I] [-m system[,...]] [-L locale] [-p string] [-C file] [-M path] [-P pager] [-r prompt] [-S list] [-e extension] [--warnings [warnings]] [[section] page ...] ...
man -l [-7] [-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-p string] [-P pager] [-r prompt] [--warnings[warnings]] file ...
man -k [apropos options] regexp ...
man -f [whatis options] page ...


md5sum

md5sum - compute and check MD5 message digest

md5sum [OPTION] [FILE]... # calculates the files md5sum and prints it

SOURCE: whatis md5sum && man md5sum


motd

$motd 

Message of the Day lists several important items you should be aware of in using your Shellium account.

talk

talk - talk to another user

talk username       # talk to username on server
talk username@host  # talk to username on another host

SOURCE: SlackBook

whatis

whatis - prints a short description of a command

whatis command # prints a short description of the command

whereis

whereis - gives the location of a binary,source or online manual page.

whereis command # gives the place where the command and it's manual page can be found

Full MAN listing: http://www.linfo.org/whereis.html


which

which - locate a command

which [-a] filename ...

who

who - show who is logged on

who [OPTION]... [ FILE | ARG1 ARG2 ]

for looping

Bash is in and of itself a very powerful programming language. For this reason, it has native ability to do for and while looping. With nested bash statements, dictated by either backticks '`' or '$()', it can be used to do automate extremely tedious tasks.

Nested bash statements essentially run the command that is specified, then insert the output of this command in where the command was nested. Looping makes this extremely powerful, as it allows you to define a variable, for each iteration of the loop, then use that variable for whatever command you're running.

For example, If we wanted to run 'host' on every IP connected to shellium over ssh individually to see what they were, we could do it with the following for loop. (Put into scripting syntax for easy reading)

for ip in $(netstat -pant |grep ':22' |awk '{print $5}' |awk -F: '{print $1}'); do
       host $ip;
done

This would produce an output like the following:

04:47:53 v3trae@beetlejuice:~$ for ip in $(netstat -pant |grep ':22' |awk '{print $5}' |awk -F: '{print $1}'); do host $ip; done
253.182.82.68.in-addr.arpa domain name pointer c-68-82-182-253.hsd1.de.comcast.net.
197.248.166.67.in-addr.arpa domain name pointer c-67-166-248-197.hsd1.ga.comcast.net.
41.95.203.173.in-addr.arpa domain name pointer radio.gshellz.org.
80.76.67.68.in-addr.arpa domain name pointer bnc1.shellium.org.
215.17.127.78.in-addr.arpa domain name pointer 215.17.127-78.rev.gaoland.net.
253.199.114.209.in-addr.arpa domain name pointer monolith.natesbox.com.
230.52.137.174.in-addr.arpa domain name pointer 230.52.137.174.ip.caratnetworks.com.
67.80.198.66.in-addr.arpa domain name pointer efnet.as6453.net.

This, while giving you the information you've requested, is not very pretty however. With further awk, and nested bash statements, we can make this output considerable prettier and easier to read.

for ip in $(netstat -pant |grep ':22' |awk '{print $5}' |awk -F: '{print $1}'); do 
          echo "Host record for " $(echo "$ip :"$(host $ip |awk '{print $5}') |column -t); 
done

Which would output something like the following:

04:53:41 v3trae@beetlejuice:~$ for ip in $(netstat -pant |grep ':22' |awk '{print $5}' |awk -F: '{print $1}'); do echo "Host record for " $(echo "$ip :"$(host $ip |awk '{print $5}') |column -t); done
Host record for 68.82.182.253 : c-68-82-182-253.hsd1.de.comcast.net.
Host record for 67.166.248.197 : c-67-166-248-197.hsd1.ga.comcast.net.
Host record for 173.203.95.41 : radio.gshellz.org.
Host record for 68.67.76.80 : bnc1.shellium.org.
Host record for 78.127.17.215 : 215.17.127-78.rev.gaoland.net.
Host record for 209.114.199.253 : monolith.natesbox.com.
Host record for 174.137.52.230 : 230.52.137.174.ip.caratnetworks.com.

While looping

While looping is also extremely powerful, as it allows for the same type of looping as for, but also can give you some other really powerful ways to watch the outputs of commands.

Like the watch command, it is a quick way to see what is going on with a command you're running, while giving you a record of what was output (unlike watch). For example, if you wanted to see an output of what a user is running at any given time, you could do this conveniently with a while loop.

while true; do
     ps aux |grep v3trae;
     echo "=============================="
     sleep 1;
done

This produces an output like the following:

04:58:03 v3trae@beetlejuice:~$ while true; do ps aux |grep v3trae; echo "=================="; sleep 1; done
v3trae    8606  0.0  0.0   6680  2872 ?        Ss   Feb15   0:20 SCREEN
v3trae    8607  0.0  0.1   7848  4392 pts/63   Ss   Feb15   0:00 /bin/bash
root     15675  0.0  0.1  11356  4604 ?        Ss   04:42   0:00 sshd: v3trae [priv]
v3trae   15899  0.0  0.0  11356  3332 ?        S    04:43   0:00 sshd: v3trae@pts/18
v3trae   15903  0.0  0.1   7820  4356 pts/18   Ss   04:43   0:00 -bash
v3trae   17044  0.0  0.0   4952  1028 pts/79   S+   03:31   0:00 screen -x
v3trae   18003  0.0  0.1  11208  5444 pts/63   S+   Feb15   2:24 irssi
v3trae   19421  0.0  0.0   4548  1064 pts/18   R+   04:59   0:00 ps aux
v3trae   19422  0.0  0.0   3948   856 pts/18   S+   04:59   0:00 grep --color=auto v3trae
root     27822  0.0  0.1  11368  4556 ?        Ss   Feb22   0:01 sshd: v3trae [priv]
v3trae   27955  0.0  0.0  11500  3436 ?        S    Feb22   0:07 sshd: v3trae@pts/79
v3trae   27957  0.0  0.1   7820  4332 pts/79   Ss   Feb22   0:00 -bash
==================
v3trae    8606  0.0  0.0   6680  2872 ?        Ss   Feb15   0:20 SCREEN
v3trae    8607  0.0  0.1   7848  4392 pts/63   Ss   Feb15   0:00 /bin/bash
root     15675  0.0  0.1  11356  4604 ?        Ss   04:42   0:00 sshd: v3trae [priv]
v3trae   15899  0.0  0.0  11356  3332 ?        S    04:43   0:00 sshd: v3trae@pts/18
v3trae   15903  0.0  0.1   7820  4356 pts/18   Ss   04:43   0:00 -bash
v3trae   17044  0.0  0.0   4952  1028 pts/79   S+   03:31   0:00 screen -x
v3trae   18003  0.0  0.1  11208  5444 pts/63   S+   Feb15   2:24 irssi
v3trae   19465  0.0  0.0   4548  1064 pts/18   R+   04:59   0:00 ps aux
v3trae   19467  0.0  0.0   3948   852 pts/18   S+   04:59   0:00 grep --color=auto v3trae
root     27822  0.0  0.1  11368  4556 ?        Ss   Feb22   0:01 sshd: v3trae [priv]
v3trae   27955  0.0  0.0  11500  3436 ?        S    Feb22   0:07 sshd: v3trae@pts/79
v3trae   27957  0.0  0.1   7820  4332 pts/79   Ss   Feb22   0:00 -bash
==================
v3trae    8606  0.0  0.0   6680  2872 ?        Ss   Feb15   0:20 SCREEN
v3trae    8607  0.0  0.1   7848  4392 pts/63   Ss   Feb15   0:00 /bin/bash
root     15675  0.0  0.1  11356  4604 ?        Ss   04:42   0:00 sshd: v3trae [priv]
v3trae   15899  0.0  0.0  11356  3332 ?        S    04:43   0:00 sshd: v3trae@pts/18
v3trae   15903  0.0  0.1   7820  4356 pts/18   Ss   04:43   0:00 -bash
v3trae   17044  0.0  0.0   4952  1028 pts/79   S+   03:31   0:00 screen -x
v3trae   18003  0.0  0.1  11208  5444 pts/63   S+   Feb15   2:24 irssi
v3trae   19524  0.0  0.0   4548  1068 pts/18   R+   04:59   0:00 ps aux
v3trae   19525  0.0  0.0   3948   852 pts/18   S+   04:59   0:00 grep --color=auto v3trae
root     27822  0.0  0.1  11368  4556 ?        Ss   Feb22   0:01 sshd: v3trae [priv]
v3trae   27955  0.0  0.0  11500  3436 ?        S    Feb22   0:07 sshd: v3trae@pts/79
v3trae   27957  0.0  0.1   7820  4332 pts/79   Ss   Feb22   0:00 -bash
==================

For every instance of "============" a second goes by. This can be extremely useful for watching MySQL queries that are being run, and many other things.

While looping is also extremely helpful when you want to strace a quickly running process, such as a php page load, that you cannot trigger from shell using the php cli binary. For example, lets say that I need to strace my irssi session, but whenever it runs, it only runs for a quick second, then dies. I could achieve this by doing the following:

while true; do ps aux |grep v3trae |egrep -v "SCREEN|bash|sshd|screen|aux|grep"** |awk '{print "strace -p "$2}' |bash; done

This will automatically attach to the process that is excluded from the 'egrep -v' and show you what it's doing.

05:02:43 v3trae@beetlejuice:~$ while true; do ps aux |grep v3trae |egrep -v "SCREEN|bash|sshd|screen|aux|grep" |awk '{print "strace -p "$2}' |bash; done
Process 18003 attached - interrupt to quit
restart_syscall(<... resuming interrupted call ...>) = 1
gettimeofday({1298541773, 458314}, NULL) = 0
read(3, ":Sacred|!~Someone@74-131-10-155."..., 2048) = 127
time(NULL)                              = 1298541773
time(NULL)                              = 1298541773
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3519, ...}) = 0
time(NULL)                              = 1298541773
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3519, ...}) = 0
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3519, ...}) = 0
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3519, ...}) = 0
time(NULL)                              = 1298541773
time(NULL)                              = 1298541773
time(NULL)                              = 1298541773
time(NULL)                              = 1298541773
time(NULL)                              = 1298541773

V3trae 05:22, 24 February 2011 (EST)

Background Processes

jobs - list background processes in current shell
bg - send and start currently stopped process to background
fg - the last background process to the foreground
fg num bring the process num to the foreground

Press Control-Z to pause a process. Then run "bg" to send / start the process to the background. "Note: I know this works in bash but I'm unsure of the rest of the shells because these are built into bash"

SOURCE: SlackBook

Shells

Personal tools
Namespaces
Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox