Archive

Posts Tagged ‘bash’

Set file’s time stamp to exif header date

December 10th, 2012 No comments

Jhead is a great tool for manipulating jpeg exif headers. To change a file’s time stamp to what’s stored in the header, invoke:

$ jhead -ft filename.jpg

To change the time stamp of all files that were changed within the last day:

find . -mtime -1 -exec -type f jhead -ft {} \;

See also http://www.sentex.net/~mwandel/jhead/usage.html

Categories: Uncategorized Tags:

ps2psdf, A4 paper, landscape and wrong margins

April 24th, 2012 No comments

ps2pdf messes up the paper margins when you run it on a A4 landscape postscript file, because it assumes letter sized paper per default.

To fix this, you can either fiddle around in the shell scripts itself or RTFM and invoke the command correctly:

$ ps2pdf -sPAPERSIZE=a4 myfile.ps myfile.pdf
Categories: Uncategorized Tags: ,

How to find multiple patterns with GNU findutils

November 30th, 2011 2 comments

Actually, searching for multiple patterns should be a trivial task. Find provides a -o operator (and many others) that lets you combine multiple expressions.

A simple Example: You want to find all files in the current directory whose filename extension are either .c or .h

$ find . \( -name "*.c" -o -name "*.h" \) -print

This is not limited to the -name test but can be combined with any other test (like -perm, -size, -type, etc.)

But Careful! You need to quote patterns that contain metacharacters (such as * in the example above). Singe quotes work as well as double quotes. The braces surrounding the expression have to be escaped, too. And watch those spaces right before and after the braces, they’re essential.

See also find manpage, GNU find documentation

Categories: Uncategorized Tags:

Batch converting files from iso-8859-1 to utf-8

May 8th, 2011 No comments

I’ve already posted on how to convert a single file from one encoding to another (iso-8859-1 to utf-8 in this example). But how to do it for a large amount of files, let’s say LaTeX source files for example, in one go?

Here’s a little bash one-liner that could help:

find ~ -type f -name "*.tex" -exec sh -c '
  f="{}"
  iconv --from-code=ISO-8859-1 --to-code=UTF-8 -- "$f" > "$f".tmp
  mv -v -- "$f".tmp "$f"
  ' \;
Categories: Uncategorized Tags:

Remove the last n installed packages with yum

February 6th, 2011 No comments

For n equals 13

yum remove `tail -n 13 /var/log/yum.log | cut -d \  -f 5`

See also cut manpage, tail manpage

Categories: Uncategorized Tags: ,

Where are user based crontab entries stored?

February 3rd, 2011 No comments

Well, assumed that the user is either listed in /etc/cron.allow or not listed in /etc/cron.deny, i.e. the user is allowed to create his own cron table, there should be a file for every user that is allowed to and has a crontab in /var/spool/cron/crontabs/.

Besides editing (crontab -e) or listing (crontab -l) his own crontab entries, root can also specify the name of the user whose crontab is to be tweaked with

crontab -u user -e

See also crontab manpage

Categories: Uncategorized Tags: ,

Display current git branch in bash

August 8th, 2010 No comments

Sometimes it’s very useful to know which git branch you are working on right from the command prompt. There are many solutions out there, but most of them include python and some awk or grep magic which can time a serious amount of time when you cd into a reasonably large git tree.

But you can also take advantage of the __git_ps1 function, provided by /etc/bash_completion.d/git in the git package. Add this line to ~/.bashrc:

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

This setting helps you keep track of which branch you are in at a given time. If you are in a git working directory, it shows the current branch as part of the prompt:

[user@host directory-name (master)]$ 

If you do not have the bash-completion package installed, you must manually source the git completion script prior to using __git_ps1(). To do this, add

source /etc/bash_completion.d/git

to ~/.bashrc.

You might also want to display when there are changes in your work tree or the git index:

[user@host directory-name (master*)]$ 
[user@host directory-name (master+)]$ 
[user@host directory-name (master%)]$ 
  • * indicates that a tracked file was modified
  • + indicates that a tracked file was modified and staged (with git add)
  • % indicates that you have untracked files in your tree

To do so, simply add these lines in your ~/.bashrc, right before the line modifying your prompt:

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true

See the comments at the beginning of /etc/bash_completion.d/git for more details.

Categories: Uncategorized Tags: , ,

Dealing with .rpmnew and .rpmsave files

July 8th, 2010 No comments

When an upgrade includes changes to a default configuration file, the package will write either a .rpmnew or a .rpmsave file instead of overwriting the configuration file on your system. Which file a package creates is up to the discretion of the package maintainer.

From “Dealing with .rpmnew and .rpmsave files” By Bruce Byfield:

An .rpmnew file contains the new default configuration file and leaves your original configuration file untouched. By contrast, and .rpmsave file is a copy of your original configuration file, which has been replaced by the new default file.

The following script can be helpful to find (and possibly merge) those files with your original configuration

for a in $(find /etc /var -name '*.rpm?*'); do diff -u $a ${a%.rpm?*}; done

You may also want to check on yum-merge-conf, a yum plugin to merge configuration files.

Categories: Uncategorized Tags: , , , ,

Extract single file from tar archive

May 12th, 2010 No comments

Sometimes, extracting a whole tar archive can be a waste of time (and temporarily disk space).

To extract just a single file, you can use

tar -x file -zf archive.tar.gz -C /tmp/

This extracts the file file to /tmp/


Of course, most of the time the relative path to the file is not as simple as in the example above:

tar -x path/to/file -zf archive.tar.gz -C /tmp/

This creates the file /tmp/path/to/file.


To avoid the creation of the file’s relative path (or parts of it) in the target directory, use the switch --strip-components

tar -x path/to/file -zf archive.tar.gz -C /tmp/ --strip-components=2

This creates the file /tmp/file


And finally, you can use stdout redirection to achieve the same result

tar -x path/to/file -zf archive.tar.gz -O >/tmp/file

or even shorter

tar xfz archive.tar.gz path/to/file -O >/tmp/file
Categories: Uncategorized Tags:

Find empty folders on Linux

December 1st, 2009 No comments

Well, there is no native command (at least, afaik), but you can use find to easily identify empty folders (which maybe helpful for maintenance, etc.):

# find /path -type d -empty


Find empty folder and list

# find /path -type d -empty -exec ls -ld {} \;


Find empty folder and save as temporary file

# find /path -type d -empty -exec ls -ld >> /tmp/savefiles.txt {} \;


Find empty folder and delete

# find /path -type d -empty -exec rmdir {} \;

See also find manpage

Categories: Uncategorized Tags: