Sunday, October 9, 2016

Show file in git at specific commit

I wanted to double check what a script looked like two commits ago.

git log path/to/file

Get the commit number from the version I want to see, get the first 4 to 6 characters.

git show commit#:path/to/file

Credit goes here

Tuesday, May 31, 2016

List of PIDs only

I wanted a list of pids only so I could run them through a while loop for processing. To get just a list of pids on Linux I used:

ps --no-headers -o pid -C processname

Thursday, April 14, 2016

Get filename without path, filename without extension, extension of filename in Bash

SOURCE

Get filename without path
basefilename=$(basename /full/path/to/file.txt)

Get filename without extension
filewoext="${basefilename%.*}"

Get extension of file
fileext="${basefilename##*.}"

Friday, April 1, 2016

Arrays in Bash

#!/usr/bin/env bash

# Create the array
myarray=()

# Add items to the array
# Add a sequence of numbers from 1 to 16
#for item in {0..16}
cd ~/Documents/

# Add a list of files to the array
# List all *.txt files in the ~/Documents directory and grab the three oldest
# To grab the 3 most recent files use ls -rt. Order by time in reverse with most recent listed last
for item in $(ls -t *.txt | tail -3)
do
  myarray+=("${item}")
done

# Iterate over the array and print out items
for item in ${myarray[@]}
do
  printf "Item: ${item}\n"
done

# Print out the number of elements in the array

printf "${#myarray[@]}\n"

Friday, March 25, 2016

Fedora 23 netinstall URL

Not sure why I found this so challenging, maybe because I haven't done a netinstall in ages. Anyway, finally figured out how to setup the Installation Source

your selected mirror/path/to/installation/end with os/

So for me it was:

http://fedora.mirrors.tds.net/fedora/releases/23/Server/x86_64/os/

Key was to end it with os/.

Monday, February 8, 2016

Move directory in one git repository to another repository

I've been looking for a way to move a directory in a git repository to a new repository and preserve the history. We are reorganizing some code at work so that's what prompted me to start looking. The scenario is I have a repository and structure that looks like the following:

repoA/
 file1
 file2
 file3
 directory1/
  file1a
  file1b
  file1c
 directory2/
  file2a
  file2b

I want to move directory1 and all of the files in that directory(file1a, file1b, file1c) to a new git repository and preserve the change history. I want to end up with the following:

repoB/
 directory1/
  file1a
  file1b
  file1c

The solution is here. I started with this solution but with the second solution I ended up with the files from my directory1 in the root of the repo. By the time I moved the files into a new directory1 the change history was stored in the root directory of the new repo so if I was in directory1 in repoB I would need to type git log --follow file1a to actually get the full history. Not a big deal but with the first solution listed my directory structure follows, so no need to git mv the files, AND my change log is with the file in the new repo and directory.

Get PID from running process


pgrep -f processname(i.e. vim, emacs, postgres, etc.)

Wednesday, January 20, 2016

Too many files for ls

Using ls you receive the error that there are too many arguments:

Example:

find /path/to/files/ -name "*.ZIP" -exec basename {} \;

Saturday, January 2, 2016

Get path to current script in Python

import os 
os.path.dirname(os.path.realpath(__file__))
 
Source 

Python string formatting in print statements

#!/usr/bin/env python

# Format using a dictionary
print '%(language)s has %(number)03d quote types.'
% {"language":"Python","number":2}

# Format using a list

print '%s has %03d quote types.' % ("Python",2)

# Best method

print '{} has {} quote types.'.format("Python",2)

Planning to post more code snippets this year to remind me of how to do various things in Python.

Source: Pyformat