Chymeric Tutorials

Do awesome stuff - awesomely.

Miscellaneous Cheat Sheet

| Comments | Edit

This is a collection of workflows, commands, or bash scripts solving a series of common use cases. These instructions use linux commands and directory structures.

Apply Git Patch Directly from Remote Server

Many git hosts offer you automatic summaries of pull requests in the form of diffs or patches (simply append .diff or patch to your commit link).

To apply such patches directly (without having to manually download the files) you can run the following command:

1
curl https://your.host/path/to/commit/commit_identifier.patch | git apply --ignore-space-change --ignore-whitespace

The --ignore-space-change and --ignore-whitespace are not needed, but they save you the pain of patches failing due to unmatching white spaces.

Copy Remote Folders

There are a number of commands which you can use to transfer files and folders remotely via the command line interface (CLI). On of them is scp (which stands for secure copy) and uses the SSH protocol.

1
scp -r user@your.server.example.com:/path/to/your/files /your/desired/destination

Keep in mind that /path/to/your/files is an absolute path. If you do not have root access on your server, use a home/directory/relative/path (without the initial slash).

List Files Versioned with Git

To get a list of all the files in your git repository (excluding untracked files from your repo directory), run the following:

1
git ls-tree --full-tree -r HEAD

Batch Rotate Images

There are a number of ways to do this. One would be via the GIMP script-fu, which is awfully complicated to use (as an example of how you would batch rotate files with script-fu, you can see this thread on StackOverflow).

If your image happens to be in the JPEG file format, however, you can easily rotate it with a library function called jpegtran. This function ships with the libjpeg-turbo package, which you already have installed if your system is capable of viewing JPEG format files. To use this function simply run:

replace .JPG with your respective extension formatting
1
find *.JPG -exec jpegtran -rotate 180 -outfile "{}" "{}" \;

An other option is via ImageMagick, which is easy, but which would require you to download software you may otherwise not need. The respective command would be:

replace .png with your respective extension as necessary
1
mogrify -rotate 180 *.png

Paste the last 100 lines from some output

Sometimes your output is too large to paste in whole. If the part of the output your are interested in is located close to the end, tail can be of good use.

If you want to paste from a file, run:

1
tail -n 100 your/file/path | wgetpaste

and if you want to pipe some output directly from a command (e.g. dmesg) to a bastebin, run:

1
dmesg | tail -n 100 | wgetpaste

Change file permissions recursively

This command grants all users read permissions to the folder, subfolders, and all files therein - in addition to whatever permissions are already set. Note that chmod may need to be run as root.

1
chmod -R a+r your/directory/path/

Or better yet, use octal to accurately define what permissions the owner, the group, and everyone else has:

1
chmod -R 775 your/directory/path/

Remove .orig files from (Git) folder

Occasionally Git merges may leave you with residual .orig files which clutter your repository. This question on StackOverflow exemplifies how the issue may arise. To solve the issue run the following command from your repository root.

1
find . -name '*.orig' -delete

Text lookup in folder

The grep command with the -r option lets you find the occurrences of a string inside all files within a folder and all its subfolders.

1
grep -r "yourstring" folder1/ folder2/

Nested Markdown/reST Syntax

Use the following too get a bold (or italic, etc.) link via markdown:

1
**[link name](http://address.you.want.to.point.to)**

In reStructured Text (reST) this feature is on the to do list, but not yet available.

OpenRC: See and edit runlevels

This assumes you are using OpenRC and will not work on systems set up with systemd. Note that rc-update may need to be run as root.

1
rc-update show/add/del

Convert .flac to .mp3

Here you will need ffmpeg - a command line program which ships with the FFmpeg libraries, and comes together with media playback dependencies on many linux distros (meaning that you probably have it installed already).

1
ffmpeg -i your/song/path.flac your/song/path.mp3

Rsync sub-directory

Run from the directory containing DIR. DIR is the directory to be synced and created if needed on the remote host. Do not use trailing slashes after the DIR directory name or all the contents will get dumped directly into your/remote/path/:

1
rsync --filter '- */.*' -e "/usr/bin/ssh" --bwlimit=2000 -av DIR user@server.domain.com:your/remote/path/

Mass copy files without overwriting

Sometimes you want to copy all files in one (large) directory to another - which already contains some of these files. Usually using a file manager of cp (whithout arguments) for such a task can prove quite tedious. Here is an variant using rsync (recommended):

1
rsync -tr /copy/from/here/* /copy/to/here/

And alternative using the --no-clobber argument for cp:

1
cp -n /copy/from/here/* /copy/to/here/

Download flash videos under Linux

1) Youtube-dl

Youtube-dl is a FOSS python script which allows you to download flash videos from not only YouTube, but over 150 websites. You can download it directly from the official website or through your package manager of choice (it is provided by portage and many others).

In case you foresee running into DRM restrictions, you may also want to get RTMPDump. Youtube-dl calls RTMPDump automatically if it encounters Adobe’s proprietary RTMP protocol and the software is installed.

2) Chrome and wget

  • Open your Chrome cache page (at chrome://cache/).
  • Open the page containing your video in a new tab.
  • Start playing the video.
  • Return to your chrome cache page and refresh it, your topmost link should point to the video
  • Copy the link (in plaintext, or copy the link destination and remove the chrome://... part at its start)
  • Run this from your terminal:
1
wget "your_URL" video

Edit apps launched at startup in GNOME 3.*

1
gnome-session-properties

Make Git cache passwords for 10 hours

1
2
git config --global credential.helper cache
git config --global credential.helper "cache --timeout=36000"

Change owner of a directory recursively

Note that chown may need to be run as root.

1
chown -R user your/directory/path/

Comments