This is an old revision of the document!
This page contains a collection of useful shell / bash scripting snippets. For a general introduction see this page.
date -d @1278923870 # Mon Jul 12 10:37:50 CEST 2010
for i in `seq -w 1 31`; do ./myscript 2007-08-$i; done
You can use inner loops for month and year of course.
find DIRECTORY -type f -print | wc -l
cat $1 | while read line; do sectionId=`echo $line | grep -o "[0-9]\{4\}-[0-9]\{4\}"` statusString=`echo $line | grep -o "auf .*$"` case $statusString in auf\ FREI ) STATUS=1;; auf\ DICHT ) STATUS=2;; auf\ STOCKEND ) STATUS=3;; auf\ STAU ) STATUS=4;; auf\ NICHT\ VERFÜGBAR ) STATUS=5;; esac NEWLINE="${line};${sectionId};${STATUS}" echo $NEWLINE >> out.txt done
# starting from current directory, recursively tar and compress everything NOT ending with .csv tar cfvz output.tar.gz * --exclude "*.csv"
grep -v '^$' input.txt > output.txt
Or
sed -i '/^$/d' input.txt
tr "\n" " " input.txt
Use the “rename” program.
rename 's/\.bak$//' *.bak # removes the .bak suffix from all files matching *.bak
*.hh → *.h
rename 's/\.hh/.h/' *.hh
XML taxi files from heedfled have syntax of webservices as name:
rename -n s/.*Start=// FCD*
rename -n s/T.*// 2007*
NOTE: remove -n (=no-act) to run the rename
Run your rename with -n to see which files would be renamed.
find / -name <fieename>
Find files older than 10 minutes.
find /path/to/dir -mmin +10
Find files younger than 10 minutes
find /path/to/dir -mmin -10
Other options
find /path/to/dir -mtime +15 -delete
find / -name muh.txt -exec cat '{}' \;
Multiple -exec switches can be used to execute more commands on one file:
find / -name muh.txt -exec echo '{}' \; -exec cat '{}' \;
Exchangeable Image File Format
# adjust date/time of exiv tag exiv2 image.jpg # print exiv data exiv2 -a 0:42 image.jpg # add 42 minutes to image timestamp exiv2 -a -1:10 image.jpg # subtract 1 hour 10 minutes from image timestamp
Simple rename:
exiftool '-FileName<CreateDate' -d '%Y-%m-%d_%H-%M-%S%%-c.%%le' image.jpg
Rename and rotate:
renrot -n %Y-%m-%d_%H-%M-%S image.jpg
Compresses the images in a pdf to a reasonable size (but still useful for printing)
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/preprint -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
See also http://www.ghostscript.com/doc/9.05/Ps2pdf.htm#Options