Other Posts

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
~$ 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

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 ../../../../

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
~$ pwd
/home/foo/bar/one/two/three

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

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

~$ up h
~$ pwd
/home

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”:

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:

1
2
alias ls='exa -s mod --git'
alias ll='exa -l -s mod --git --time-style=long-iso'

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:

1
2
3
~$ 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

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

1
~$ find . -ipath "*how*exp*" -name "index.html" -exec vi {} \;

With fd, the command is more readable:

1
2
~$ fd -p 'how.*exp.*/index'
2017/04/17/how-i-do-exploratory-testing/index.html

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:

1
~$ vi (press ctrl-t)

An alias to combine vim and fzf:

1
alias vif='vim $(fzf)'

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:

watch? loop!

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

1
2
3
4
5
6
7
# monitor current directory and print file list every 6 seconds

# watch
~$ watch -n 6 ls -l

# loop
~$ loop -e 6s 'ls -l'

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

1
2
3
4
5
6
...
-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.)
...

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

1
~$ loop -e 6s 'clear;date;ls -l' -c 'stop-now'; vi stop-now

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

1
2
3
4
5
6
7
# 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'

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!

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. ❤️