Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice.
The Linux Command Line, William E. Shotts
Since I switched to Ruby on Rails from .NET, I find myself increasingly using the terminal every day. One thing I've realized as a Rails developer is that you need to have some basic competence with the terminal, as you will use it all the time. So I spent some time getting familiar with some basic Linux commands, and this post tries to summarize the essentials.
This is not a comprehensive list, but I will try to keep adding to this list on the original blog post as I learn more. If you want a detailed overview of Linux operating system, I highly recommend The Linux Command Line, 2nd Edition by William E. Shotts.
So here are some of the commands that you might find useful as a developer on a Linux/Mac machine.
cat displays the contents of a file, or concatenates the contents of multiple files.
touch creates a file if it doesn't exist; updates the timestamp if it exists.
grep searches the term
client in the provided file.
-ifor case-insensitive search
-nfor printing line numbers next to the results.
grep client -in /etc/ssh/sshd_config
less displays a large file one page at a time. Use the space bar to go forward and
b to go back.
file tells the format of a file.
head/tail displays the top or bottom of the file. Pass
-n for number of lines,
head -5 file
reset re-initializes terminal. Especially useful after resizing the window or if a command results in scrambled window.
To send the output of a command to a file, use
>, which overwrites the existing content of the file. To append, use
ls > file_name
To send the output of a command to the standard input of another command, use the pipe
head /proc/cpuinfo | tr a-z
sort sorts lines of text.
uniq Report or omit repeated lines
grep Print lines matching a pattern
wc Print newline, word, and byte counts for each file
head Output the first part of a file
tail Output the last part of a file
tee Read from standard input and write to standard output and files
A process is a running program. Each process has a process ID (PID). Sometimes a computer will become sluggish or an application will stop responding.
Here are some of the tools available at the command line that let us examine what programs are doing.
ps lists all the running processes.
ps xShow all of your running processes.
ps axShow all processes on the system, not just the ones you own.
ps uInclude more detailed information on processes.
ps wShow full command names, not just what fits on one line.
To check on a specific process, add the PID at the end of the
ps command, e.g.
ps u 1234
top displays tasks
jobs lists active jobs
bg places a job in the background
fg places a job in the foreground
kill sends a signal to a process
killall kills processes by name
shutdown shuts down or reboots the system
Normally, after you run a command, you don't get the prompt back until the process finishes. You can detach a process from the shell with the
& which runs it as a background process. The shell returns immediately with the PID of the process.
If you want to keep a program running when you log out from a remote machine, use the nohup command.
Determine if a user can read, write, or run the file. View the permissions using
ls -l command.
-rw-rw-r-- 1 ak ak 14 Oct 5 07:00 file_one
- indicates a file,
d indicates a directory.
rwx stands for read, write, and execute. From left to right, the permissions stand for a user, group, and other.
To modify the permissions, use the
chmod command, e.g.
chmod 644 file.
|644||user: r/w; group, other: read||files|
|600||user: read/write; group, other: none||files|
|755||user: read/write/execute; group, other: read/execute||dirs, programs|
|700||user: read/write/execute; group, other: none||dirs, programs|
|711||user: read/write/execute; group, other: execute||dirs|
id displays user identity
chmod changes a file’s mode
su runs a shell as another user
sudo executes a command as another user
chown changes a file’s owner
chgrp changes a file’s group ownership
passwd changes a user’s password
The Linux shell maintains information about the current environment. Programs use this data to change their runtime behavior, e.g. selecting different database when the application is running in the production environment, as opposed to the test environment.
printenv prints part or all of the environment
set sets shell options (show the environment when used without argument)
export exports environment to subsequently executed programs
alias creates an alias for a command (or show all aliases when used without argument)
Set environment variable
NAME=akshay export NAME echo $NAME
This sets it locally. For setting it globally, add it in the
Hope that helps.