DEV Community

Mateusz Jarzyna
Mateusz Jarzyna

Posted on

Linux's commands and tricks I'm using in my daily job as a developer

This is not a post from the series of those describing the cd command. It's just a list of commands and tricks I'm using (almost) every day.

Port forwarding

Sometimes I have to connect to database and of course I prefer to use my GUI manager (JetBrains DataGrip).
So, if security policy exist in your company and your database's port is not exposed you can execute

ssh -L{port on your PC}:localhost:{database's port} root@{server IP}

The command below will open port 3308 on your laptop and everything will be forwarded to 192.168.1.2:3306

ssh -L3308:localhost:3306 root@192.168.1.2

localhost means that database is listening on 192.168.1.2. You can type for example 192.168.3.77 and everything will be forwarded to .3.77 server via .1.2.

Edit file in VIM without sudo, but save with sudo

Have you ever edited some configs file and forgot to sudo? Me too... There is a trick to save the file anyway, just type in VIM:

:w !sudo tee %

Explanation

Go to beggining/end of line in terminal

If you wrote a very long command in the terminal it may take a long time before you return to the begging of the line to add missing sudo. And back to the end to add some parameters.
Press crtl + a to move to the begging and crtl + e to the end of the line in terminal.

ll

Save few days in a year by typing ll instead of ls -la. Works on most Linux servers.

Execute command you executed in the past

Last command

To execute last command over again you can of course press ↑ (arrow up) key. But you can also type !!. So executing last command as a root is very easy

sudo !!

To run the last command that started with apt type !apt

Search history

To find the command that contains /tmp you have executed in the past press ctrl + r and type /tmp. Press ctrl + r again for next result.
To show all commands or to search using regular expression use

history | grep "/tmp"

Agree for everything

To say yes for each question you can use application called yes

yes | yum install curl

use yes no to say no and discard.


WARNING

As @patricnox notices in the comment - using yes may do unexpected things. You can accidentally install 10 GB of dependencies or other things you don't want to do.


Run a long-lasting process in the background and close the terminal

If you run a script that will end in 3 days, you don't have to wait with the terminal window open to end. You can run it using nohup command

nohup wget http://large-files.com/10gb-super-movie.avi &

wget works in the background, output is saved to nohup.out file in working directory.

Checking who has stolen your favourite port

It's really annoying when you are trying to run nginx but you can't because there is already apache running and port 443 is busy.
So, how to determinate which process is listening on port 80:

$ netstat -tulpn | grep 80
tcp6       0      0 :::80                 :::*                   LISTEN     10177/java

10177 is a pid you are looking for. Now execute

ps aux | grep 10177

for more details.

Reading logs

Everyone knows that less is a very good way to read a logs files. But you can also read gziped logs without extracting!

less /var/log/my-app/my-app.log.2015.12.14.gz

Live reading

tail -f /var/log/my-app/my-app.log | grep ERROR

The command above will show only new lines that contains ERROR.

Sort process

Show top 3 processes sorted by CPU usage

ps aux --sort=-pcpu | head -n 4

Show top 3 processes sorted by memory usage

ps aux --sort=-rss | head -n 4

Executing command every X seconds

To print command's output every X seconds you can use watch command. For example to create clock run

watch -n 1 date

Quiet mode

A lot of standards commands has quiet or silent mode. Very useful when you are creating some bash script. In most of the cases just add -q or -s (read --help or man or check on StackOverflow)

zip -q archive.zip big-file.jpg

But sometimes (practically always with in-house scripts) you have to ignore the output (send to /dev/null)

./very-verbose.sh 1>/dev/null

Create log files for scripts executed by crontab

0 22 * * 1-5 /opt/scripts/send-report.sh 2>/var/log/scripts/report-error.log

So next time when your script will fail you won't lose the reason

Latest comments (44)

Collapse
 
abhicoding profile image
Abhishek Shingane

Do you have to do compressing and uncompressing of files/folders?

Check disk usage?

List files inside a zip?

Collapse
 
rafaelcg profile image
Rafael Corrêa Gomes

Excellent, my favorite was the !!, thank you for sharing it!

Collapse
 
doctor_brown profile image
James Miranda

Great tips!

Collapse
 
gergelypolonkai profile image
Gergely Polonkai • Edited

ll is actually a convenient alias set on most systems. Aliases are really powerful in and of themselves and worth a separate article (in fact, there’s a lot out there…)

Collapse
 
araslanove profile image
Araslanov Eugene

This also works in git

  git checkout -
Thread Thread
 
luanfonceca profile image
Luan Fonseca

A lot of git commands works with the -

Like:

(my-branch*) $ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.

(develop*) $ git checkout my-branch
Switched to branch 'my-branch'

(my-branch*) $ git merge -
Already up-to-date.
Collapse
 
sreenihari profile image
Sreeni

CTRL - R is best to scroll through history

Collapse
 
ptim profile image
Tim Osborn

Follow mode for less! less +F

Collapse
 
matheusam profile image
the-harry

Great tips! I think it worth to mention the !$ operator in addition to !!.
Where !$ is the argument of last command.
So if you:

mkdir foo

You can enter the folder typing:

cd !$

Collapse
 
patryktech profile image
Patryk • Edited

alt + . is a great one too, in bash.

$ mkdir -p /tmp/some/nested/directory
$ cd <alt+.> # types the LAST argument - /tmp/some/nested/directory

Re: netstat, it still works, of course, and you can continue using it, but it is deprecated in favour of ss.

Re: sshing into a DB server as root, no offence, but I don't believe your company actually has a security policy :D (hopefully it was just an example).

Collapse
 
gtobar profile image
Guillermo Tobar

For years I looked for this, and finally found it. Thank you very much for your contribution Patryk.

Collapse
 
mateuszjarzyna profile image
Mateusz Jarzyna • Edited

Re: sshing into a DB server as root, no offence, but I don't believe your company actually has a security policy :D (hopefully it was just an example).

Yup, it was a 'funny' example

Collapse
 
vladimirnikolic profile image
Vladimir Nikolic

Your title is misleading kind of.
Says commands you use in daily job, implies you are using them every day? Or that you use those command in your daily job as developer. If second whats your night job?
I dont see any command i would run daily.
Sorry to say but i dont see any of these commands so useful for daily work.
Hard to believe you would be executing ssh -L{port on your PC}:localhost:{database's port} root@{server IP} daily (or almost) instead of creating an alias, same as for the other longer command you mention.
Or this:
0 22 * * 1-5 /opt/scripts/send-report.sh 2>/var/log/scripts/report-error.log
Really? Almost daily?
Cant take this seriously :)