Five Linux tips to save you time!

in #utopian-io7 years ago

image.png

Five Linux tips to save you time!

What will I learn?

Below you will find five time-saving tips for navigating the Linux operating system more efficiently.

Requirements

You are expected to have some experience using Linux.

Difficulty

Basic

Let's go!

1) History Expansion

While history expansion can easily be its own article, I am going to show a few examples.

Run the last command that started with...

Say you used the command vim .bashrc and then did a few other things. If you know that was the last file you edited, you can type !vim to execute the last command that started with vim.

Forget to use sudo?

How many times have you tried to run a command that required escalation but you forgot to use sudo? Probably not as often as you should, because you are likely using root (shame on you!).

apt update

Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)

Oops, this is where !! comes in handy, it will allow you to prefix your last command run.

sudo !! will be replaced with sudo apt get

Execute the last command that contains a string

Similar to the !vim command, there is also a search command if you just want to run something that contained a string but did not have to start with it.

vim /etc/nginx/nginx.conf

!?nginx will execute the following command again:

vim /etc/nginx/nginx.conf

These exclamation point commands will save you a lot of time while using Linux.

2) Kill a process by name

Most everyone is familiar with the ability to kill a process by pid. Did you know you can kill a process by name using killall?

Is a process hung or you want to abort it? Without using ps to find its pid you can use the following command to kill all instances of a process and it's child processes.

killall mysql

You can also use SIGKILL which is more successful for stubborn processes.

killall -SIGKILL mysql

3) Reset your terminal

This one is easy but is super helpful. Have you ever used a curses program that exits and leaves your terminal all funky looking? You likely just exit SSH and reconnect when this happens right?

Well now you will know to just type reset and your terminal will be as good as new.

4) Aliases

I love this trick, do you have a command you type often but it is a syntactical nightmare or you just want a shortcut?

In your .bashrc file, you can specify aliases that will be available to you when you are logged in.

The syntax is alias [alias name]='[command]'

Some good examples.

alias untar='tar -zxvf '
alias wget='wget -c '
alias ls='ls --color=auto'

and my favorite

alias yolo='git commit -am "DEAL WITH IT" && git push -f origin master'

These can be big time savers but they also can cause you to forget the real syntax of commands you use often. Keep that in mind and review them from time to time.

5) Get suggestions and tips for your shell scripts.

This is an amazing tip, there is a tool called shellcheck by Koalaman that will lint your shell scripts and give you feedback on how you can clean them up. If you are particularly weak at creating shell scripts, this will be your best friend.

Install shellcheck

sudo apt update && sudo apt install shellcheck

Once installed, all you need to do is type shellcheck <your script> and it will analyze your script and give you suggestions on how to clean it up. It will detect portability issues, beginner mistakes, style issues, data errors, and more.

image.png

Source

If you don't write shell scripts all day, this tool will become your best friend when the time comes to write one.

Hopefully, you found these helpful and they will save you some time. Let me know if you want to see more Linux tips or my previous Python Tips series.

Sort:  

Thank you for your contribution @themarkymark.
After analyzing your tutorial we suggest the following points:

  • We suggest you make your tutorial more detailed. Explaining a little theory for users to perceive perfectly, especially the most basic users.

Thank you for your work in developing this tutorial.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Thanks a lot! This one's bookmarked now.

I especially liked history expansion (starts with and contains). I can't count how many times I run a command and it needs to be run with sudo. Although I just use the arrow up key to go through the command history and type sudo if need be. But this is a time saver for sure so I don't have to go through all my history and wear out the ink on the arrow up key.

Next favorite is the alias for convenience and so appreciate the warning :)

Damn, I have definetely not enough experience for these tips.
Favorited your article to read him again in some time :)

Posted using Partiko Android

Good suggestions.

  1. Kill a process by name

I use pkill for that.

killall is a bit safer than pkill because it will require an exact match of process name and has a few more options.

i was about to ask this question... because i also use pkill... hahahah

Whichever you prefer, they accomplish the same thing, just slightly different.

I think this is advices for advanced. There is no help for untar "man untar" and etc ..

untar is not a command, it is an alias being created to represent the tar function. You can find more information about tar by looking at “man tar”

Hi @themarkymark!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @themarkymark!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Posts like these are awesome to see. And it reminds me that I've gotten a bit lax in my knowledge base and experience building. Lately, I do just what I need to at work and focus on expanding that information base where I can. But I've let me experience that I got from useful classes at school lapse. I guess I'll have to come up with a way to correct that issue.

This is some great info, thank you!