Bash tips and tricks

Updated:
Edit: Fix some typos

This article should provide some tips and tricks for bash and command line in general. It's part sharing knowledge of things I use a lot, and part saving things for later I might use some day and I don't think I'll remember. I'm planning to slowly expand this article with more things, as I remember them or find something new.

Content:
  1. History
    1. List last [10] commands
    2. Reuse last command
    3. Search bash history
    4. Clear bash history
  2. Files and Directories
    1. Reading files
    2. Create nested directories
    3. Change extension of files (easily)
    4. Optimise images
      1. Optimise PNG Files
      2. Optimise JPG Files
      3. Optimise GIF FIles
  3. Misc. bash shortcuts

History

history command is quite useful. Especially if you use terminal a lot.

List last [10] commands

If you want to list last 10 commands, you can use history 10 (mind you, this will include history 10 itself, as it's the last command in ~/.bash_history file. Change the number to whatever number you desire.

Reuse last command

If you want to use the same command you've just used, you can use your arrow key, or !!. Advantage of using !! is that you can use regexp to substitute parts of the command. !!:s/[what you want to change]/[to what you want it to change]/

Note: Great thing about !! is that if a command fails, because you forgot to use sudo, you can write sudo !! and run it again.

Note: If you just want to re-run the last command, you can also use a shortcut ALT+.

Search your history

This is a shortcut. You can use CTRL+R to search your bash history.

Clear history

If you need to clear history (not the same as clear command which clears the screen) for whatever reason, remember history -c.

Files and directories

Reading files

tail

tail file is a command that by default outputs last 10 lines of a file (it's tail). You can use arguments, such as -n 24 to print last 24 files, or -n +7 to get all lines after 7th (including) line.

What I use a lot is tail -f file, which appends data as the file grows. Quite helpful with logs.

tac

tac is essentially reverse cat command, where it outputs a file last line first.

Create nested directories

Example: You want to create ~/a/b/c/d, but there's no directory a. If you use mkdir ~/a/b/c/d/, you will get an error telling you so.

Use -p flag to create all the directories in the path. That would be mkdir -p ~/a/b/c/d and directories a, b, c, and d will be created.

Change extension of files easily

If you're changing extension of a file, it can get annoying with long names. The most obvious way would be mv file.abc file.xyz. However, you could also use mv file{abc,xyz}, which does the same job.

Note: This also works on directories, and with cp.

Optimise images

Image files can take a lots of space on your hard drive. Let's see how we could make them smaller. Optimisation won't work every time, sometimes the output file will be bigger, however on average you can save some space.

Optimise png files

We will use optipng, first we have to download it. Run dnf install optipng (or equivalent on your system, for example apt-get install optipng) with root privileges (sudo if you're not logged in as root)

Run optipng FileToOptimise.png

You can run optipng ./*.png for all png images in your directory.

Optimise jpg files

We will use jpegoptim, make sure it's installed.

Run jpegoptim FileToOptimise.jpg or jpegoptim FileToOptimise.jpeg dependinding on its extension.

You can run jpegoptim ./*.jpg for all jpg images in your directory. Alternatively run jpegoptim ./*.jpeg for all images with jpeg extension in your directory.

Optimise gif files

Warning: Reducing colours will degrade quality of the image!

We will use gifsicle which needs ffmpeg. dnf install gifsicle and if you don't have ffmpeg installed yet, install it, too. dnf install ffmpeg

You can compress gif files by reducing (limiting) colours in the file. This will result in a smaller file, but also fewer colours (reduced quality). We can limit colours to 256.

gifsicle -i original.gif -O3 --colors 256 -o smallerGIF.gif

Misc. shortcuts

Note: CTRL+U is helpful in password prompt, too, when you think you've made a typo and need to retype it.

13/01/24
see you, space cowboy

Share on fediverse: