Dates and Times
Date Displays date
Cal Displays Calendar
Manual man - Manual
Basic File Operations
ls List files in a directory.
The ls command (pronounced as it is spelled, ell ess) lists
attributes of files and directories. You can list files in the
current directory:
ls -a hidden
cp Copy a file.
The cp command normally copies a file:
cp myfile anotherfile
cp myfile myfile2 myfile3 mydir
mv Rename (“move”) a file.
The mv (move) command can rename a file:
mv somefile yetanotherfile
mv myfile myfile2 dir1 dir2 destination_directory
rm Delete (“remove”) a file.
The rm (remove) command can delete files:
rm deleteme deleteme2
or recursively delete directories:
rm -r dir1 dir2
List
ln Create links (alternative names) to a file.
A link is a reference to another file, created by the ln
command. Intuitively, links give the same file multiple
names,allowing it to live in two (or more) locations at once.
ln -s /home/amit/cyberfrat/conul /bin/consul
Directory Operations
cd - Change your current directory (i.e., “where you are now” in
the filesystem).
pwd - Print the name of your current directory.
basename - Print the final part of a file path.
mkdir - Create (make) a directory.
rmdir - Delete (remove) an empty directory.
rm -r - Delete a nonempty directory and its contents.
File Viewing
cat - View files in their entirety.
less - View text files one page at a time.
nl - View text files with their lines numbered.
Head - View the first lines of a text file.
tail - View the last lines of a text file.
File Creation and Editing
nano A simple text editor included by default in popular Linux distros.
emacs Text editor from Free Software Foundation.
vim Text editor, extension of Unix vi.
Creating a File Quickly
You can quickly create an empty file (for later editing) using the
touch command:
touch newfile
File Properties
stat Display attributes of files and directories.
wc Count bytes, words, and lines in a file.
du Measure disk usage of files and directories.
file Identify (guess) the type of a file.
touch Change timestamps of files and directories.
chown Change owner of files and directories.
Chmod The chmod (change mode) command protects files and directories
from unauthorized users on the same system
File Text Manipulation
grep "keyword" filename
cut
The cut command extracts columns of text from files. A “column”
is defined by character offsets (e.g., the nineteenth character of
each line):
cut -c5 myfile
cat data.csv
one,two,three,four,five,six,seven
ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN
1,2,3,4,5,6,7
→ cut -f5 -d, data.csv
five
FIVE
5
tr
tr [options] charset1 [charset2]
The tr command performs some simple, useful translations of
one set of characters into another. For example, to capitalize
everything in a file:
→ cat somefile
amit
cyberfrat
ubuntu
debian
redhat
→ cat somefile | tr 'a-z' 'A-Z'
AMIT
CYBERFRAT
UBUNTU
DEBIAN
REDHAT
File Compression and Packaging
tar
The tar program packs many files and directories into a single
file for easy transport, optionally compressed. (It was originally
for backing up files onto a tape drive; its name is short for “tape
archive.”) Tar files are the most common file-packaging format
for Linux.
→ tar -czf cyberfrat.tar.gz cyberfrat ( to Create)
→ ls -lG myarchive.tar.gz
-rw-r--r-- 1 amit 350 Jun 20 12:49 cyberfrat.tar.gz
→ tar -tf cyberfrat.tar.gz (to l)ist contents
cyberfrat/
cyberfrat/amit
cyberfrat/cyberfrat.txt
cyberfrat/two/
cyberfrat/three/
cyberfrat/nameoffile
cyberfrat/one/
cyberfrat/cyber.csv
...
→ tar -xf cyberfrat.tar.gz (to Extract)
Zip
Zip [options] [files]
zip and unzip compress and uncompress files in Windows Zip
format. Compressed files have the extension .zip. Unlike most
other Linux compression commands, zip does not delete the
original files.
zip cyberfrat.zip cyberfrat ... (Pack.)
zip -r cyberfrat.zip cyberfrat (Pack recursively.)
unzip -l cyberfrat.zip (List contents.)
unzip cyberfrat.zip (Unpack.)
Viewing Processes
A process is a unit of work on a Linux system. Each program you run represents one or more
processes, and Linux provides commands for viewing and manipulating them. Every process
is identified by a numeric process ID, or PID.
ps - List process.
To view your processes:
→ ps -ux
all of user “amit’s” processes:
→ ps -U amit
all occurrences of a program:
→ ps -C apache2
all processes with command lines truncated to screen width:
→ ps -ef
Uptime - View the system load.
The uptime command tells you how long the
system has been running since the last boot:
top - Monitor resource-intensive processes interactively.
The top command lets you monitor the most active
processes,updating the display at regular intervals (say, every
second).
free - The free command displays memory usage in kilobytes
free -h - human readable
Controlling Processes
kill Terminate a process (or send it a signal).
The kill command sends a signal to a process. This can termi-
nate a process (the default action), interrupt it, suspend it,
crash it, and so on. You must own the process, or be the super-
user, to affect it.
sudo kill 9464 9465 9466
ps -uax | grep apache2
pidof apache2
iimeout -
The timeout command sets a time limit for running another timeout Kill a command that runs for
too long.program, in seconds.
sleep 60
timeout 3 sleep 60
As a more practical example, play music from your MP3 collection for an hour, then stop:
→ timeout 3600 mplayer *.mp3
Scheduling Jobs
sleep Wait a set number of seconds, doing nothing.
The sleep command simply waits a set amount of time.
sleep 5m - Do nothing for 5 minutes
sleep is useful for delaying a command for a set amount of
time:
→ sleep 10 && echo 'Ten seconds have passed.
watch - Run a program at set intervals.
The watch program executes a given command at regular inter-
vals; the default is every two seconds.
watch -n 60 date - watch date command every 60 secs
crontab
The crontab command, like the at command, schedules jobs
for specific times. However, crontab is for recurring jobs, such
as “Run this command at midnight on the second Tuesday of
each month.”
crontab -e - Edit your crontab file in your default editor ($VISUAL).
crontab -l - Print your crontab file on standard output.
Here are some example time specifications:
* * * * * date >> /home/amit/cyberfrat/amit
45 * * * * 45 minutes after each hour (1:45, 2:45, etc.)
45 9 * * * date >> /home/amit/cyberfrat/amit
Every day at 9:45 am
45 9 8 * * date >> /home/amit/cyberfrat/amit The eighth day of every month at 9:45 am
45 9 8 12 * date >> /home/amit/cyberfrat/amit Every December 8 at 9:45 am
45 9 8 dec * date >> /home/amit/cyberfrat/amit Every December 8 at 9:45 am
45 9 * * 6 date >> /home/amit/cyberfrat/amit Every Saturday at 9:45 am
45 9 * * date >> sat /home/amit/cyberfrat/amit Every Saturday at 9:45 am
45 9 * date >> 12 6 /home/amit/cyberfrat/amit Every Saturday in December, at
9:45 am
45 9 8 12 6 date >> /home/amit/cyberfrat/amit Every Saturday in December, plus
December 8,at 9:45 am
Users and Their Environment
logname - Print your login name.
logname
The logname command prints your login name (it might
seem trivial, but it’s useful in shell scripts):
whoami - Print your current, effective username.
The whoami command prints the name of the current, effective user.
id - Print the user ID and group membership of a user.
Every user has a unique, numeric user ID, and a default group
with a unique, numeric group ID. The id command prints these
values along with their associated user and group names:
finger - Print information about users.
The finger command prints logged-in user information in a
short form:
Printenv - The printenv command prints all environment variables
known to your shell and their values:
User Account Management
Adduser - create an account
The useradd command lets the superuser create a user account:
sudo useradd balaji
Deluser - Delete a user
It does not delete the files in the user’s home directory unless
you supply the -r option. Think carefully before deleting a
user; consider deactivating the account instead (with usermod-L).
deluser balaji
usermod - The usermod command modifies the given user’s account in
various ways, such as changing a home directory:
sudo usermod -d /home/balajik balaji
Useful options
-d dir Change the user’s home directory to dir.
-l username Change the user’s login name to username.
-s shell Change the user’s login shell to shell.
-g group Change the user’s initial (default) group to
Group, which can either be a numeric group ID or
a group name, and which must already exist.
-L Disable a user
-U Unlock the user
passwd - The passwd command changes a login password,
chfn -
The chfn (change finger) command updates a few pieces of
personal information maintained by the system: real name,
home telephone, office telephone, and office location, as dis-
played by the finger command.
Group Management
groups - Print the group membership of a user.
sudo groupadd cyber - Create a group.
The groupadd command creates a group.
sudo groupdel cyber - deletes a group The groupdel command deletes an
existing group
Host Information
uname Print basic system information.
The uname command prints fundamental information about
your computer:
This includes the kernel name (Linux),
Hostname (server.example.com),
kernel release (4.2.0-17-generic),
Kernel version (#21-Ubuntu SMP Fri Oct 23 19:56:16 UTC 2015),
hardware name (x86_64),
operating system name (GNU/Linux).
hostname - The hostname command prints the name of your computer.
ip Set and display network interface information.
The ip command displays and sets various aspects of your
computer’s network interface.
ip addr show wlp1s0
ip addr show
ifconfig Older command to set and display network interface
information.
The ifconfig command is an ancestor of ip. It is still found on
many Linux systems but is less powerful
ifconfig wlp1s0
Host Location
host Look up hostnames, IP addresses, and DNS info.
host www.ubuntu.org
ping Check if a remote host is reachable.
The ping command tells you if a remote host is reachable. It
sends small packets (ICMP packets to be precise) to a remote
host and waits for responses:
ping google.com
traceroute View the network path to a remote host.
The traceroute command prints the network path from your
local host to a remote host, and the time it takes for packets to
traverse the path:
Lynx - text browser
Lynx www.google.com
wget - The wget command hits a URL and downloads the data to a file
or standard output. It’s great for capturing individual web
pages, downloading files,
Echo “ hello world”
Installing Softwares
sudo apt update
sudo apt install htop -y - Installs htop
sudo apt remove htop -y - removes htop