general linux

using a lockfile to run a script only one at a time

#!/bin/bash
lockfile="$0.lockfile"
if [[ -e "$lockfile" ]] && ! kill -0 "$(< "$lockfile")" 2>/dev/null; then
echo "Warning: lockfile exists but the process is dead, continuing."
rm -f "$lockfile"
elif [[ -e "$lockfile" ]]; then
echo "My important work is already running. Exiting."
exit 1
fi
printf '%s\n' "$$" > "$lockfile"
# add all your important stuff here.
rm -f "$lockfile"
exit 0

viewing crontab of all users

for user in $(cut -f1 -d: /etc/passwd); do echo $user; sudo crontab -u $user -l; done

listing only one column of a file

a file with columns separated by : can have its data displayed like this

cat file.log | cut -d":" -f4

showing a file content, removing duplicates

cat file.log | awk '!a[$0]++'

list the recursive contents of a directory

ls -R /my_path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'

besides showing the contents recursively, this command also shows the full path of the files, not just the names.

reference: AskUbuntu.com

simulating the java trim() in bash

echo " test test test " | sed -e 's/^ *//' -e 's/ *$//'

reference: StackOverflow

get a file mime-type

file --mime-type -b my_file

using google drive with bash

this is accomplished with gdrive.

list files

$ drive list
Id Title Size Created
0B3X9GlR6EmbnenBYSFI4MzN0d2M drive-freebsd-amd64 5 MB 2013-01-01 21:57:01
0B3X9GlR6EmbnOVRQN0t6RkxVQk0 drive-windows-amd64.exe 5 MB 2013-01-01 21:56:41
0B3X9GlR6Embnc1BtVVU1ZHp2UjQ drive-linux-arm 4 MB 2013-01-01 21:57:23
0B3X9GlR6EmbnU0ZnbGV4dlk1T00 drive-linux-amd64 5 MB 2013-01-01 21:55:06
0B3X9GlR6EmbncTk1TXlMdjd1ODQ drive-darwin-amd64 5 MB 2013-01-01 21:53:34

file upload

$ drive upload --file drive-linux-amd64
Id: 0B3X9GlR6EmbnU0ZnbGV4dlk1T00
Title: drive-linux-amd64
Size: 5 MB
Created: 2013-01-01 21:55:06
Modified: 2013-01-01 21:55:06
Owner: Petter Rasmussen
Md5sum: 334ad48f6e64646071f302275ce19a94
Shared: False
Uploaded 'drive-linux-amd64' at 510 KB/s, total 5 MB

file download

$ drive download --id 0B3X9GlR6EmbnenBYSFI4MzN0d2M
Downloaded 'drive-freebsd-amd64' at 2 MB/s, total 5 MB

file sharing

$ drive share --id 0B3X9GlR6EmbnOVRQN0t6RkxVQk0
File 'drive-windows-amd64.exe' is now readable by everyone @ https://drive.google.com/uc?id=0B3X9GlR6EmbnOVRQN0t6RkxVQk0

pipe a file content directly to drive

$ echo "Hello World" | drive upload --stdin --title hello.txt
Id: 0B3X9GlR6EmbnVHlHZWZCZVJ4eGs
Title: hello.txt
Size: 12 B
Created: 2013-01-01 22:05:44
Modified: 2013-01-01 22:05:43
Owner: Petter Rasmussen
Md5sum: e59ff97941044f85df5297e1c302d260
Shared: False
Uploaded 'hello.txt' at 6 B/s, total 12 B

show the file content on stdout

$ drive download --stdout --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGs
Hello World

get file info

$ drive info --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGs
Title: hello.txt
Size: 12 B
Created: 2013-01-01 22:05:44
Modified: 2013-01-01 22:05:43
Owner: Petter Rasmussen
Md5sum: e59ff97941044f85df5297e1c302d260
Shared: False

get file download url

$ drive url --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGs
https://drive.google.com/uc?id=0B3X9GlR6EmbnVHlHZWZCZVJ4eGs

find file by content

grep -lr "text to fine" *.txt

reference: Quickly find any text string in any set of files