shellsnippets
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
shellsnippets [2006/12/10 18:03] – 221.223.180.243 | shellsnippets [2012/12/16 11:28] (current) – [Set system date by time from webserver] 2001:6f8:1c1a:0:922b:34ff:fe18:7138 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Shell ====== | ||
+ | ===== Useful standard utilities ===== | ||
+ | |||
+ | * [[man> | ||
+ | * [[man> | ||
+ | * [[man> | ||
+ | * [[man> | ||
+ | * [[man> | ||
+ | |||
+ | ===== Find space wasters ===== | ||
+ | |||
+ | This gives you a quick overview where the largest directories are | ||
+ | |||
+ | $> du --max-depth=3 -x / |sort -n | ||
+ | |||
+ | ===== Delete empty directories ===== | ||
+ | |||
+ | $> find . -type d -empty -delete | ||
+ | |||
+ | ===== Execute a local shellscript on a remote host ===== | ||
+ | |||
+ | |||
+ | $> cat script.sh |ssh user@host "bash -s" | ||
+ | |||
+ | |||
+ | ===== Looping over lines with spaces ===== | ||
+ | |||
+ | To read filenames from a file and do something (eg. echo them) you might wanna do this: | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | for x in `cat files.txt` | ||
+ | do | ||
+ | echo " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | Unfortunately this breaks when some of these filenames contain spaces. [[man> | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | cat files.txt |while read x | ||
+ | do | ||
+ | echo " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | If you need to [[man> | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | cat files.txt |while read x | ||
+ | do | ||
+ | scp user@remote:" | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Replace by regular expression in multiple files ===== | ||
+ | |||
+ | You can use Perl to replace a certain string with another in multiple files. But first you should make sure your regexp matches the right thing. The following is looking for a path in quotes starting with the dir '' | ||
+ | |||
+ | $> find -name ' | ||
+ | |||
+ | If you're happy use the following to do the replacement. Here we surround the string with the PHP function PI: | ||
+ | |||
+ | $> find -name ' | ||
+ | |||
+ | ===== Return absolute filename ===== | ||
+ | |||
+ | This handy function returns the absolute filename of a given file. Example: | ||
+ | |||
+ | $> absname ~/.bashrc | ||
+ | / | ||
+ | |||
+ | <code bash> | ||
+ | function absname { | ||
+ | if [ -d " | ||
+ | dir=" | ||
+ | unset file | ||
+ | elif [ -f " | ||
+ | dir=$(dirname " | ||
+ | file="/" | ||
+ | else | ||
+ | # The file did not exist. | ||
+ | # Return null string as error. | ||
+ | echo | ||
+ | return 1 | ||
+ | fi | ||
+ | |||
+ | # Change to the directory and display the absolute pathname. | ||
+ | cd " | ||
+ | echo ${PWD}${file} | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Here is a simpler one which doesn' | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | p=`echo " | ||
+ | echo "The scripts absolute path is $p" | ||
+ | </ | ||
+ | |||
+ | Or you could just use readlink, which also resolves symlinks: | ||
+ | |||
+ | $> readlink -f ~/.bashrc | ||
+ | / | ||
+ | |||
+ | ===== Print ANSI colors ===== | ||
+ | |||
+ | This small script prints all available ANSI-colors on the terminal. | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | for i in 30 31 32 33 34 35 36 37 39 | ||
+ | do | ||
+ | for j in 40 41 42 43 44 45 46 47 49 | ||
+ | do | ||
+ | # skip if same fore- and backgroundcolor | ||
+ | if [ $j -eq $[ i + 10 ] ]; then | ||
+ | | ||
+ | fi | ||
+ | echo -e $i $j " | ||
+ | done | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | ===== Copy remote directories ===== | ||
+ | |||
+ | This is a short snippet to copy whole directory trees with all permissions, | ||
+ | |||
+ | $> cd /home/user | ||
+ | $> ssh user@remote.host.com "cd /home/user && find . \ | ||
+ | |grep -v bigfile | cpio -H crc -o" | cpio -idum --verbose | ||
+ | |||
+ | The above statement will copy everything but // | ||
+ | |||
+ | ===== 30 days backup ===== | ||
+ | |||
+ | Just found at http:// | ||
+ | |||
+ | $> tar czf / | ||
+ | $> find /backupdir -name \*.tar.gz -mtime +30 -print0 | xargs -0r rm -f | ||
+ | |||
+ | ===== TAR all directories and delete them afterwards ===== | ||
+ | |||
+ | $> for x in `find -type d -maxdepth 1|grep -v ' | ||
+ | |||
+ | ===== Set system date by time from webserver ===== | ||
+ | |||
+ | Sometimes you want to set the system date of Unix system to some sane value. You could do it manually or use some NTP client. The first idea is too much work ;-) and for the second one you need a time server and some client installed. But why not use the HTTP Response Header '' | ||
+ | |||
+ | #> date -s "`curl -I http:// | ||
+ | |||
+ | ===== Backups with afio ===== | ||
+ | |||
+ | The following command uses [[man> | ||
+ | |||
+ | #> find / -path '/ | ||
+ | |||
+ | Creating multiple maximum 1GB sized chunks: | ||
+ | |||
+ | #> find / -path '/ | ||
+ | afio -o -v -M 100m -Z -0 -s 1000m \ | ||
+ | -H 'bash -c "mv \$2 \$2.\$1" | ||
+ | |||
+ | This is to prefer over other solutions using [[man> | ||
+ | |||
+ | Unfortunately this does not work with the '' | ||
+ | |||
+ | ===== Show specified lines from a file ===== | ||
+ | |||
+ | This shows lines 713-720 from '' | ||
+ | |||
+ | $> sed -n ' | ||
+ | |||
+ | ===== dos2unix unix2dos ===== | ||
+ | |||
+ | These are handy in the .bashrc | ||
+ | |||
+ | <code bash> | ||
+ | alias dos2unix=" | ||
+ | alias unix2dos=" | ||
+ | </ | ||
+ | |||
+ | I've got trouble with unix2dos while calling it on DOS files. So I replaced it with a new version. I'm not very familar with " | ||
+ | |||
+ | <code bash> | ||
+ | alias unix2dos=" | ||
+ | </ | ||
+ | |||
+ | Command Line (Windows to Unix): | ||
+ | col -bx < win_file.txt > unix_file.txt | ||
+ | |||
+ | ===== Strip UTF-8 Byte Order Mark (BOM) ===== | ||
+ | |||
+ | The following command removes the UTF-8 Byte Order Mark from given file(s). See [[http:// | ||
+ | |||
+ | $> sed -i -s -e ' | ||
+ | |||
+ | |||
+ | ===== Print formatted man pages ===== | ||
+ | |||
+ | You can print out man pages -- in this case, 2 man pages on one paper sheet: | ||
+ | |||
+ | $> function lprman () { man -t $1 $2 | psnup -2 | lpr -P $3 } | ||
+ | |||
+ | Parameter 1: man page category \\ Parameter 2: man page name \\ Parameter 3: name of the queue to print with | ||
+ | |||
+ | Sample call: | ||
+ | $> lprman 5 fstab mylaser | ||
+ | |||
+ | Notes: | ||
+ | * you need psutils to have that working -- psnup belongs into this package | ||
+ | * you can put this function in a file that is sourced when you log in, for example / | ||
+ | * of course you can omit '' | ||
+ | * on my machines, the duplex setting is ignored, it's always printing on one side of the paper |