Linux Commands

From miki
Revision as of 10:56, 31 January 2011 by Mip (talk | contribs) (→‎xargs)
Jump to navigation Jump to search

File System

dd

Convert and copy a file.

dd returns current copy status when sent an HUP signal. Type in another shell:

while :; sleep 5; do killall -s SIGHUP1 dd; done

This will force dd to update its status every 5sec.

find

See here for further examples on how to combine find and xargs.

Frequent pitfall! The semi-colon ; must be escaped in Bash !

find . -name "*.jpg"                                     # find files/directories whose name matches specified pattern
find . -path "*/cur/*"                                   # find files/directories whose path spec matches specified pattern
find . -type d -name "cur"                               # find only directories whose name matches specified pattern
find . -exec echo '{}' \;                                # semi-colon must be escaped!
find . -exec echo one '{}' \; -exec echo two '{}' \;     # to execute several commands on one match
find . -name \*.jpg | xargs echo                         # much faster version using xargs
find . -name \*.jpg | xargs -n 2 echo                    # limit #args passed to echo to 2 max.
find . -name \*.jpg -print0 | xargs -0 ls                # Use NULL separator if files contains space or special char !!!
find . -name 'dirname' -prune -o -iname '*.txt' -print   # Find all text files, but exclude any directory named dirname
find . -path './path' -prune -false -o -iname '*.txt' -print
                                                         # Find all text files, but exclude path ./path
find . -iname '*.zip' -exec sh -c 'unzip -l "$0" | egrep -o " [0-9]+ file"' '{}' \;
                                                         # How to use pipe in -exec.

Accelerating find:

find / -mount -name "*splash*"                           # Only search on CURRENT file system (same as find -xdev)

grep

grep -Rsl PATTERN [FILE]            # Recursive, no error output, only list filename
grep BASIC-REG-EXP-PATTERN [FILE]   # Use classic regexp (like "dma\|DMA")
egrep EXT-REG-EXP-PATTERN [FILE]    # Same as grep -E. Use extended regexp (like "dma|DMA")
fgrep FIXED-STRINGS-REG-EXP [FILE]  # Same as grep -F. Pattern is a list of strings to match.
grep -n PATTERN [FILE]              # Print matched line numbers.
grep -- "-s" [FILE]                 # Search for text "-s"
grep -e "-s" [FILE]                 # Search for text "-s" - alternative solution
grep -R -include=*.in PATTERN *     # Search recursively through folders, limiting to files matching pattern "*.in"
grep -R PATTERN *.in                # Idem, but matching pattern "*.in" also applies to folders.
grep -o PATTERN [FILE]              # (--only-matching) Print only the matched (non-empty) parts of a matching line.

gzip / gunzip / zcat

gzip -c FILE                        # Compress FILE to stdout
gunzip -c FILE                      # Uncompress FiLe to stdout
zcat FILE                           # Equivalent to gunzip -c FILE

ls

Listing directory content:

ls --full-time               # To list files full date & time
ls -d directory-name         # List directory entries, not content
                             #  eg. ls -d /etc/rc*
ls -lS | head                # List biggest files first - print only first 10 entries
ls -lt | head -n 20          # List most recent files first, print only 20 first entries

tar

tar archives and restores timestamps. When run as root, tar will also restore ownership and permissions.

tar -cvzf archive.tgz somedir/                             # Archive & GZIP compress the directory somedir/ (recursively)
tar -cvjf archive.tar.bz2 somedir/                         # ... Use BZIP2 insteadl
tar -cvzf archive.tgz --exclude=notme somedir/             #  ... exclude any file matching name
tar -cvzf archive.tgz --exclude=somedir/not/me somedir/    #  ... exclude file at specific location

tar -xvzf archive.tgz                                      # Extract archive (will create somedir/)
tar -xvzf archive.tgz -C targetdir                         # ... to specified directory
tar -tvzf archive.tgz                                      # List the content (equiv. of tar --list -vzf ...)

Miscellaneous

badblocks
An utility to detect bad sectors on USB flash drive or so.
dosfslabel
Change FAT16/FAT32 disk label
sudo dosfslabel /dev/sdc1 UBUNTULIVE
install-mbr
Install MS-like Master Boot Record (see [1])
sudo apt-get install mbr
install-mbr -i n -p D -t 0 /dev/sda        # Fix MBR on /dev/sda
                                           # -i interrupt n = none (do not display a MBR prompt)
                                           # -p partition D = boot the partition with the bootable flag set
                                           # -t timeout 0 = do not wait to boot
dd if=/dev/sda of=opensource.mbr bs=512 count=1
install-mbr -i n -p D -t 0 opensource.mbr  # Create a file opensource.mbr containing the generated MBR
mkfs
Format disk utility
sudo mkfs.vfat -F 32 -c /dev/sdc1
ms-sys
Install MS-compatible Master Boot Record (similar to FIXMBR on WinXP rescue console, see [2])
# Get ms-sys from http://ms-sys.sourceforge.net/#Download
sudo apt-get install gettext
make
sudo make install
sudo ms-sys -m /dev/sdb
relocntfs
relocntfs (see [3]) is more or less the equivalent of the FIXBOOT command in the Windows Recovery Console. It is among other available on the Trinity Rescue Kit. For instance, to fix the boot record of a windows partition at /dev/sda2 issue the commands:
relocntfs /dev/sda2        
relocntfs -w /dev/sda2                    # To actually write the new boot record
stat
display file or file system status (like file access time, creation time, modification time...)
tree
List the contents of directories in a tree-like format.

Network

autossh

autossh - Automatically restart SSH sessions and tunnels.

autossh by default monitors the ssh connection through a dedicated port to see whether the current ssh connection must be restarted. However the simpler is to use the ssh config option ServerAliveCountMax and ServerAliveInterval so that ssh exits automatically when the connection is down. In that case, autossh will then restart ssh automatically without need of additional monitoring.

Add to your ~/.ssh/config:

Host *
  ServerAliveCountMax       3    # default value actually
  ServerAliveInterval       15   # ssh will exit after 3 x 15s = 45s

Example of uses:

$ autossh -M 0 -f -D 1080 noekeon       # -M 0 tells autossh to only restart ssh upon ssh's exit

dig

dig stands for domain information groper. It replaces the deprecated nslookup. It comes with package dnsutils:

sudo apt-get install dnsutils

multitee

multitee sends multiple inputs to multiple outputs.
Check this page.
Original is here, or can also be found here on Debian.

Here a patch to build it on Cygwin. The patch ports calls to BSD signal.h API (struct sigvec, sigvec(), sigmask(), sigblock()...) to the POSIX API (struct sigaction, sigaction(), sigprocmask(), sigfillset()...):

$ patch -Np1 <../multitee-3.0-cygwin.patch
$ make
$ cp multitee.1 /usr/local/man/man1
$ cp multitee.exe /usr/local/bin

Example of use:

$ multitee 0-1,4,5 4> foo 5> bar             # same as tee foo bar with better blocking behavior
$ multitee 0:1 3:1 4:1,2 6:7                 # various merge and copy
$ tcpclient server smtp multitee 0:7 6:1e0   # e0 tell multitee to quit as soon connection close
$ multitee 0:3 3:1                           # same as 'socat - FD:3'

netcat

TCP-IP swiss army knife (equivalent of the telnet program. Check wikipedia:netcat. Also known as command nc).

netstat

Print network connections, routing tables, interface statistics, masqurade connections, and multicast memberships

netstat -utpn      #Active ports, tcp, socket program PID, numeric
netstat -lutpn     #Listen ports, tcp, socket program PID, numeric
netstat -autpn     #All (active and listen), tcp, socket program PID, numeric
netstat -rn        #Kernel route table, numeric

When listing sockets (default output), you'll get an output like:

% netstat -at

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:time                  *:*                     LISTEN
tcp        0      0 localhost:mysql         *:*                     LISTEN
tcp        0      0 andLinux.local:43449    windows-host:x11        ESTABLISHED
% netstat -atn

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:37              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
tcp        0      0 192.168.11.150:43449    192.168.11.1:6000       ESTABLISHED
Local Address
* or 0.0.0.0 means that the process accepts connection from any interface.
127.0.0.1 means it only accepts connection on localhost loopback (and so only connection that originates from local PC as well).
Any other IP address means that the process listen on the given port at the given IP address

/etc/init.d/nscd

This is not really a command, but the init service Name Service Cache Daemon.

sudo /etc/init.d/nscd restart                # To restart daemon and flush DNS cache

socat

Command-line utility that establishes two bidirectional byte streams and transfers data between them ([4]). socat is the more powerful version of netcat. Check the homepage. And also this page on Yobi for examples on how to use socat to bypass a proxy.

socat can easily tunnel connections:

socat -ly 'TCP4-LISTEN:143,reuseaddr,fork' PROXY:ton.imap.server:143|TCP:134.27.168.36:8080

socat can also be used as SSH ProxyCommand:

ProxyCommand socat -ly - 'PROXY:%h:%p|TCP:proxy.server:8080'
# Using socat v2.0.0 beta
ProxyCommand socat -ly - 'PROXY:%h:%p,proxyauth=user:pass|SSL,verify=0|PROXY:my.server:443,proxyauth=user:pass|TCP:big.brother.proxy:8080'

socat can be easily used as a replacement of telnet:

socat tcp:<host>:<port> -       # <port> can be a port number or service name (telnet,imap...)

Some useful command-line options:

socat -ly                       # Writes messages to syslog instead of stderr; severity as defined with option -d
socat -v                        # Writes the transferred data to their target stream but also to stderr (text mode)
QUESTION TO MR DOEGOX:
- WHY -s in SED?
- Where does it get info on % for socat?

$ socat echo -
$ socat echo STDIO
$ socat echo STDOUT%STDIN
$ socat echo STDIO%STDIO
$ socat echo -%-
$ socat echo FD:1%FD:0
$ socat echo 1%0

/usr/local/bin/socat TCP:127.0.0.1:1234 -%EXEC:"awk 'BEGIN{print \"BANNER\";fflush()}/BANNER/{next}//{print;fflush()}'"

Testing socat:
- /usr/local/bin/socat - TCP-LISTEN:1234
- /usr/local/bin/socat TCP:127.0.0.1:1234 EXEC:"/bin/sed -s 's/foo/bar/g'"%EXEC:"/bin/sed -u 's/toto/tata/g'"

  First open a port for listening and connect it to STDIO
  In another shell, connect and do the black magic...


reference:
- http://eros.ph0k.eu/~nizox/twin/V2/modules/socat-1.6.0.0/EXAMPLES
- http://www.rschulz.eu/2008/09/ssh-proxycommand-without-netcat.html
		$ exec 3<>/dev/tcp/zeus.yobi.be/22;(cat <&3 & );cat >&3
		SSH-2.0-OpenSSH_5.1p1 Debian-3
- http://thesmithfam.org/blog/2006/05/23/bash-socket-programming-with-devtcp-2/
- http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=146464
- http://www.mtu.net/~engstrom/ssh-proxy.php



ATTEMPT 3:
Window 1: socat -v -%'EXEC:/home/beq06659/.ssh/mys2.sh' TCP-LISTEN:1234
Window 2: /usr/local/bin/socat -ly -v -%"EXEC:sed -unf /home/beq06659/.ssh/sedy" "PROXY:ftp.noekeon.org:22|TCP:localhost:1234"

File mys2.sh:
	#! /bin/bash
	echo -e "HTTP/1.1 200 Connection established\r\n\r\n"
	cat
File sedy:
	1 s/^/SSH-2.0-OpenSSH_5.1\n/p; /SSH-2.0-OpenSSH_5.1/d; /SSH-2.0-OpenSSH_5.1/! p

LESSONS LEARNED:
- READ THE CHANGES DOC, especially for BETA SW
- SOCKET = bidir --> a filter = 4 descriptors
- PIPE = unidir --> a filter = 2 descriptor (stdin/stdout)

Package Management

dpkg

Package manager for Debian / Ubuntu. See Package Management#Ubuntu / Debian

rpm

See Package Management#RPM's

System

iwconfig

iwconfig configures a wireless network interface:

iwconfig
# # iwconfig
# lo        no wireless extensions.
# 
# eth0      no wireless extensions.
# 
# wlan0     IEEE 802.11abg  ESSID:off/any  
#           Mode:Managed  Access Point: Not-Associated   Tx-Power=15 dBm   
#           Retry  long limit:7   RTS thr:off   Fragment thr:off
#           Encryption key:off
#           Power Management:off
#

lsmod / modinfo / modprobe

lsmod                              # Show the status of modules in the Linux Kernel
modinfo iwlagn                     # Show information about a Linux Kernel module
modprobe -r iwlagn                 # Remove module from the Linux Kernel
modprobe iwlagn 11n_disable=1      # Add module to the Linux Kernel, with specific parameters

lspci

lspci                              # List all PCI devices

pidinfo

Find the process ID of a running program

pidof apache2                      # Find all pid of apache2

pgrep

Prints PID of the process matching given pattern. Never match pgrep itself!

ps -fp $(pgrep autossh)            # Much better that "ps -aef | grep autossh", which match grep process itself

pkill

Send a signal (default TERM) to process matching given pattern. Never match pkill itself!

pkill -0 autossh                   # Test if process "autossh" is running

sudo / gksudo

See also Ubuntu's page at [linux reference script security sudo ubuntu].

ls | sudo tee /root/somefile                # Instead of redirection sudo ls > /root/somefile
ls | sudo tee -a /root/somefile             # ... same but append instead
sudo sh -c "ls > /root/somefile"            # Yet another way to do redirection with sudo
sudo !!                                     # Repeat last command with root privilege
sudo -i                                     # Get a root *login* shell
sudo su -                                   # ... another solution
sudo -s                                     # Root shell but keeping current environment
sudo su                                     # ... another solution
sudo -k                                     # Reset sudo password timeout

gksudo is the graphical equivalent of sudo:

gksudo gedit /etc/fstab

showkey

showkey examine the codes sent by the keyboard (linux kernel only - for X keyboard events, see xev!).

showkey -k                                  # Dump key codes (default)
showkey -s                                  # Dump scan codes

Miscellaneous

free
Display the amount of free and used memory (free -m to get info in MB)
htop
an improved top command
lsb_release
lsb_release -a prints version information for the Linux release you're running.
mkfifo
make FIFOs (named pipes)
pv, pipeview, pipebench
monitor the progress of data through a pipe
strace
trace system calls and signals
tee
read from standard input and write to standard output and files
uname
uname -a prints all system information, include machine name, kernel name & version.
watch
Execute a program periodically, showing output full screen

User Administration

groupadd / addgroup

Use groupadd, addgroup to create a new group. groupadd is the low-level utility. addgroup is the Debian more friendly version.

sudo groupadd groupname                      # Create a new group groupname
sudo groupadd -g 123 groupname               # ... and specify gid of the new group
sudo addgroup groupname
sudo addgroup --gid 123 groupname

useradd / adduser

Use useradd to create new user. useradd is the low-level utility. adduser is the Debian more friendly version.

sudo useradd username                        # Create a new user username
sudo useradd -s /bin/bash username           # ... specify the deafult shell in use for the user
sudo useradd -s /bin/bash -m username        # ... and create user home directory (from /etc/skel)
sudo useradd -u 1234 username                # Create a new user username with uid 1234
sudo useradd -g GRP1 -G GRP2,GRP3 username   # Specify primary and supplementary groups
sudo adduser username                        # Create a new user username
sudo adduser --system username               # Create a new system user (in the system uid range)
sudo adduser --shell /bin/bash username      # Use /bin/bash as default login shell

userdel / groupdel / deluser

Use userdel, groupdel, deluser to remove an existing user /group. deluser is the Debian more friendly version

sudo userdel username                        # Remove existing user username
sudo groupdel groupname                      # Remove existing group groupname
sudo deluser username                        # Remove existing user username
sudo deluser --group groupname               # Remove existing group groupname

usermod

Use usermod to modify an existing user

sudo usermod -g GRP1 username                # Modify the primary group of user username
sudo usermod -g GRP1 -G GRP2,GRP3 username   # ... and also the supplementary groups

X

disper

TO install:

sudo add-apt-repository ppa:wvengen/ppa
sudo apt-get update
sudo apt-get install

Some examples of use:

disper -s               # Only enable primary screen (laptop screen)
disper -S               # Only enable secondary screen (external monitor)
disper -d auto -e       # Auto-detect + enable extended mode
disper -d auto -c       # clone mode

xbindkeys / xbindkeys-config

  • xbindkeys is a program that allows you to launch shell commands with your keyboard or your mouse under the X Window System. It links commands to keys or mouse buttons, using a configuration file. It's independent of the window manager and can capture all keyboard keys (ex: Power, Wake...).
  • xbindkeys-config is an easy to use gtk program for configuring Xbindkeys (see [5].

xclip

xclip is a command line interface to the X11 clipboard. It can also be used for copying files, as an alternative to sftp/scp, thus avoiding password prompts when X11 forwarding has already been setup. Check this guide.

xdpyinfo

Display information utility for X

xdpyinfo | grep dimensions               # Return dimension of the desktop

XSel / XSelection

XSel is a command-line program for getting and setting the contents of the X selection and context-menu clipboard. Normally the former is only accessible by manually highlighting information and pasting it with the middle mouse button, and the latter is only accessible by using context-menu cut/copy/paste command.

xselection is an equivalent package on OpenSUSE.

On Cygwin, one has to use getclip, putclip or device /dev/clipboard.

xsel -p                                 # To get content of PRIMARY buffer (X selection)
xselection PRIMARY                      # (equivalent command for xselection)
xsel -b                                 # To get content of CLIPBOARD (context-menu clipboard)
xselection CLIPBOARD                    # (equivalent command for xselection)
echo sometext | xsel -p                 # To set PRIMARY buffer
echo sometext | xselection PRIMARY -    # (equivalent command for xselection)
xselection PRIMARY sometext             # (equivalent command for xselection)
echo sometext | xsel -b                 # To set CLIPBOARD buffer
echo sometext | xselection CLIPBOARD -  # (equivalent command for xselection)
xselection CLIPBOARD sometext           # (equivalent command for xselection)

xvkbd

xvkbd is a virtual (graphical) keyboard program for X Window System which provides facility to enter characters onto other clients (softwares) by clicking on a keyboard displayed on the screen. It can also be used to send keyboard events through command-line:

xvkbd -xsendevent -text text-string

xwininfo

xwininfo is the window information utility for X. Could be used to find the geometry of a window. See here

xwininfo                               # Return information on the clicked windows
xwininfo -root                         # ... or on the root window
xwininfo -root |awk '/Width/{print $2} # Get Width of the current desktop

Miscellaneous

cksum / jacksum / md5sum / sha1sum / sum

cksum file                                              # Generate a CRC similar to CRC32, but with length appended.
cksum -o3 file                                          # Generate a CRC32 checksum (BSD only)
sum file                                                # Generate a checksum
md5sum file                                             # Hash file using MD5
sha1sum file                                            # Hash file using SHA1
jacksum -a crc32 file                                   # Hash using CRC32

diff

Use diff to compare 2 files together and/or to generate patch files

$ cp project_dir project_dir-patched     
$ cd project_dir-patched
$ vi somefile                                             # We start modifying the copy
$ vi someotherfile
$ cd ..
$ diff -u project_dir project_dir-patched >project.patch  # We generate the patch file (universal format)

Patch can be applied with the command patch (see below). Be careful regarding file names / directory structures when generating patch files! Sometimes you'll need to edit the generate patch files to fix the directory structure.

dog

dog is better than cat. It writes the contents of each give file, URL, or the standard input to standard output.

dtach

dtach simple program that emulates the detach feature of screen. A session in dtach is a single instance in which a program is running under the control of dtach. The program is disassociated from the original terminal, and is thus protected from your original terminal being disconnected for some reason.

echo

echo -e "Some text\n...on 2 lines..."                    # Enable interpretation of backslash escapes (must be quoted!)

expect

Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc.

iconv

locale encoding conversions

hd / hexdump

ASCII, decimal, hexadecimal, octal dump.

hexdump -e '"%2x"' <myfile>             # Convert myfile into a long hexadecimal string - ! See DOUBLE-QUOTED parameter

See also od and xxd.

konwert

fancy encoding conversions

mc (Midnight Commander)

mc or Midnight Commander is a powerful file manager working in a shell terminal.
See dedicated page on this wiki.

mimencode

binary file conversion for the mail.

mrxvt

A multi-tab version of rxvt (terminal-emulator).

od

od - dump files in octal and other formats

od -xc file                                              # Output file in ascii and hex output (2-byte, ! little-endian)
od -t x1 -a --width=32 testvar.sh                        # Output 32-byte at once

See also hd / hexdump and xxd.

patch

Use patch to apply a patch previously generated with the command diff. patch use is pretty much straightforward, one mainly has to pay attention to directory structure / blanks.

Starting from the end of our example in diff above:

$ cd project_dir                 # We cd into project directory to patch
$ patch -lNp1<../project.patch   # We apply the patch in place
  • -l — ignore white spaces (very handy when copying patch from internet page for instance).
  • -N — ignore patch that seems reversed or already applied (same as --forward).
  • -p1 — tells patch to ignore the first (i.e. one level of) directory in the file name given in the patch header.

The value to use for option p depends actually on the patch header:

  • First example - same root directory for both to and from file
--- outguess-0.2/jpg.c       2001-02-13 01:29:07.000000000 +0100
+++ outguess-0.2/jpg.c       2009-08-25 16:06:05.242378300 +0200
  • second example - different root directory between to and from file
--- outguess-0.2/jpg.c       2001-02-13 01:29:07.000000000 +0100
+++ outguess-0.2-patched/jpg.c       2009-08-25 16:06:05.242378300 +0200
$ patch -lNp0 <project.patch                        # For first example header above
$ cd project_dir; patch -lNp1 <../project.patch     # For second example header above

recode

recode /cl../cr <dos.txt >mac.txt
recode /cr.. <mac.txt >unix.txt
recode ../cl <unix.txt >dos.txt
recode charset1/surface1..charset2/surface2 <input.txt >output.txt
recode /QP.. <qp.txt >normal.txt                                    # To convert quoted-printable text
charset surface
us ASCII (7 bits) /cr Carriage return as end of line (Mac text)
l1 ISO Latin-1 (ISO-8859-1, Western Europe, 8 bits) /cl Carriage return line feed as end of line (DOS text)
EUCJP EUC-JP for Japanese (Unix) / Line feed as end of line (Unix text)
SJIS Shift-JIS for Japanese (Microsoft) /d1 Human readable bytewise decimal dump
ISO2022JP Mail encoding for Japanese (7 bits) /x1 Human readable bytewise hexidecimal dump
u2 UCS-2 (Universal Character Set, 2 bytes) /64 Base64 encoded text
u8 UTF-8 (Universal Transformation Format, 8 bits) /QP Quoted-Printable encoded text

reformime

MIME E-mail reformatting tool

screen

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

sed

Moved to page dedicated to Sed.

sort

sort by default takes into account the current locale. To get the traditional sort order that uses native byte values:

LC_ALL=C sort myfile.txt            # traditional sort, indep of current locale

uuencode, uudecode

Binary file conversion for Unix.

wc

Counts lines, words and character in a file

xargs

For more examples, see combinations with the command find

#By default, xargs cuts at WHITE SPACE and NEWLINE. Use -d '\n' to only cut at NEWLINES
for i in $(locate .pdf); do basename $i; done      # WRONG
locate .pdf | xargs -d '\n' -n 1 basemane          # Correct

xxd

Make a hexdump or do the reverse

echo 202122 | xxd -r -p                    # Convert hexdump to a binary string
echo -n ' !"' | xxd -p                     # Convert binary string to hexdump - DON'T FORGET -n
echo -n ' !"' | xxd -u -p                  # Convert binary string to hexdump - DON'T FORGET -n (uppercase)

See also hd / hexdump and od.