megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Remove patterns from multiple files

If you wanted to remove a specific pattern in a list of files, like the ones below:

23_2020_03_01_article-three.md
22_2020_02_01_article-two.md
21_2020_01_01_article-one.md

You can simply do that using Regular Expressions and the rename tool:

rename 's/[0-9]+_[0-9]+_[0-9]+_[0-9]+_//' *.md

Now, the desired output should look like this:

article-three.md
article-two.md
article-one.md

This should come in handy if you're lazy to rename each file manually! :)

Rename extensions of multiple files

In this example, we're going to change a list of .txt files to .md files:

#!/bin/bash

shopt -s nullglob
files=($(ls -v *.txt))

for file in "${files[@]}"
do
    mv ${file} ${file}.md
done

s You can use modify this script to rename any extension you want.

Checkout branch

Want to create a new branch in your project? Simple, just do this:

git checkout -b new_branch

By doing this, you'll automatically be shifted to a new branch of your project. To check which branch you're working on, type this:

git branch

And you should be able to see your current branch marked with a *:

master
* new_branch

View the filesize in human-readable format

Wanted to view the size of a file in terminal but don't understand the number of bytes displayed? No worries, just type this command and it'll display the size of the in human-readable format:

du -sh filename.ext

Check RAM and disk space

RAM space

Type the following command to view available memory in your system:

free -h

Disk space

Type the following command to view available disk space in your system:

df -h

Check branch status

Ever wondered if you've edited or committed anything in your project before pushing it to your repository, do this:

git status

Check Ubuntu version

Want to know the current version of your Ubuntu distro? Type this:

lsb_release -r

Clear all lines

  1. Switch to normal mode by pressing ESC key
  2. Press gg and it will take you to first line of the file.
  3. Then type dG and it will clear all the lines from start to the end of the file.