User Tools

Site Tools


linux:bash_snippets

Bash Snippets

This page contains a collection of useful shell / bash scripting snippets. For a general introduction see this page.

Date / Time

Convert UNIX timestamps to human-readable date

date -d @1278923870 # Mon Jul 12 10:37:50 CEST 2010

Date loop

 for i in `seq -w 1 31`; do ./myscript 2007-08-$i; done

You can use inner loops for month and year of course.

File Juggling

Recursively count number of files in directory

find DIRECTORY -type f -print | wc -l

Parse a Logfile

  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

Make a tar'ed archive from selected files

# starting from current directory, recursively tar and compress everything NOT ending with .csv
tar cfvz  output.tar.gz * --exclude "*.csv" 

Remove empty lines from text

 grep -v '^$' input.txt > output.txt

Or

 sed -i '/^$/d' input.txt

Remove newlines from text

 tr "\n" " " input.txt

Renaming Stuff

Use the “rename” program.

rename 's/\.bak$//' *.bak # removes the .bak suffix from all files matching *.bak

Change suffix for multiple files in a directory

*.hh → *.h

rename 's/\.hh/.h/' *.hh

Change name for multiple files in a directory

XML taxi files from heedfled have syntax of webservices as name:

  • First step:
     rename -n s/.*Start=// FCD*
  • Second step:
      rename -n s/T.*// 2007*

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

  find / -name <fieename>

Find files with time restriction

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

  • -ctime
  • -mtime
  • -atime
  • .. (see man page)

Delete Files Older than 15 days

  find /path/to/dir -mtime +15 -delete

Execute a command on every found file

  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 '{}' \;

EXIF

Exchangeable Image File Format

Adjust EXIF information

# 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

Retrieve GPS locations

exiftool -n -p '$gpslatitude, $gpslongitude, $gpsdatetime' image.jpg

Rename file according to EXIV 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

PDF

Shrink pdf files

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

Split pdf files

pdfposter -p 2x1a4 in.pdf out_across_2_A4_pages_sidebyside.pdf

see also https://wiki.ubuntuusers.de/pdfposter/

linux/bash_snippets.txt · Last modified: 2019/09/19 14:39 by mstraub