Bash tips and tricks

Updated:

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. Create nested directories
    2. Change extension of files (easily)
    3. Optimise images
      1. Optimise PNG Files
      2. Optimise JPG Files
      3. Optimise GIF FIles
    4. Optimise PDF 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 (no the same as clear command which clears the screen) for whatever reason, remember history -c.

Files and directories

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

Optimise pdf files

Be more careful here, compressing pdf files means losing quality. You will need ghostscript to do this. You can install it with dnf install ghostscript (or equivalent on your system, for example apt-get install ghostscript) with root privileges (sudo if you're not logged in as root).

The output pdf file might end up being bigger than the original file, always check the file size after running the command to see if you save space.

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=smallerPDF.pdf originalFile.pdf

You can change -dPDFSETTINGS=/prepress depending on what you're looking for (quality vs size) based on the table below.

dPDFSETTINGS valuesDescription
/screenSmallest size, low quality (72 dpi)
/ebookSmall size, medium quality (150dpi)
/prepressBigger size, higher quality (300 dpi)

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