Useful Linux Commands – DEV Community

Studying the Linux command line, like turning into an achieved pianist, isn’t one thing that we decide up in a day. It takes years of follow.

The Linux Command Line, William E. Shotts

Since I switched to Ruby on Rails from .NET, I discover myself more and more utilizing the terminal day-after-day. One factor I’ve realized as a Rails developer is that that you must have some fundamental competence with the terminal, as you’ll use it on a regular basis. So I spent a while getting aware of some fundamental Linux instructions, and this put up tries to summarize the necessities.

This isn’t a complete checklist, however I’ll attempt to hold including to this checklist on the original blog post as I study extra. If you need an in depth overview of Linux working system, I extremely suggest The Linux Command Line, 2nd Edition by William E. Shotts.

So listed below are a few of the instructions that you just would possibly discover helpful as a developer on a Linux/Mac machine.



File system

cat shows the contents of a file, or concatenates the contents of a number of information.

contact creates a file if it would not exist; updates the timestamp if it exists.

grep searches the time period consumer within the supplied file.

  • -i for case-insensitive search
  • -n for printing line numbers subsequent to the outcomes.
grep consumer -in /and so forth/ssh/sshd_config
Enter fullscreen mode

Exit fullscreen mode

much less shows a big file one web page at a time. Use the area bar to go ahead and b to return.

file tells the format of a file.

head/tail shows the highest or backside of the file. Move -n for variety of traces, head -5 file

reset re-initializes terminal. Particularly helpful after resizing the window or if a command leads to scrambled window.



I/O Redirection

To ship the output of a command to a file, use >, which overwrites the present content material of the file. To append, use >>.

ls > file_name
Enter fullscreen mode

Exit fullscreen mode

To ship the output of a command to the usual enter of one other command, use the pipe | character.

head /proc/cpuinfo | tr a-z
Enter fullscreen mode

Exit fullscreen mode

type types traces of textual content.

uniq Report or omit repeated traces

grep Print traces matching a sample

wc Print newline, phrase, and byte counts for every file

head Output the primary a part of a file

tail Output the final a part of a file

tee Learn from normal enter and write to plain output and information



Processes

A course of is a operating program. Every course of has a course of ID (PID). Generally a pc will grow to be sluggish or an utility will cease responding.

Listed here are a few of the instruments out there on the command line that allow us study what applications are doing.

ps lists all of the operating processes.

  1. ps x Present your entire operating processes.
  2. ps ax Present all processes on the system, not simply those you personal.
  3. ps u Embrace extra detailed data on processes.
  4. ps w Present full command names, not simply what suits on one line.

To test on a particular course of, add the PID on the finish of the ps command, e.g. ps u 1234

prime shows duties

jobs lists lively jobs

bg locations a job within the background

fg locations a job within the foreground

kill sends a sign to a course of

killall kills processes by title

shutdown shuts down or reboots the system

Background Course of

Usually, after you run a command, you aren’t getting the immediate again till the method finishes. You may detach a course of from the shell with the & which runs it as a background course of. The shell returns instantly with the PID of the method.

If you wish to hold a program operating whenever you log off from a distant machine, use the nohup command.



File Modes & Permissions

Decide if a person can learn, write, or run the file. View the permissions utilizing ls -l command.

-rw-rw-r-- 1 ak ak  14 Oct  5 07:00 file_one
Enter fullscreen mode

Exit fullscreen mode

Leftmost character: - signifies a file, d signifies a listing.

rwx stands for learn, write, and execute. From left to proper, the permissions stand for a person, group, and different.

To switch the permissions, use the chmod command, e.g. chmod 644 file.

Mode That means Used For
644 person: r/w; group, different: learn information
600 person: learn/write; group, different: none information
755 person: learn/write/execute; group, different: learn/execute dirs, applications
700 person: learn/write/execute; group, different: none dirs, applications
711 person: learn/write/execute; group, different: execute dirs



Safety

id shows person id

chmod adjustments a file’s mode

su runs a shell as one other person

sudo executes a command as one other person

chown adjustments a file’s proprietor

chgrp adjustments a file’s group possession

passwd adjustments a person’s password



Surroundings Variables

The Linux shell maintains details about the present surroundings. Packages use this information to vary their runtime habits, e.g. choosing totally different database when the applying is operating within the manufacturing surroundings, versus the check surroundings.

printenv prints half or all the surroundings

set units shell choices (present the surroundings when used with out argument)

export exports surroundings to subsequently executed applications

alias creates an alias for a command (or present all aliases when used with out argument)

Set surroundings variable

NAME=akshay
export NAME
echo $NAME
Enter fullscreen mode

Exit fullscreen mode

This units it domestically. For setting it globally, add it within the ~/.bashrc or ~/.zshconfig file.

Hope that helps.

Add a Comment

Your email address will not be published. Required fields are marked *