DEV Community

Arpit Mohan
Arpit Mohan

Posted on • Originally published at arpitmohan.com on

Linux Tips And Tricks

It seems that publishing a post of Linux tips & tricks is a rite of passage that all engineers must go through. More importantly, it's a bookmark for my future self to find these eclectic mix of commands easily with minimal googling.

So without further ado, here's my list:

find

This command is very helpful while searching for needles in a haystack. It also has plenty of modifiers (too many to list), so I'll only mention the ones I use almost daily.

The general syntax of the command is:

$ find <starting directory> \
  -type <type of file/directory> \
  -name "<Regex of the file/dir name>" \
  -exec <arbitrary command to execute> {} \;
Enter fullscreen mode Exit fullscreen mode

Basic example looking for java files:

$ find ~/projects -type f -name "*.java"
Enter fullscreen mode Exit fullscreen mode

Example looking for directories called main

$ find ~/projects -type d -name "main"
Enter fullscreen mode Exit fullscreen mode

We can also recursively execute a command on the matching files. This exposes the true power of the find command.

Example: Search all java files for the Main function. The output of this command will only be the matched strings.

$ find ~/projects -type f -name "*.java" -exec grep -i "Main" '{}' \;
Enter fullscreen mode Exit fullscreen mode

Alternatively, if you wish to output the name of the matching file along with the matched string:

$ find ~/projects -type f -name "*.java" -exec grep -i "Main" '{}' +
Enter fullscreen mode Exit fullscreen mode

Notice the difference in how the command ends. The braces '{}' are substituted by each matching file name at runtime. It's similar to how xargs works.

less

For log files (typically large) and files that I wish to only read and
NOT edit, I use the less command extensively. One of the major
reasons I love this command is, I can tail files without it
cluttering my terminal. This command also has the benefit of only
loading the file partially into memory, thereby making it a sleek
alternative to vi

Example:

$ less /var/log/insanely-large-log-file.log
Enter fullscreen mode Exit fullscreen mode

Once inside, you can use G to go to the end of the file or use F to tail the file.

Alternatively, you can open & tail the file directly using

$ less +F /var/log/insanely-large-log-file.log
Enter fullscreen mode Exit fullscreen mode

To quit and return to the terminal, use the normal vi command

<ESC> :q
Enter fullscreen mode Exit fullscreen mode

entr

I recently came across this command and although it hasn't made it to my most frequently used commands, I think it's pretty useful. It would be a crime to compile such a list and not mention entr

This command allows users to execute an arbitrary script whenever a file changes. It's similar to watchr, guard & nodemon. Since entr is written in C, it's faster and more responsive on larger directories.

Usecases can range from running test cases whenever your source files change

$ ls *.c | entr 'make && make test'
Enter fullscreen mode Exit fullscreen mode

or reloading the browser whenever an HTML file changes.

$ ls *.css *.html | entr reload-browser Firefox
Enter fullscreen mode Exit fullscreen mode

You can also restart server processes using the -r modifier

$ ls *.rb | entr -r ruby main.rb
Enter fullscreen mode Exit fullscreen mode

Check out more details here.

htop

If you are looking at your machine's performance in any way apart from
htop, you're doing it wrong. It's what top should have been all along. Although it's not built-in, you can easily install it via:

$ apt install htop
Enter fullscreen mode Exit fullscreen mode

or

$ brew install htop-osx.
Enter fullscreen mode Exit fullscreen mode

The output is very self-explanatory and easier to understand & sort.

jq

Your search for a JSON parser ends here. jq runs on a stream of JSON data. Each input is parsed as a sequence of whitespace-separated JSON values which are passed through each filter of jq. The filters themselves, can be combined in any way by piping the output of one filter to the input of another.

Example:

This example extracts the field 'foo' from the input JSON.

$ jq '.foo' {"foo": 42, "bar": "less interesting data"}
Enter fullscreen mode Exit fullscreen mode
=> 42
Enter fullscreen mode Exit fullscreen mode

This example extracts the 0th element of the JSON array.

$ jq '.[0]' [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
Enter fullscreen mode Exit fullscreen mode
=> {"name":"JSON", "good":true}
Enter fullscreen mode Exit fullscreen mode

vim commands

By popular opinion, vim is the awesomest editor in town. But for newbies, it can be a little daunting and un-friendly. Once you get vim, you'll never go back to any other editor.

The following vim commands aren't for newbies. It's for more advanced users.

How often do you open a file in vim to edit it and realize you should have opened it as root? You can use the following command to save your changes without exiting vim.

<ESC> :w !sudo tee %
Enter fullscreen mode Exit fullscreen mode

Slightly longer to type but completely worth it.

I think vim commands demand a dedicated post to do justice to the most beloved editor.

Conclusion

I hope this was useful. I'll probably edit this post to reflect any new commands that cross my path/blow my mind.

Top comments (7)

Collapse
 
polyluxus profile image
Martin Schwarzer

While the list is quite nice, and I couldn't agree more about vim, these are all standalone programs, not limited to bash.

Maybe worth to mention that you don't need the command colon in less, a simple button press should do in most cases. Also I believe pressing v, should open vi directly from the file. That makes it convenient to just preview, find stuff, and edit right away.

Collapse
 
mohanarpit profile image
Arpit Mohan

Crap, I never realized that I didn't need the colon in the less command. Always used it like vi (muscle memory) and never bothered to check on this. Thanks! #TIL

Collapse
 
polyluxus profile image
Martin Schwarzer

You should see it like this: the Devs of less were clever enough to not make the colon a problem for vi. Probably because they were using vi ;)

Collapse
 
ferricoxide profile image
Thomas H Jones II

I literally cringe every time I see the -exec flag used with find. Pipe your find to xargs. Your system will thank you. :)

Collapse
 
mohanarpit profile image
Arpit Mohan

One of the reasons that I like -exec is that it makes the find command complete in it's own right.

Also, using the syntax find -name "*.java" -exec grep Main {} \+ is equivalent to xargs in terms of performance. Also it's shorter to type as compared to find -name "*.java" -print0 | xargs -0 grep "Main"

Collapse
 
ferricoxide profile image
Thomas H Jones II

I'm an old guy. Even in the early 2000s (nearly 10 years into my career), using find with its -exec flag — rather than piping things through xargs — was a great way to bring a system to its knees. Between the slowness of storage and blowing out the process-table space on the much lower memory systems, at the time, you kind of habituated to shuddering at just the thought of someone using -exec. Painful memories die hard, even when you know the apparent cost barely registers, any more.

Collapse
 
pkristiancz profile image
Patrik Kristian

I didnt know about entr command, i am looking forward to try it. Thank you! :)