Azer Koçulu
August 13th, 2018

Linux Commands For Productivity

In this blog post, I'll share the Linux commands that saved a lot of my time when getting complicated tasks done. This is a personal list — I basically selected them from my digital notebook. Hopefully it's useful for you as well. If you see anything that needs improvement please send a pull request!

P.S. Thanks Mattia making corrections in this post.

10. mount

Are you one of those spending hours configuring NGINX to serve static websites from your home folder? If you haven't met mount yet, then:

$ mount --bind ~/my-new-website /var/www

It could resemble ln, but it's quite different. The above command basically binds a folder in your home directory to /var/www, which NGINX has full access to, so the time spent on configuring NGINX user permissions is saved for calling parents, taking walk in a nearby park :)

9. nl

nl adds line numbers to the beginning of lines. It skips empty lines by default, unlike cat -n or less -N:

$ nl
1  print "Hello World!"
2  print "Hello Again"
3  print "I like typing this."

You can optionally apply more conditions, such as counting lines matching regular expressions.

8. ufw

Uncomplicated Firewall (ufw) makes firewall configuration easier than exiting Vim. For example, you can allow a specific port to be accessible by any IP:

$ ufw allow 22

Or, if you want to allow only specific IPs:

$ ufw allow from $trustedIP to any port 22

7. sort

As the name suggests, this command sorts the content of a given file or files. Let's create an empty file with some numbers:

$ cat > foobar.txt
5 Foo
3 Bar
4 Qux
2 Quux
1 Span

sort will, by default, sort the input by the number found at the beginning of every line:

$ sort foobar.txt
1 Span
2 Quux
3 Bar
4 Qux
5 Foo

6. column

column is great for making command-line output less messy. Let's say your bash script has a help option, and it looks like this:

  $ cat help.txt
start: Start development mode.
stop: Stop development mode.
compile: Compile the binary.

It's hard to read that! We can add some spacing with column:

  $ column -t -s ':' help.txt
start     Start development mode.
stop      Stop development mode.
compile   Compile the binary.

5. yes

yes automatically approves all confirmations. This is quite useful when you're automating some tasks that involve running commands that require confirmation, such as installations.

Here is an example:

  $ yes | pacman -S wtf

4. uniq

Filters duplicate lines, as its name suggests. Let's create a file with duplicate lines first:

$ cat > file.txt

We can output the file without the duplicates, adding counts to the beginning of each line:

$ sort file.txt | uniq -c
1 bar
3 foo
2 qux

3. awk

Awk is a data manipulation language, and it's probably one of the most powerful command-line tools ever. In this post I'll only cover some basic use cases for it. For example, let's say we want to get the list of disks in our system:

  $  lsblk -l
nvme0n1   259:0    0   477G  0 disk
nvme0n1p1 259:1    0   511M  0 part /boot
nvme0n1p2 259:2    0 476.5G  0 part /

This looks too verbose to my eyes. I only want to see the name and the size of the disks. awk can help me:

$ lsblk -l | awk '{print $1,$4}'
nvme0n1    477G
nvme0n1p1  511M
nvme0n1p2  476.5G

In the example above, we asked awk to select and print the 1st and the 4th column with '{print $1,$4}'.

We can apply some conditions, too. Let's list the disks that are bigger than 250 GB:

$ lsblk -bl | awk '$4 > 268435456000  {p about `sloprint ;}'
nvme0n1   259:0    0 512110190592  0 disk
nvme0n1p2 259:2    0 511572967424  0 part /

This time we filtered the 4th column in the output by checking if the value is bigger than 250 GB with '$4 > 268435456000 {print ;}'.

2. convert

convert is a powerful command-line image manipulation utility. You can get simple tasks done easily, like converting between formats, but this utility does more than that.

Here is an example: We downloaded a high-resolution wallpaper and want to scale it down to 2000 px, keeping its original proportions.

$ convert -scale 2000 ~/wallpaper.jpg  /tmp/wallpaper.png

Let's do something more complicated than just resizing: We'll add a black, transparent overlay on top of the wallpaper and write a Goethe's quote in the middle of it, just to make it look cool.

$ convert ~/wallpaper.jpg \
       -scale 1500 \
       -fill black -colorize 50% \
       -font System-San-Francisco-Display \
       -fill "#ffffff33" \
       -gravity center -pointsize 30 -annotate +0-200 'A man sees in the world what he carries in his heart. — Goethe' \

Here is the result:

1. ffmpeg

I remember spending hours looking for a simple audio or video editing tool when I was not using Linux. ffmpeg is one amazing command that can be used for editing both audio and video files.

For example, you could trim an mp3 file with it:

$ ffmpeg -i input.mp3 -ss 00:00:20 -to 00:00:40 -c copy output.mp3

The above command will simply create a new mp3 file between 20 (--ss) and 40 (--t) seconds. You could apply the same command to a video file, too:

  $  ffmpeg -i input.mp4 -ss 00:00:30 -t 00:00:11 output.mp4

ffmpeg can record video, too. If you wanted to record your screen to share with others, here is the command to do that;

$ ffmpeg -video_size 1200x600 -f x11grab -i :0.0 output.mp4

Let's explain the parameters above:

Optionally, we can choose a specific x/y value instead of the left/top of the screen:

  $  ffmpeg -video_size 1200x600 -f x11grab -i :0.0+250,150 output.mp4

How would you know the coordinates of where you want to point the left and top of the video though? xdotool has an option to output mouse location:

$ xdotool getmouselocation --shell

This is not handy enough though. Check out slop below.


Someone who saw this article on HN recommended using slop instead of xdotool, and I really liked it. slop basically lets you select a region in your screen, then outputs that region. Really cool:

$ slop

This is great. I created my custom screen recording command that first lets me select the part of the screen I want to record, and saves the result to ~/screenshots/{date-time}.mp4 in my system.

Here is the script:

slop=$(slop -f "%x %y %w %h %g %i") || exit 1
read -r X Y W H G ID < <(echo $slop)
ffmpeg -f x11grab -s "$W"x"$H" -i :0.0+$X,$Y -f alsa -i pulse ~/screenshots/`date +%Y-%m-%d.%H:%M:%S`.mp4

The only limitation is, you need to interrupt the script to stop the recording. Check out maim if you're interested in a screenshot command based on slop.

Hopefully this post had some commands that you found useful. You can share your feedback with me by e-mail, or send a pull request if you think there could be some improvements!