UNIX příkazová řádka

Linux or UNIX command line reference - examples

Find files

  -mtime +30 means file modified 30 days ago,

  -mtime -30 means less than 30 days,

  -mtime 30 means exactly 30 days.

 

$ find /home/myhome -iname "*.html" -mtime -30 -print

Find html files last modified 30 days ago.

$  touch -mt <Month-Day-Hour-Minute> /tmp/empty_file

find / -newer /tmp/empty_file -local -print

Find files modified within the last x hours 

  $ find . -name '*.txt' -mv cp  {} /tmp/ \;

Find and copy files

Truncate multiple filenames to 40 characters

for f in *; do mv $f ${f:0:40}; done 

Rename multiple files

Upper to lower case:

rename 'y/A-Z/a-z/' *

Strip spaces:

rename 's/ //' *.JPG
  file-01.jpg to file-1.jpg

  file-02.jpg to file-2.jpg

  rename 's/-0(.)\.jpg/-$1\.jpg/' *

Find text in files

grep -lr --include=*.html MyText .

find MyText in html files in current directory and subdirectories and list files containing MyText.

Check broken links

linklint -error -warn -xref -forward -out linklint.out -net -http -host <site> /@

Replace text in multiple files

perl -pi -e 's/old/new/' *.html

Replace line in file with line in another file based on matching string

awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file2 file1
  1.  awk buffer the file2 data in the form of key, value pair (associative array) -  key as col1 and value as entire line.
  2. then awk will check whether array key is exist for column 1 in file1 (joining columns) if yes, then replace file1 current value (i.e $0) with current value in array, if not skip it.
  3.  finally print the data.


Backup and restore mysql database

mysqldump db_name -u root -p > file.sql
mysql -u root -p db_name < file.sql

Adding db user:

  mysql> create database db_name;

  mysql> grant all on db_name.* to 'user'@'localhost' identified by 'password';

  mysql> flush privileges;

VI Editor

Delete blank lines:

:g/^$/d
:g/^ *$/d

Remove duplicate rows (must be sorted)

:%s/^\(.*\)\n\1$/\1/

Replace string:
Every occurrence in file:

:%s/OLD/NEW/g 

Between two lines #,#:

:#,#s/OLD/NEW/g

Globally (all) on current line:

:s/OLD/NEW/g

First occurrence on current line:

:s/OLD/NEW

Downloading an Entire Web Site

  wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains website.cz --no-parent www.website.cz

without pictures:
wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains website.cz --no-parent -R jpg,jpeg,png,gif www.website.cz

Convert pdf to swf - separate pages

pdf2swf -p 1-10 -o %.swf file.pdf 

Converts file.pdf pages from 1 to 10 into separated swf files named 1.swf, 2.swf ... 10.swf

Recursively remove all .svn directories

find . -name .svn -print0 | xargs -0 rm -rf

Rename multiple files

Upper case to lower case

for f in `find`; do mv -v $f `echo $f | tr '[A-Z]' '[a-z]'`; done

Replace with regular expressions

for i in `find`; do j=`echo $i | sed 's/-0/-/g'`; mv "$i" "$j"; done

Move files based on year of creation

  for i in `ls -ltr|awk '$8=="2011"{print $9}'`; do mv $i /srv/mail/virtual/mailbox.cz/postmaster/.Archives.2011/cur; done;

Find empty files from the current directory down

find . -size 0 -print