В началоUnix Toolbox → 14. USEFUL COMMANDS
Gentoo-doc HOME Пред.: 13. SVNВ началоУровень выше: Unix ToolboxСлед.: 15. INSTALL SOFTWARE

14. 14. USEFUL COMMANDS

14.1. 14.1 less

The less command displays a text document on the console. It is present on most installation.

# less unixtoolbox.xhtml

Some important commands are (^N stands for [control]-[N]):

h H good help on display

f ^F ^V SPACE Forward one window (or N lines).

b ^B ESC-v Backward one window (or N lines).

F Forward forever; like "tail -f".

/pattern Search forward for (N-th) matching line.

?pattern Search backward for (N-th) matching line.

n Repeat previous search (for N-th occurrence).

N Repeat previous search in reverse direction.

q quit

11.http://www.cs.put.poznan.pl/csobaniec/Papers/svn-refcard.pdf

12.http://tortoisesvn.tigris.org

14.2. 14.2 vi

Vi is present on ANY Linux/Unix installation and it is therefore useful to know some basic commands. There are two modes: command mode and insertion mode. The commands mode is accessed with [ESC], the insertion mode with i.

Quit

:w newfilename save the file to newfilename

:wq or :x save and quit

:q! quit without saving

Search and move

/string Search forward for string

?string Search back for string

n Search for next instance of string

N Search for previous instance of string

{ Move a paragraph back

} Move a paragraph forward

1G Move to the first line of the file

nG Move to the n th line of the file

G Move to the last line of the file

:%s/OLD/NEW/g Search and replace every occurrence

Delete text

dd delete current line

D Delete to the end of the line

dw Delete word

x Delete character

u Undo last

U Undo all changes to current line

14.3. 14.3 mail

The mail command is a basic application to read and send email, it is usually installed. To send

an email simply type "mail user@domain". The first line is the subject, then the mail content.

Terminate and send the email with a single dot (.) in a new line. Example:

# mail c@cb.vu

Subject: Your text is full of typos

"For a moment, nothing happened. Then, after a second or so,

nothing continued to happen."

.

EOT

#

This is also working with a pipe:

# echo "This is the mail body" | mail c@cb.vu

This is also a simple way to test the mail server.

14.4. 14.4 tar

The command tar (tape archive) creates and extracts archives of file and directories. The archive .tar is uncompressed, a compressed archive has the extension .tgz or .tar.gz (zip) or .tbz (bzip2). Do not use absolute path when creating an archive, you probably want to unpack it

Create

# cd /

# tar -cf home.tar home/ # archive the whole /home directory (c for create)

# tar -czf home.tgz home/ # same with zip compression

# tar -cjf home.tbz home/ # same with bzip2 compression

Only include one (or two) directories from a tree, but keep the relative structure. For example

archive /usr/local/etc and /usr/local/www and the first directory in the archive should be local/.

# tar -C /usr -czf local.tgz local/etc local/www

# tar -C /usr -xzf local.tgz # To untar the local dir into /usr

# cd /usr; tar -xzf local.tgz # Is the same as above

Extract

# tar -tzf home.tgz # look inside the archive without extracting (list)

# tar -xf home.tar # extract the archive here (x for extract)

# tar -xzf home.tgz # same with zip compression

# tar -xjf home.tgz # same with bzip2 compression

# tar -xjf home.tgz home/colin/file.txt # Restore a single file

More advanced

# tar c dir/ | gzip | ssh user@remote 'dd of=dir.tgz' # arch dir/ and store remotely.

# tar cvf - `find . -print` > backup.tar # arch the current directory.

# tar -cf - -C /etc . | tar xpf - -C /backup/etc # Copy directories

# tar -cf - -C /etc . | ssh user@remote tar xpf - -C /backup/etc # Remote copy.

# tar -czf home.tgz --exclude '*.o' --exclude 'tmp/' home/

14.5. 14.5 dd

The program dd (disk dump) is used to copy partitions and disks and for other copy tricks.

Typical usage:

# dd if=<source> of=<target> bs=<byte size> conv=<conversion>

Important conv options:

notrunc do not truncate the output file, all zeros will be written as zeros.

noerror continue after read errors (e.g. bad blocks)

sync pad every input block with Nulls to ibs-size

The default byte size is 512 (one block). The MBR, where the partiton table is located, is on the first block, the first 63 blocks of a disk are empty. Larger byte sizes are faster to copy but require also more memory.

Backup and restore

# dd if=/dev/hda of=/dev/hdc bs=16065b # Copy disk to disk (same size)

# dd if=/dev/sda7 of /home/root.img bs=4096 conv=notrunc,noerror # Backup /

# dd if /home/root.img of=/dev/sda7 bs=4096 conv=notrunc,noerror # Restore /

# dd bs=1M if=/dev/ad4s3e | gzip -c > ad4s3e.gz # Zip the backup

# gunzip -dc ad4s3e.gz | dd of=/dev/ad0s3e bs=1M # Restore the zip

# dd bs=1M if=/dev/ad4s3e | gzip | ssh eedcoba@fry 'dd of=ad4s3e.gz' # also remote

# gunzip -dc ad4s3e.gz | ssh eedcoba@host 'dd of=/dev/ad0s3e bs=1M'

# dd if=/dev/ad0 of=/dev/ad2 skip=1 seek=1 bs=4k conv=noerror # Skip MBR

# This is necessary if the destination (ad2) is smaller.

Recover

The command dd will read every single block of the partiton, even the blocks. In case of problems it is better to use the option conv=sync,noerror so dd will skip the bad block and write zeros at the destination. Accordingly it is important to set the block size equal or smaller than the disk block size. A 1k size seems safe, set it with bs=1k. If a disk has bad sectors and the data should be recovered from a partiton, create an image file with dd, mount the image and copy the content to a new disk. With the option noerror, dd will skip the bad sectors and write zeros instead, thus only the data contained in the bad sectors will be lost.

# dd if=/dev/hda of=/dev/null bs=1m # Check for bad blocks

# dd bs=1k if=/dev/hda1 conv=sync,noerror,notrunc | gzip | ssh \ # Send to remote

root@fry 'dd of=hda1.gz bs=1k'

# dd bs=1k if=/dev/hda1 conv=sync,noerror,notrunc of=hda1.img # Store into an image

# mount -o loop /hda1.img /mnt # Mount the image (page 12)

# rsync -ax /mnt/ /newdisk/ # Copy on a new disk

# dd if=/dev/hda of=/dev/hda # Refresh the magnetic state

# The above is useful to refresh a disk. It is perfectly safe, but must be unmounted.

Delete

# dd if=/dev/zero of=/dev/hdc count=1 # Delete MBR and partiton table

# dd if=/dev/zero of=/dev/hdc # Delete full disk

# dd if=/dev/urandom of=/dev/hdc # Delete full disk better

# kill -USR1 PID # View dd progress (Linux only!)

14.6. 14.6 screen

Screen has two main functionalities:

• Run multiple terminal session within a single terminal.

• A started program is decoupled from the real terminal and can thus run in the

background. The real terminal can be closed and reattached later.

Short start example

start screen with:

# screen

Within the screen session we can start a long lasting program (like top). Detach the terminal

and reattach the same terminal from an other machine (over ssh for example).

# top

Now detach with Ctrl-a Ctrl-d. Reattach the terminal with

# screen -r

or better:

# screen -R -D

Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user.

Screen commands (within screen)

All screen commands start with Ctrl-a.

• Ctrl-a ? help and summary of functions

• Ctrl-a c create an new window (terminal)

• Ctrl-a Ctrl-n and Ctrl-a Ctrl-p to switch to the next or previous window in the list, by

number.

• Ctrl-a Ctrl-N where N is a number from 0 to 9, to switch to the corresponding window.

• Ctrl-a " to get a navigable list of running windows

• Ctrl-a a to clear a missed Ctrl-a

• Ctrl-a Ctrl-d to disconnect and leave the session running in the background

• Ctrl-a x lock the screen terminal with a password

— Useful Commands —

39

The screen session is terminated when the program within the running terminal is closed and you logout from the terminal.

14.7. 14.7 Find

Some important options:

-x (on BSD) -xdev (on Linux) Stay on the same file system (dev in fstab).

-exec cmd {} \; Execute the command and replace {} with the full path

-iname Like -name but is case insensitive

-ls Display information about the file (like ls -la)

-size n n is +-n (k M G T P)

-cmin n File's status was last changed n minutes ago.

# find . -type f ! -perm -444 # Find files not readable by all

# find . -type d ! -perm -111 # Find dirs not accessible by all

# find /home/user/ -cmin 10 -print # Files created or modified in the last 10 min.

# find . -name '*.[ch]' | xargs grep -E 'expr' # Search 'expr' in this dir and below.

# find / -name "*.core" | xargs rm # Find core dumps and delete them

# find / -name "*.core" -print -exec rm {} \; # Other syntax

# find . \( -name "*.png" -o -name "*.jpg" \) -print

# iname is not case sensitive

# find . \( -iname "*.png" -o -iname "*.jpg" \) -print -exec tar -rf images.tar {} \;

# find . -type f -name "*.txt" ! -name README.txt -print # Exclude README.txt files

# find /var/ -size +1M -exec ls -lh {} \;

# find /var/ -size +1M -ls # This is simpler

# find . -size +10M -size -50M -print

# find /usr/ports/ -name work -type d -print -exec rm -rf {} \; # Clean the ports

Find files with SUID; those file have to be kept secure

# find / -type f -user root -perm -4000 -exec ls -l {} \;

14.8. 14.8 Miscellaneous

# which command # Show full path name of command

# time command # See how long a command takes to execute

# time cat # Use time as stopwatch. Ctrl-c to stop

# set | grep $USER # List the current environment

# cal -3 # Display a three month calendar

# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

# date 10022155 # Set date and time

# whatis grep # Display a short info on the command or word

# whereis java # Search path and standard directories for word

# setenv varname value # Set env. variable varname to value (csh/tcsh)

# export varname="value" # set env. variable varname to value (sh/ksh/bash)

# pwd # Print working directory

# mkdir -p /path/to/dir # no error if existing, make parent dirs as needed

# rmdir /path/to/dir # Remove directory

# rm -rf /path/to/dir # Remove directory and its content (force)

# cp -la /dir1 /dir2 # Archive and hard link files instead of copy

# cp -lpR /dir1 /dir2 # Same for FreeBSD

# mv /dir1 /dir2 # Rename a directory

Пред.: 13. SVNВ началоУровень выше: Unix ToolboxСлед.: 15. INSTALL SOFTWARE
В началоUnix Toolbox → 14. USEFUL COMMANDS