Вы находитесь на странице: 1из 14

Command Description

Show commands
• apropos whatis pertinent to string. See
also threadsafe
make a pdf of a manual
• man -t man | ps2pdf - > man.pdf
page
Show full path name of
  which command
command
See how long a
  time command
command takes
Start stopwatch. Ctrl-d
• time cat
to stop. See also sw
Run a low priority
• nice info command (The "info"
reader in this case)
Make shell (script) low
• renice 19 -p $$ priority. Use for non
interactive tasks
dir navigation
Go to previous
• cd -
directory
Go to $HOME
• cd
directory
Go to dir, execute
  (cd dir && command) command and return to
current dir
Put current dir on stack
• pushd . so you can popd back
to it
file searching
• alias l='ls -l --color=auto' quick dir listing
List files by date. See
• ls -lrt also newest and
find_mm_yyyy
Print in 9 columns to
• ls /usr/bin | pr -T9 -W$COLUMNS
width of terminal
Search 'expr' in this dir
  find -name '*.[ch]' | xargs grep -E 'expr' and below. See also
findrepo
Search all regular files
  find -type f -print0 | xargs -r0 grep -F 'example' for 'example' in this dir
and below
Search all regular files
  find -maxdepth 1 -type f | xargs grep -F 'example'
for 'example' in this dir
Process each item with
find -maxdepth 1 -type d | while read dir; do echo $dir; echo
  multiple commands (in
cmd2; done
while loop)
Find files not readable
• find -type f ! -perm -444 by all (useful for web
site)
Find dirs not accessible
• find -type d ! -perm -111 by all (useful for web
site)
Search cached index for
• locate -r 'file[^/]*\.txt' names. This re is like
glob *file*.txt
Quickly search (sorted)
• look reference
dictionary for prefix
Highlight occurances of
• grep --color reference /usr/share/dict/words regular expression in
dictionary
archives and compression
  gpg -c file Encrypt file
  gpg file.gpg Decrypt file
Make compressed
  tar -c dir/ | bzip2 > dir.tar.bz2
archive of dir/
Extract archive (use
  bzip2 -dc dir.tar.bz2 | tar -x gzip instead of bzip2
for tar.gz files)
Make encrypted archive
  tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg' of dir/ on remote
machine
find dir/ -name '*.txt' | tar -c --files-from=- | bzip2 > Make archive of subset
 
dir_txt.tar.bz2 of dir/ and below
find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ Make copy of subset of
 
--parents dir/ and below
Copy (with
  ( tar -c /dir/to/copy ) | ( cd /where/to/ && tar -x -p ) permissions) copy/ dir
to /where/to/ dir
Copy (with
permissions) contents
  ( cd /dir/to/copy && tar -c . ) | ( cd /where/to/ && tar -x -p )
of copy/ dir to
/where/to/
Copy (with
( tar -c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar
  permissions) copy/ dir
-x -p'
to remote:/where/to/ dir
Backup harddisk to
  dd bs=1M if=/dev/sda | gzip | ssh user@remote 'dd of=sda.gz'
remote machine
rsync (Network efficient file copier: Use the --dry-run option for testing)
Only get diffs. Do
  rsync -P rsync://rsync.server.com/path/to/file file multiple times for
troublesome downloads
Locally copy with rate
  rsync --bwlimit=1000 fromfile tofile limit. It's like nice for
I/O
Mirror web site (using
rsync -az -e ssh --delete ~/public_html/
  compression and
remote.com:'~/public_html'
encryption)
Synchronize current
rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh .
  directory with remote
remote:/dir/
one
ssh (Secure SHell)
Run command on
$HOST as $USER
  ssh $USER@$HOST command
(default
command=shell)
Run GUI command on
• ssh -f -Y $USER@$HOSTNAME xeyes $HOSTNAME as
$USER
Copy with permissions
  scp -p -r $USER@$HOST: file dir/ to $USER's home
directory on $HOST
Forward connections to
  ssh -g -L 8080:localhost:80 root@$HOST $HOSTNAME:8080
out to $HOST:80
Forward connections
  ssh -R 1434:imap:143 root@$HOST from $HOST:1434 in to
imap:143
wget (multi purpose download tool)
Store local browsable
(cd dir/ && wget -nd -pHEKk
• version of a page to the
http://www.pixelbeat.org/cmdline.html)
current dir
Continue downloading
  wget -c http://www.example.com/large.file a partially downloaded
file
Download a set of files
  wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/dir/
to the current directory
FTP supports globbing
  wget ftp://remote/file[1-9].iso/
directly
wget -q -O- http://www.pixelbeat.org/timeline.html | grep 'a
• Process output directly
href' | head
  echo 'wget url' | at 01:00 Download url at 1AM
to current dir
Do a low priority
  wget --limit-rate=20k url download (limit to
20KB/s in this case)
  wget -nv --spider --force-html -i bookmarks.html Check links in a file
Efficiently update a
  wget --mirror http://www.example.com/ local copy of a site
(handy from cron)
networking (Note ifconfig, route, mii-tool, nslookup commands are obsolete)
Show status of ethernet
  ethtool eth0
interface eth0
Manually set ethernet
  ethtool --change eth0 autoneg off speed 100 duplex full
interface speed
Show status of wireless
  iwconfig eth1
interface eth1
Manually set wireless
  iwconfig eth1 rate 1Mb/s fixed
interface speed
List wireless networks
• iwlist scan
in range
• ip link show List network interfaces
Rename interface eth0
  ip link set dev eth0 name wan
to wan
Bring interface eth0 up
  ip link set dev eth0 up
(or down)
List addresses for
• ip addr show
interfaces
Add (or del) ip and
  ip addr add 1.2.3.4/24 brd + dev eth0
mask (255.255.255.0)
• ip route show List routing table
Set default gateway to
  ip route add default via 1.2.3.254
1.2.3.254
Add 20ms latency to
• tc qdisc add dev lo root handle 1:0 netem delay 20msec loopback device (for
testing)
Remove latency added
• tc qdisc del dev lo root
above
Lookup DNS ip address
• host pixelbeat.org
for name or vice versa
Lookup local ip address
• hostname -i (equivalent to host
`hostname`)
Lookup whois info for
• whois pixelbeat.org
hostname or ip address
• netstat -tupl List internet services on
a system
List active connections
• netstat -tup
to/from system
windows networking (Note samba is the package that provides all this windows specific
networking support)
Find windows
• smbtree machines. See also
findsmb
Find the windows
(netbios) name
  nmblookup -A 1.2.3.4
associated with ip
address
List shares on windows
  smbclient -L windows_box machine or samba
server
mount -t smbfs -o fmask=666,guest //windows_box/share
  Mount a windows share
/mnt/share
Send popup to windows
  echo 'message' | smbclient -M windows_box machine (off by default
in XP sp2)
text manipulation (Note sed uses stdin and stdout. Newer versions support inplace
editing with the -i option)
Replace string1 with
  sed 's/string1/string2/g'
string2
Modify anystring1 to
  sed 's/\(.*\)1/\12/g'
anystring2
Remove comments and
  sed '/ *#/d; /^ *$/d'
blank lines
Concatenate lines with
  sed ':a; /\\$/N; s/\\\n//; ta'
trailing \
Remove trailing spaces
  sed 's/[ \t]*$//'
from lines
Escape shell
  sed 's/\([`"$\]\)/\\\1/g' metacharacters active
within double quotes
• seq 10 | sed "s/^/      /; s/ *\(.\{7,\}\)/\1/" Right align numbers
  sed -n '1000p;1000q' Print 1000th line
  sed -n '10,20p;20q' Print lines 10 to 20
Extract title from
  sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'
HTML web page
  sed -i 42d ~/.ssh/known_hosts Delete a particular line
  sort -t. -k1,1n -k2,2n -k3,3n -k4,4n Sort IPV4 ip addresses
• echo 'Test' | tr '[:lower:]' '[:upper:]' Case conversion
• tr -dc '[:print:]' < /dev/urandom Filter non printable
characters
• history | wc -l Count lines
set operations (Note you can export LANG=C for speed. Also these assume no duplicate
lines within a file)
  sort file1 file2 | uniq Union of unsorted files
Intersection of unsorted
  sort file1 file2 | uniq -d
files
Difference of unsorted
  sort file1 file1 file2 | uniq -u
files
Symmetric Difference
  sort file1 file2 | uniq -u
of unsorted files
  join -a1 -a2 file1 file2 Union of sorted files
Intersection of sorted
  join file1 file2
files
Difference of sorted
  join -v2 file1 file2
files
Symmetric Difference
  join -v1 -v2 file1 file2
of sorted files
math
Quick math (Calculate
• echo '(1 + sqrt(5))/2' | bc -l
φ). See also bc
More complex (int) e.g.
• echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bc This shows max FastE
packet rate
Python handles
• echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | python
scientific notation
echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | Plot FastE packet rate

gnuplot -persist vs packet size
Base conversion
• echo 'obase=16; ibase=10; 64206' | bc (decimal to
hexadecimal)
Base conversion (hex to
• echo $((0x2dec)) dec) ((shell arithmetic
expansion))
Unit conversion (metric
• units -t '100m/9.69s' 'miles/hour'
to imperial)
Unit conversion (SI to
• units -t '500GB' 'GiB'
IEC prefixes)
• units -t '1 googol' Definition lookup
Add a column of
• seq 100 | (tr '\n' +; echo 0) | bc numbers. See also add
and funcpy
calendar
• cal -3 Display a calendar
Display a calendar for a
• cal 9 1752
particular month year
What date is it this
• date -d fri
friday. See also day
exit a script unless it's
• [ $(date -d "tomorrow" +%d) = "01" ] || exit the last day of the
month
What day does xmas
• date --date='25 Dec' +%A
fall on, this year
Convert seconds since
• date --date='@2147483647' the epoch (1970-01-01
UTC) to date
What time is it on West
• TZ=':America/Los_Angeles' date coast of US (use
tzselect to find TZ)
echo "mail -s 'get the train' P@draigBrady.com < /dev/null" | at
  Email reminder
17:45
echo "DISPLAY=$DISPLAY xmessage cooker" | at "NOW +
• Popup reminder
30 minutes"
locales
Print number with
• printf "%'d\n" 1234 thousands grouping
appropriate to locale
get ls to do thousands
• BLOCK_SIZE=\'1 ls -l grouping appropriate to
locale
Extract info from locale
• echo "I live in `locale territory`"
database
Lookup locale info for
• LANG=en_IE.utf8 locale int_prefix specific country. See
also ccodes
List fields available in
• locale | cut -d= -f1 | xargs locale -kc | less
locale database
recode (Obsoletes iconv, dos2unix, unix2dos)
Show available
• recode -l | less conversions (aliases on
each line)
Windows "ansi" to
  recode windows-1252.. file_to_change.txt local charset (auto does
CRLF conversion)
Windows utf8 to local
  recode utf-8/CRLF.. file_to_change.txt
charset
Latin9 (western europe)
  recode iso-8859-15..utf8 file_to_change.txt
to utf8
  recode ../b64 < file.txt > file.b64 Base64 encode
Quoted printable
  recode /qp.. < file.txt > file.qp
decode
  recode ..HTML < file.txt > file.html Text to HTML
Lookup table of
• recode -lf windows-1252 | grep euro
characters
Show what a code
• echo -n 0x80 | recode latin-9/x1..dump represents in latin-9
charmap
• echo -n 0x20AC | recode ucs-2/x2..latin-9/x Show latin-9 encoding
• echo -n 0x20AC | recode ucs-2/x2..utf-8/x Show utf-8 encoding
CDs
Save copy of data
  gzip < /dev/cdrom > cdrom.iso.gz
cdrom
Create cdrom image
  mkisofs -V LABEL -r dir | gzip > cdrom.iso.gz
from contents of dir
Mount the cdrom image
  mount -o loop cdrom.iso /mnt/dir
at /mnt/dir (read only)
  cdrecord -v dev=/dev/cdrom blank=fast Clear a CDRW
Burn cdrom image (use
  gzip -dc cdrom.iso.gz | cdrecord -v dev=/dev/cdrom - dev=ATAPI -scanbus
to confirm dev)
Rip audio tracks from
  cdparanoia -B CD to wav files in
current dir
Make audio CD from
  cdrecord -v dev=/dev/cdrom -audio *.wav all wavs in current dir
(see also cdrdao)
Make ogg file from wav
  oggenc --tracknum='track' track.cdda.wav -o 'track.ogg'
file
disk space (See also FSlint)
Show files by size,
• ls -lSr
biggest last
Show top disk users in
• du -s * | sort -k1,1rn | head current dir. See also
dutop
Show free space on
• df -h
mounted filesystems
Show free inodes on
• df -i
mounted filesystems
Show disks partitions
• fdisk -l sizes and types (run as
root)
• rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n List all packages by
installed size (Bytes) on
rpm distros
List all packages by
dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort
• installed size (KBytes)
-k1,1n
on deb distros
Create a large test file
• dd bs=1 seek=2TB if=/dev/null of=ext3.test (taking no space). See
also truncate
truncate data of file or
• > file
create an empty file
monitoring/debugging
Monitor messages in a
• tail -f /var/log/messages
log file
Summarise/profile
• strace -c ls >/dev/null system calls made by
command
List system calls made
• strace -f -e open ls >/dev/null
by command
List library calls made
• ltrace -f -e getenv ls >/dev/null
by command
List paths that process
• lsof -p $$
id has open
List processes that have
• lsof ~
specified path open
Show network traffic
• tcpdump not port 22 except ssh. See also
tcpdump_not_me
List processes in a
• ps -e -o pid,args --forest
hierarchy
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ List processes by % cpu

0.0 /d' usage
List processes by mem
• ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS usage. See also
ps_mem.py
List all threads for a
• ps -C firefox-bin -L -o pid,tid,pcpu,state
particular process
List info for particular
• ps -p 1,2
process IDs
Show system reboot
• last reboot
history
Show amount of
• free -m (remaining) RAM (-m
displays in MB)
• watch -n.1 'cat /proc/interrupts' Watch changeable data
continuously
system information (see also sysinfo) ('#' means root access is required)
Show kernel version
• uname -a
and system architecture
Show name and version
• head -n1 /etc/issue
of distribution
Show all partitions
• cat /proc/partitions
registered on the system
Show RAM total seen
• grep MemTotal /proc/meminfo
by the system
• grep "model name" /proc/cpuinfo Show CPU(s) info
• lspci -tv Show PCI info
• lsusb -tv Show USB info
List mounted
filesystems on the
• mount | column -t
system (and align
output)
Show state of cells in
• grep -F capacity: /proc/acpi/battery/BAT0/info
laptop battery
Display SMBIOS/DMI
#dmidecode -q | less
information
How long has this disk
#smartctl -A /dev/sda | grep Power_On_Hours (system) been powered
on in total
Show info about disk
#hdparm -i /dev/sda
sda
Do a read speed test on
#hdparm -tT /dev/sda
disk sda
Test for unreadable
#badblocks -s /dev/sda
blocks on disk sda
interactive (see also linux keyboard shortcuts)
Line editor used by
• readline bash, python, bc,
gnuplot, ...
Virtual terminals with
• screen
detach capability, ...
Powerful file manager
• mc that can browse rpm,
tar, ftp, ssh, ...
Interactive/scriptable
• gnuplot
graphing
• links Web browser
• xdg-open http://www.pixelbeat.org/ open a file or url with
the registered desktop
application
miscellaneous
Handy hexdump.
(usage e.g.: • hd
• alias hd='od -Ax -tx1z -v'
/proc/self/cmdline |
less)
Canonicalize path.
• alias realpath='readlink -f' (usage e.g.: • realpath
~/../$USER)
Search current
• set | grep $USER
environment
Set file timestamp
  touch -c -t 0304050607 file
(YYMMDDhhmm)
Serve current directory
tree at
• python -m SimpleHTTPServer
http://$HOSTNAME:8
000/

I’m sure you all are getting fairly tired of this, but I have to give my usual disclaimers:

1) Different Unix systems handle password security settings in different ways, so we’re just going to focus on
Linux

2) The answer is different if you’re working with a network-based authentication database like LDAP or
Kerberos, but for purposes of this article we’re just going to stick to local password files

With those disclaimers in mind, the basic answer is simple:

# chage -l hal
Last password change : Jul 14, 2007
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7

The “chage” command can be used to get (and set) basic password security parameters for accounts on your
Linux system (other Unix variants often use the “passwd” command for this). This is actual output from one
of my test systems and shows you the standard Linux defaults for these parameters, which are obviously not
terribly secure. You may change the defaults by modifying the /etc/login.defs file, but be aware that the
defaults you set in login.defs will only apply to new accounts that you create with the built-in “useradd”
program that comes with Linux. If you use some other scheme for creating accounts, then you’ll have to use
the “chage” command to manually set these values after you create each account.

Have To Do:::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
931 cd /tmp
932 cpio -icvd < /dev/st1
933 cpio -icBvt < /dev/st1
934 cpio -icvum < /dev/st1
935 tail /etc/passwd
936 more /etc/passwd
937 cat /etc/group
938 pwd
939 find .-print |cpio -ivBt < /dev/st1
940 cd /
941 pwd
942 find .-print |cpio -ivBt < /dev/st1
943 find .-print |cpio -ivBt < /dev/st0
944 find .-print |cpio -ivBt < /dev/st1
945 tar tvf /dev/st1
946 cpio -ov < /dev/st0
947 df -h
948 cd /raid
949 ls -l
950 cd /backup
951 ls -l
952 cd jr
953 ls -l
954 cd opbackup
955 ls -la
956 more opbackup
957 cd /tmp
958 ls -l
959 ls -l |more
960 pwd
961 man cpio
962 pwd
963 cpio -itv < /dev/st0
964 cpio -itvcC1 < /dev/st0
965 ipcs -s | nl
966 ipcs -m | nl

===============================================================
WRITING TO TAPE
===============================================================
root@COMAOS SYSTEMS]# ls -l
total 12
drwxrwxrwx  5 kcccatcs cat   4096 Sep 29 09:42 CAT
drwxrwxrwx 12 kcccs    users 4096 Jun  1 16:17 MAL_LIVE
drwxrwxrwx 19 kccpmscs pms   4096 Sep 29 08:29 PMS_LIVE
[root@COMAOS SYSTEMS]# cd ..
[root@COMAOS backup]# ls -l
total 24
-rwxrwxrwx 1 root root    56 Apr 23 10:34 BackupTape.sh
drwx------ 2 root root 16384 Jan 15  2009 lost+found
drwxrwxrwx 5 root root  4096 Sep 29 09:35 SYSTEMS
[root@COMAOS backup]# pwd
/backup
[root@COMAOS backup]# cd SYSTEMS
[root@COMAOS SYSTEMS]# pwd
/backup/SYSTEMS
[root@COMAOS SYSTEMS]# pwd
/backup/SYSTEMS
[root@COMAOS SYSTEMS]# cd ..
[root@COMAOS backup]# pwd
/backup
[root@COMAOS backup]# find SYSTEMS -print | cpio -ocC262144 >/dev/st0
=======================================================================
started manual backup -
cd /backup

Using command -
root@COMAOS backup]# find SYSTEMS -print | cpio -ocC262144 >/dev/st0
83018 blocks
Read back -
cpio -icvdmt -C262144 </dev/st0
83018 blocks

Linux script to prompt for password


by Vivek Gite · 4 comments

Linux do not provide any special or specific command to read password. However, bash
shell comes with builtin command called read. It read date from the standard input
(keyboard), or from file descriptor FD if the -u option is supplied. General syntax is as
follows:
read -s -p "Password: " VARIABLE

Where,

 -s: Do not display password on screen. It causes input coming from a


terminal/keyboard to not be echoed
 -p: "Password: ": Display text message
 VARIABLE: Bash variable in which your password stored

For example, try out following example at shell prompt:


$ read -s -p "Enter Password: " mypasswordOutput:

Enter Password:
Now display password:
$ echo $mypassword

Generate Password Command:


< /dev/urandom tr -dc A-Za-z0-9_ | head -c8

Вам также может понравиться