DEV Community

Kevin Cui
Kevin Cui

Posted on • Originally published at kevcui.github.io on

Replace 8 Frequently Used Linux Commands!?

coverimage

Almost a decade ago, I started to touch the command-line interface (CLI). I’m now a nerdy CLI lover and I’m so obsessed by its efficiency. Therefore, I’m always trying out new command-line commands. From time to time, some commands are replaced by other improved ones in my daily workflow. Especially for some frequently used commands, it’s worth to find better alternatives.

Here is a list of improved commands I’d like to share with you:

cd? z, up and fzf!

z

z is a very popular alternative command for jumping around. z doesn’t require any additional configurations. Although it works out of the box, you need just a little bit of patience. Because z has a “magic” algorithm based on “frequency” which can learn from the frequent vested directories after your cd around for a while.

An easy and quick example how to z:

~$ pwd
/home/foo/bar/one/two/three

~$ z tw
~$ pwd
/home/foo/bar/one/two

~$ z foo
~$ pwd
/home/foo

~$ z th
~$ pwd
/home/foo/bar/one/two/three
Enter fullscreen mode Exit fullscreen mode

Want to know how “frequency” score looks like for all directories? Simply run command z -l will give you a list.

Want to clean some/all saved directories? Simply modify $HOME/.z.

up

up is an interesting command to replace cd .. and this ridicules one cd ../../../../

~$ pwd
/home/foo/bar/one/two/three

~$ up
~$ pwd
/home/foo/bar/one/two

~$ up 2
~$ pwd
/home/foo/bar

~$ up h
~$ pwd
/home
Enter fullscreen mode Exit fullscreen mode

fzf

fzf is a versatile command, which can be combined in other commands. With the help of its amazing searchability, it can be used to “cd into the selected directory”:

fzf example

ls? exa!

exa aims to be the modem replacement for ls. In my opinion, it did it. exa is really easy to use. Its options are straightforward:

exa-example

Here are my aliases:

alias ls='exa -s mod --git'
alias ll='exa -l -s mod --git --time-style=long-iso'
Enter fullscreen mode Exit fullscreen mode

More options to tweak exa: exa options

cat? bat!

bat is a cat clone with syntax highlighting, line numbers and automatic paging (using less as the default pager). To display the contents of a file using bat looks like:

bat-example

It’s possible to customize the theme of syntax highlighting. It supports Sublime Text syntax format. Check these default themes to have some inspirations: bat themes

grep? rg!

rg, ripgrep, is fast & furious. It has almost the same parameters of grep command, with improved output style and speed.

One main usage of grep for me is to find out which files contain some specific contents. An example to compare grep and ripgrep:

grep-example

ripgrep-example

If you’re interested in how fast rg can be? Check this comparison: rg vs. grep vs. ag. vs. ack… Not sure if it’s still the case now. But overall, ripgrep is fast enough for my daily use.

find? fd and fzf!

fd

fd is a replacement for find focusing on user-friendly, with simplifying options of find. For example, I want to edit a file but I don’t remember what’s its full path actually is. Usually, I need to find its full path first and then edit it:

~$ find . -ipath "*how*exp*" -name "index.html"
./2017/04/17/how-i-do-exploratory-testing/index.html
~$ vi ./2017/04/17/how-i-do-exploratory-testing/index.html
Enter fullscreen mode Exit fullscreen mode

Since I know the result will be only one file found, I can combine vi inside find command as well:

~$ find . -ipath "*how*exp*" -name "index.html" -exec vi {} \;
Enter fullscreen mode Exit fullscreen mode

With fd, the command is more readable:

~$ fd -p 'how.*exp.*/index'
2017/04/17/how-i-do-exploratory-testing/index.html
Enter fullscreen mode Exit fullscreen mode

fzf

fzf has an extremely powerful fuzzy searchability. It can nest finding steps directly with another command. I bind the key ctrl-t to enable fzf. As the same example as above, but with fzf, it’s a completely different story:

~$ vi (press ctrl-t)
Enter fullscreen mode Exit fullscreen mode

Another fzf example

An alias to combine vim and fzf:

alias vif='vim $(fzf)'
Enter fullscreen mode Exit fullscreen mode

kill? fkill!

fkill, its f stands for “fabulously” (It’s definitely not the word I’m thinking in my mind 😉). Sometimes, kill some zombie processes is necessary. fkill can search a process and kill it interactively. It makes killing stylish:

fkill example

watch? loop!

“UNIX’s missing loop command.” loop is designed to fill the gap. It works well to replace watch:

# monitor current directory and print file list every 6 seconds

# watch
~$ watch -n 6 ls -l

# loop
~$ loop -e 6s 'ls -l'
Enter fullscreen mode Exit fullscreen mode

Moreover, loop has some awesome options to determine when to stop watching:

...
-c, --until-contains <until_contains> Keep going until the output contains this string
-r, --until-error <until_error> Keep going until the command exit status is non-zero, or the value given
-m, --until-match <until_match> Keep going until the output matches this regular expression
-t, --until-time <until_time> Keep going until a future time, ex. "2018-04-20 04:20:00" (Times in UTC.)
...
Enter fullscreen mode Exit fullscreen mode

Example: stop watching when stop-now file is created and then open it in vim:

~$ loop -e 6s 'clear;date;ls -l' -c 'stop-now'; vi stop-now
Enter fullscreen mode Exit fullscreen mode

loop example

Actually, using loop to replace for command is also promising and full of pleasure:

# create 10 markdown files: 0.md, 1.md ... 9.md

# for
~$ for ((i=0; i < 10; i++)); do touch ${i}.md; done

# loop
~$ loop -n 10 'touch ${COUNT}.md'
Enter fullscreen mode Exit fullscreen mode

man? tldr, pet and how2!

tldr

tldr is a simplified collection of man pages. It returns some command snippets with practical examples:

tldr-example

pet

pet is a CLI snippet manager. It’s a handy tool to save some commands snippets which are easily forgotten. The idea is to use pet building own reference manuals.

how2

how2 takes another approach to search for the right command: it queries questions and answers from stackoverflow!

how2 example

In case the displayed answer is not what exactly you want, how2 provides the interactive mode to select in a list of all relative questions and answers.

From now on, we can get confident to “save the world”!

xkcd-tar


Share this list if you think it’s a good read for other CLI nerds. 💝

Drop me a message if you have other commands to recommend. ❤️

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

I think bat is a nice syntax highlighter and line numberer but I disagree with it trying to replace cat. It's a different job, and I wish they hadn't gone that way with it.

I don't personally see much use with z or up, and I think exa, loop and fd are at best incrementally better, and probably worse than the things they're trying to replace.

I like fzf becuase it's adding something new, and ripgrep because it's standing on the shoulders of ack et al. but it does it really well.

Collapse
 
kigiri profile image
Clément

great list, exactly what I was looking for, thx !

Collapse
 
the_happy_hacker profile image
Ian Pan

Great post Kevin! I love using fd and fzf in the temrinal and ripgrep when searching through my project contents! I learnt "bat" from your post and can't wait to try it out.