MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Linux: Most Frequently Used Shell Commands

Xah Lee,

This is a list of most frequently used unix/linux commands.

I've been a sys admin on Solaris and various unixes (HP-UX, Linux, BSDs, …) from 1997 to 2003. Then, recently started to explore linux again. Here's the list from my experience. The code here are based on Ubuntu Linux, but 99% of them work in any unix. (except a few that are specific to linux or distro. ⁖ {apt-get, shutdown, pstree, adduser})

File/Directory Manipulation

codepurpose
lslist files in current directory
ls -allist all files in current dir, including dot files
ls -al | grep ‹string›show file name matching ‹string›
cd ‹dirpath›change directory
cdgo to $HOME dir
pwdshow the current dir
codepurpose
touch ‹filename›create a new file (or updating timestamp of a existing file)
rm ‹filename›delete a file
rm -r ‹dirname›delete a dir
cp ‹filename› ‹new filename›copy a file
cp -r ‹dirname› ‹new name›copy a dir
mkdir ‹new dir name›create a new dir
rmdir ‹dirname›delete a dir only if it is empty
mv ‹filename› ‹new name›rename file, or move to a diff dir.
du -sh ‹dirname›show dir size. 〔☛ Linux: Directory Size: du Command

For these, i usually use emacs dired. 〔☛ Emacs dired tutorial〕 〔☛ Emacs Shell Tutorial

Viewing Files

codepurpose
cat ‹filename›view a file
cat ‹filename› | moreview a file by page. Type 【q】 to exit. Type 【h】 for other keys. more can also be less; the latter is better.
vi ‹filename›view a file. Type 【Esc : q】 to exit.
head ‹filename›view the first few lines of a (big) file. (to get a idea what the heck the file contains)
tail ‹filename›view the last few lines of a file.
tail -f ‹filename›view the last few lines of a growing file, updated continuously. Typically used on log files.
file ‹filename›report what type of file it is.

See also: Emergency vi (vi tutorial)Xah Emacs Tutorial.

Locating Commands

codepurpose
type ‹cmd name›show if ‹cmd name› is a shell built-in or standalone program. ⁖ type kill. “type” is a bash built-in.
which ‹cmd name›show full path of a command, useful for checking if a program is installed, or if it's in the search path in $PATH environment variable.
man ‹cmd name›view documentation of a command. 【q】 to exit. 【h】 for help.
apropos ‹string›search man pages.
locate ‹name›find a file by name (using the database see man updatedb). This is similar to find ‹many dir paths› -name "*‹search string›*" but much faster.
updatedbupdate the database used by locate

unix find executables by searching the $PATH environment variable. Try echo $PATH. It is a list of dir paths. They are searched in order. Excutable files must have executable bits on. That is, the “x”. For example, try ls -l /usr/bin.

Install Programs/Debian-Package

codepurpose
apt-cache search ‹name›find package ‹name› for install by “apt-get”
apt-cache show ‹name›describe package ‹name›
apt-get install ‹name›install a new program. (usually used with sudo in front)
apt-get remove ‹name›remove (un-install) a program.
apt-get purge ‹name›remove a program and its config files.
dpkg -llist all installed packages

On Redhat based linuxes (⁖ Fedora, CentOS), the command to install apps is “yum”. See man yum.

Typical install from source code:

gzip -d ‹filename.tar.gz›
tar xvf ‹filename.tar›

cd ‹dirname›
./configure
make
sudo make install # optional. This basically copy the binary to /usr/local/bin

Example: Building Emacs on Linux — a Guide.

{uname, df}, see: Linux: How to Find {kernel version, Distro, CPU, RAM, Disk Size} Info

Archive, Compression {tar, gzip}

codepurpose
tar cvf ‹new name.tar› ‹dirpath›archive a folder.
tar xvf ‹filename.tar›unarchive.
gzip ‹filename›To compress a file.
gzip -d ‹filename›Decompress a file.

See: Linux Compression How-to: tar gzip bzip2 xz 7zip rar zip.

Text Processing

See: Unix Shell Text Processing Tutorial: grep, cat, awk, sort, uniq, find, xargs, …

Fetching Remote Files

See: Linux Tutorial: rsync, unison, wget, curlHow to Use Unison for Syncing Files

Managing Process

codepurpose
ps -efview running processes
ps -ef | grep ‹name›find a particular process
kill ‹pid›quit a program that has process id ‹pid›
kill -s 9 ‹pid›force quit a process
topmonitor processes with continuous update. q to quit.
pstreeshow the process parent-child relationship

See: Linux: Monitor Processes, “top” Tutorial.

Job Control

see: Linux: Job Control Tutorial

Sys Admin

codepurpose
sudo ‹command string›run a command as “root” (“root” is the name of default admin account.)
suswitch to “root”
sudo su rootswitch to “root”. (useful when root isn't setup as a login account. ⁖ default Ubuntu)
codepurpose
useradd ‹user name›create a new user account. (On Debian based linuxes, there's higher-level “adduser” written in perl.)
passwd ‹user name›change password for user.
id ‹user name›show the id number of a user, and all groups he belongs to.
cat /ect/passwdlist all users
getent grouplist all groups. See getent --help

See also: Linux: users and groups Admin.

codepurpose
chmod 664 ‹filename›change the perm bits. (664 = rw-rw-r--; typical text file perm bits)
chown ‹user name› ‹filename›change owner of a file.
chgrp ‹group name› ‹filename›change the group of a file.
ln -s ‹new name› ‹filename›make a symbolic link of a file. (symbolic is file that contains the path of another file.)
ln ‹new name› ‹filename›create hard link of a file. (Hard link makes 2 files pointing to the same index in the file system (hard disk).)
shutdown -r 0restart machine now. (power off is -P)
codepurpose
dateshow current date and time
date --rfc-3339=secondsshow time stamp in this format: “yyyy-mm-dd hh:mm:ss-07:00” the last are time offset to UTC.
wshow who is logged in.
who -alist all users that have logged in recently.
uptimeshow how long the system's been running.
wccount the number of chars, words, lines. useful with cat, grep
codepurpose
source ‹shell file›execute a shell file ‹shell file›. source ‹shell file› is equivalent to . ‹shell file›
bashstart a new bash. 【Ctrl+d】 to exit when done.
echo $PATHView value of a environment variable.
envshow all environment variables
alias ‹str›="‹cmd›";make ‹str› as shortcut for ‹cmd›. ⁖ alias l="ls -al --color"

Image Processing

Version Control

Generic Useful Bash Syntax

codepurpose
‹cmd› *.txtA asterisk “*” means any character. *.txt means all files ending in “.txt”. Can be used for any command that takes list of files or dir. See man 7 glob.
‹cmd1› | ‹cmd2›pass the output of ‹cmd1› to the input of ‹cmd2›
cat ‹filename› | ‹cmd›feed the content of ‹filename› to the input of ‹cmd›
‹cmd› > ‹filename›write the output to file
‹cmd› >> ‹filename›append output to file
cat ‹filename1› ‹filename2› > ‹new filename›join contents of ‹filename1› ‹filename2› to ‹new filename›
‹cmd1›; ‹cmd2›; …run several commands.
‹cmd1› && ‹cmd2›run ‹cmd1›, if success, then run ‹cmd2› (otherwise stop.) (the && is a logical “and” operator. Unix commands returns 0 if success, else a integer error code.)
… `‹cmd›` …generate the output of ‹cmd› and use it in your whole command. ⁖ ls -l `which more`
… &run a command in background.

Bash Keys

Linux: Bash/Terminal Keybinding List

Linux {Desktop, Window Manager} Overview

Intro to Linux Window Manager & Desktop Environment (Gnome, KDE, Xfce, xmonad, …)

blog comments powered by Disqus