User Tools

Site Tools


linux:bash_snippets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:bash_snippets [2015/05/11 22:56]
mstraub
linux:bash_snippets [2019/09/19 14:39]
mstraub [PDF]
Line 1: Line 1:
 +====== Bash Snippets ======
  
 +This page contains a collection of useful shell / bash scripting snippets. For a general introduction see [[linux:​bash|this page]].
 +
 +===== Date / Time =====
 +
 +==== Convert UNIX timestamps to human-readable date ====
 +<code bash>
 +date -d @1278923870 # Mon Jul 12 10:37:50 CEST 2010
 +</​code>​
 +
 +==== Date loop ====
 +<​code>​
 + for i in `seq -w 1 31`; do ./myscript 2007-08-$i; done
 +</​code>​
 +You can use inner loops for month and year of course.
 +
 +===== File Juggling =====
 +
 +==== Recursively count number of files in directory ====
 +<code bash>
 +find DIRECTORY -type f -print | wc -l
 +</​code>​
 +
 +==== Parse a Logfile ====
 +<code bash>
 +
 +  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
 +</​code>​
 +
 +==== Make a tar'ed archive from selected files ====
 +<code bash>
 +# starting from current directory, recursively tar and compress everything NOT ending with .csv
 +tar cfvz  output.tar.gz * --exclude "​*.csv" ​
 +</​code>​
 +
 +==== Remove empty lines from text ====
 +<​code>​
 + grep -v '​^$'​ input.txt > output.txt
 +</​code>​
 +Or
 +
 +<​code>​
 + sed -i '/​^$/​d'​ input.txt
 +</​code>​
 +
 +===== Remove newlines from text =====
 +<​code>​
 + tr "​\n"​ " " input.txt
 +</​code>​
 +
 +==== Renaming Stuff ====
 +Use the "​rename"​ program.
 +
 +<code bash>
 +rename '​s/​\.bak$//'​ *.bak # removes the .bak suffix from all files matching *.bak
 +</​code>​
 +
 +=== Change suffix for multiple files in a directory ===
 +*.hh -> *.h
 +
 +<code bash>
 +rename '​s/​\.hh/​.h/'​ *.hh
 +</​code>​
 +
 +=== Change name for multiple files in a directory ===
 +XML taxi files from heedfled have syntax of webservices as name:
 +
 +  * First step:
 +<code bash>
 +     ​rename -n s/​.*Start=//​ FCD*
 +</​code>​
 +
 +  * Second step:
 +<code bash>
 +      rename -n s/T.*// 2007*
 +</​code>​
 +NOTE: remove -n (=no-act) to run the rename
 +
 +=== Dry run ===
 +Run your rename with -n to see which files would be renamed.
 +
 +
 +===== Finding Stuff =====
 +
 +==== Find a file ====
 +<code bash>
 +  find / -name <​fieename>​
 +</​code>​
 +
 +==== Find files with time restriction ====
 +Find files older than 10 minutes.
 +<code bash>
 +  find /​path/​to/​dir -mmin +10
 +</​code>​
 +
 +
 +Find files younger than 10 minutes
 +<code bash>
 +  find /​path/​to/​dir -mmin -10
 +</​code>​
 +
 +
 +Other options
 +  * -ctime ​
 +  * -mtime
 +  * -atime
 +  * .. (see man page)
 +
 +==== Delete Files Older than 15 days ====
 +<code bash>
 +  find /​path/​to/​dir -mtime +15 -delete
 +</​code>​
 +
 +==== Execute a command on every found file ====
 +<code bash>
 +  find / -name muh.txt ​ -exec cat '​{}'​ \;
 +</​code>​
 +
 +Multiple -exec switches can be used to execute more commands on one file: 
 +<code bash>
 +  find / -name muh.txt -exec echo '​{}'​ \;  -exec cat '​{}'​ \;
 +</​code>​
 +
 +
 +
 +
 +===== EXIF =====
 +
 +Exchangeable Image File Format
 +
 +=== Adjust EXIF information ===
 +
 +<code bash>
 +# 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
 +</​code>​
 +
 +=== Retrieve GPS locations ===
 +
 +<code bash>
 +exiftool -n -p '​$gpslatitude,​ $gpslongitude,​ $gpsdatetime'​ image.jpg
 +</​code>​
 +
 +=== Rename file according to EXIV timestamp ===
 +
 +Simple rename:
 +<​code>​
 +exiftool '​-FileName<​CreateDate'​ -d '​%Y-%m-%d_%H-%M-%S%%-c.%%le'​ image.jpg
 +</​code>​
 +
 +Rename and rotate:
 +<code bash>
 +renrot -n %Y-%m-%d_%H-%M-%S image.jpg
 +</​code>​
 +===== PDF =====
 +
 +=== Shrink pdf files ===
 +Compresses the images in a pdf to a reasonable size (but still useful for printing)
 +<code bash>
 +gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/​preprint -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
 +</​code>​
 +
 +See also http://​www.ghostscript.com/​doc/​9.05/​Ps2pdf.htm#​Options
 +
 +=== Split pdf files ===
 +
 +<code bash>
 +pdfposter -p 2x1a4 in.pdf out_across_2_A4_pages_sidebyside.pdf
 +</​code>​
 +
 +see also https://​wiki.ubuntuusers.de/​pdfposter/​
linux/bash_snippets.txt · Last modified: 2019/09/19 14:39 by mstraub