general linux
using a lockfile to run a script only one at a time
#!/bin/bashlockfile="$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 1fiprintf '%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 listId Title Size Created0B3X9GlR6EmbnenBYSFI4MzN0d2M drive-freebsd-amd64 5 MB 2013-01-01 21:57:010B3X9GlR6EmbnOVRQN0t6RkxVQk0 drive-windows-amd64.exe 5 MB 2013-01-01 21:56:410B3X9GlR6Embnc1BtVVU1ZHp2UjQ drive-linux-arm 4 MB 2013-01-01 21:57:230B3X9GlR6EmbnU0ZnbGV4dlk1T00 drive-linux-amd64 5 MB 2013-01-01 21:55:060B3X9GlR6EmbncTk1TXlMdjd1ODQ drive-darwin-amd64 5 MB 2013-01-01 21:53:34
file upload
$ drive upload --file drive-linux-amd64Id: 0B3X9GlR6EmbnU0ZnbGV4dlk1T00Title: drive-linux-amd64Size: 5 MBCreated: 2013-01-01 21:55:06Modified: 2013-01-01 21:55:06Owner: Petter RasmussenMd5sum: 334ad48f6e64646071f302275ce19a94Shared: FalseUploaded 'drive-linux-amd64' at 510 KB/s, total 5 MB
file download
$ drive download --id 0B3X9GlR6EmbnenBYSFI4MzN0d2MDownloaded 'drive-freebsd-amd64' at 2 MB/s, total 5 MB
file sharing
$ drive share --id 0B3X9GlR6EmbnOVRQN0t6RkxVQk0File '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.txtId: 0B3X9GlR6EmbnVHlHZWZCZVJ4eGsTitle: hello.txtSize: 12 BCreated: 2013-01-01 22:05:44Modified: 2013-01-01 22:05:43Owner: Petter RasmussenMd5sum: e59ff97941044f85df5297e1c302d260Shared: FalseUploaded 'hello.txt' at 6 B/s, total 12 B
show the file content on stdout
$ drive download --stdout --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGsHello World
get file info
$ drive info --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGsTitle: hello.txtSize: 12 BCreated: 2013-01-01 22:05:44Modified: 2013-01-01 22:05:43Owner: Petter RasmussenMd5sum: e59ff97941044f85df5297e1c302d260Shared: False
get file download url
$ drive url --id 0B3X9GlR6EmbnVHlHZWZCZVJ4eGshttps://drive.google.com/uc?id=0B3X9GlR6EmbnVHlHZWZCZVJ4eGs
find file by content
grep -lr "text to fine" *.txt