Thursday, 29 June 2017

Helpful bash posts

2018-03-17

To show a precis (--oneline) of the recent commits, the branches that they affect (--decorate=auto) and an illustration of the current branches (--graph):

git log --decorate=auto --oneline --graph

Note that --decorate=auto is the default option from git v2.13. For earlier versions (as on my current WSL) you can add --decorate=auto option to your git config:

git config log.decorate auto

2018-03-02
Dont extract the whole of that archive!

If you just want to list the contents:

tar ---list -jf some_archive.tar.bz2
tar -jtf some_archive.tar.bz2

If you just want to extract a specific file from the archive (stores my_filename relative to the current directory making any subdirs that are required):
tar --xjf my_archive.tar.bz2 my_filename

If you want to extract the contents of a specific file, eg, to pipe the data into another command:
tar --to-stdout -xjf my_archive.tar.bz2 my_filename

Thanks to you at nixCraft for the listing code

2017-07-06
The command ":" is an odd one. Why have a NULL command? grep returns with a non-zero error code if it finds no matching values in the input. So in a pipeline like the following:

VAR1=$( do_something file.1 | command2 | grep "some_value" - )

The pipeline would return a non-zero error value if some_value wasn't found in the results of command2. If you're running with option -pipefail or set -e enabled then that final grep will kill your script, even if you're happy to pass an empty list to VAR1 (eg, you want to subsequently do further tests on VAR1). To prevent grep from killing your script you can use

 VAR1=$( do_something file.1 | command2 | grep "some_value" - ) || true
or
VAR1=$( do_something file.1 | command2 | grep "some_value" - ) || :

and the pipeline doesn't return with a non-zero error code. I think it's more common to use true than to use ':' here though.

2017-07-04
Needed to make a backup of an external drive, so that I could repartition the drive. To check that everything's copied over correctly, the md5sums of all the copied files can be checked.This is done recursively, so the md5sums are computed for each subdirectory as well. From here:

find . -type f -exec md5sum {} \; > .checks.md5
grep -v "\.\/\.checks\.md5" .checks.md5 > checks.md5
rm .checks.md5

Then to check all the file-copying has worked properly:

md5sum -c checks.md5

2017-07-03
Anyone else working with some big big files? To print out the amount of storage used by a given directory, use du:

du -h <directory>

This shows how much storage each subdir is using  (-h: in human readable form). To sum this all up and thus compute total disk-usage in and below <directory>:

du -sh <directory>

2017-07-02
sed and awk are fine, but perl provides me a bit more control over regexes. I use this if I need to pass an absolute path to a program that doesn't understand "~" or I want to compare the full paths of two files.

expand_tilde()
{
  echo "$1" | perl -lne 'BEGIN{$H=$ENV{"HOME"}}; s/~/$H/; print'
}

expand_tilde ~/temp/abc
# /home/me/temp/abc

2017-07-01
Find out what the different config files (/etc/profile, ~/.bashrc, ~/.bash_profile) do and when they are called. See here.

2017-06-30
I always used to use backticks for command substitution and only recently learned about using parentheses for this:

$(command) == `command`

Importantly, you can readily nest commands with the former:

$(command $(other_command))

See here for further info (especially about the interplay between quoting and substitution).

2017-06-29
Useful bash tips at shell-fu, for example, no need to open vim to remove vacuous whitespace:

vim -c "1,$ s/ \+$//g | wq" <filename>

2017-06-28
A variety of helpful bash things that I found on the web.

cp file{1,2}.txt == cp file1.txt file2.txt

See here

No comments:

Post a Comment