Pages

Command Line in Linux

Yes as you may heard of it, Linux would be operated with commands like Dos commands in Windows. This command also would be called Shell scripting as well. Those Linux commands would work under Bash scripting like batch files in Windows as well. All of my description will walk you through with Ubuntu. 

Essential and convenient shortcut keys


Before looking into commands, these shortcut keys could make your life easy. 

Ctrl+Alt+T

Launch Terminal on Unity.  

Tab

In a terminal, it would completed automatically if there is only one option, otherwise all available options. It would be really great if we would have to type a long file name.

MiddleMouseButton

It would work with "copy and paste at the same time, if a text is highlighted somewhere else on Terminal. 

ArrowUp
ArrowDown
Scroll and edit the command history, which would be executed being followed by <Enter>.

Shift+PageUp
Scroll page up on Terminal.

Shift+PageDown
Scroll page down on Terminal. 

Ctrl+s
Stop any transferring to the Terminal. 

Ctrl+q

Resume transferring to the Terminal. It would be worth trying if Terminal stops responding strange way.

Ctrl+c

Kill the current process. 

Ctrl+z

Send the current process to the background. On the Terminal perspective, it may look like working same as Ctrl+c.

Ctrl+Alt+Del

Show shut down or reboot system options to users. 

Command Line Syntax


In Linux we should use Upper and Lower case precisely because it is sensitive on it. Even searching a certain file on Linux, it should be taken care of.

All commands should be used under one of the following two rules below since OBJECT should be required necessarily or optionally. 

  • command [options] [arguments]
  • command [options] [arguments] OBJECT
Some commands may have either [options] or [arguments], or both of them depending on cases. But mostly commands should work without [options] or [arguments]. And commands could be composed of several words. 

Sanity Expressions


Before jumping into command lines, learning essential expressions would help us to use commands fluently. 

/
Root or Slash. If it is used alone, it would mean "/" directory, which is called Root. If it is done with a directory or several other directories including several /, it would mean one of directory path. 

~
Tilde. It means your home directory. With this, you can easily move down to one of sub-directory under home directory. 

.
Dot. It means the current directory where you are. With this, you can easily move down to one of sub-directory under the current one. 

..
Two dots. It means a parent directory above where you are now. You can simply move up with this. 


|
Pipe. Send output of left-side command execution to right-side as input of the right-side command. It can be used multiple times consequently. 
Note : || would work as OR operator.


&
Ampersand. If it is used any command, the command would run in background. Eg) COMMAND&
Note : && would work as AND operator.

;
Semi colon. Connects several commands sequentially. Unlike |, there is no certain relations among the used commands in-between.

\
Back slash. It would work like concatenation operator. When a space or special character is required, they can be used after \.

PROGRAM
Launch a certain program from Terminal with program name itself. You can just simply type the program on Terminal.

killall PROGRAM
Kill program(s) by name. 

man [Option] COMMAND
Stands for Manual, which requires any COMMAND as an object. 

COMMAND --help
Shows us usage of the command.

Common commands including system information


pwd
Stands for "Print Working Directory" and print the current working path in full name.

cd DIRECTORY
Stands for "Change Directory" and requires a full path as an object.

ls [Option] [File]
Stands for "LiSt" and shows you files in the current directory where you are.
-a, --all : Do not ignore entries starting with ".", which means really everything.
-A, --almost-all : Do not list implied "." and "..".
-c : List entries by columns.
-d : List directories, not their contents. 
-l : Use a long listing format.
-t : Sort by modification time, newest first. 

exit
Logout from Terminal you are using now. If there are multiple Terminals, you can move to another one once it is used. 

reset
Restore a screwed-up Terminal to default settings. 

hostname
Print the name of local host you are currently working on. 

whoami
Print the login name.

who [Option]
Print all usernames and info currently logged in.

id [Option] [USERNAME]
Print real and effective user and group IDs.

finger USERNAME
Lookup user information like login name, mail, plan, or etc. with a program, which is required to be installed if it is tried first time. 

date [Option] [Format]
Print or set the system date and time. 
-d : Display time described by the following string format. Eg) date -d 20161201
-r : Display the last modification time of File. Eg) date -r Filename
-s : Set time described by the following string format. Eg) date -d 20161211

uptime [Option]
Tell how long the system has been running after booted. 

last [Option] [Username] [tty]
Show listing of users last logged-in on your system.

history
Display GNU history of commands you have been used so far. 

uname
Information on your server that you log on now. 
-a : All.
-r : Kernel release.
-v : Kernel version.
-p : Processor type. 

free [Option]
Display amount of free and used memory in the system. 
-b : In Bytes.
-k : In Kilobytes, which is default. 
-m : In Megabytes.
-g : In Gigabytes.
-h : Automatically scaled to shortest three digit unit for human being. 
-t : Add a line showing the column totals.

df [Option]
Stands for "Disk Free", which will be reported. 
-a : Include all pseudo, duplicate, inaccessible file systems.
-B SIZE : Scaled by size unit. Eg) df -B MB
-h : Human readable format and units.

lsmod
Stands for "LiSt MODules in Linux Kernel". It is showed nicely from /proc/modules of your system. 

echo
Display a line of text.

shutdown [Option] [Time] [Message]
Will shut down your system. It will be done in a minute as default with no option. You may overwrite with another shutdown commands or cancel the shutdown command with -c option.  
-h : Power off the machine, which is identical to power off, -P.
-H : Halt the machine. 
-P : Power off the machine, which is identical to power off, -h.
-r : Reboot the machine.  
-c : Cancel shutdown command. 
[Time]
It can be set like following examples. Eg) shutdown 17:20 (at 17:20), shutdown +5 (in 5 minutes), shutdown (in 5 minutes)shutdown now
[Message] 
This message would be shown just right before shutting down, which requires [Time] all the time. 

Basic operations


cp [Option] SOURCE DEST 
cp [Option] SOURCE DIRECTORY
cp [Option] -t DIRECTORY SOURCE
Stands for "CoPy", which requires at least SOURCE and others as usage. SOURCE can be multiple files or a directory itself. 
-i : Prompt before overwrite.
-n : No overwrite existing files.
-r, -R : Copy directories recursively.

mv [Option] SOURCE DEST
mv [Option] SOURCE DIRECTORY
mv [Option] -t DIRECTORY SOURCE
Stands for "MoVe" files to somewhere.
-f : Do not prompt before overwrite.
-i : Prompt before overwrite.
-n : No overwrite existing files.

rm [files]
rm [Option] [FILE]
rm [-r] [files]
Stands for "ReMove". 
-f : Ignore nonexistent files with no prompt. 
-i : Prompt before removal.
-r, -R : Remove directories and their contents recursively.

mcopy [Option] SOURCEFILE TARGETFILE
mcopy [Option] SOURCEFILE [SOURCEFILES...] TARGETDIRECTORY
Copy MSDOS files from/to Unix.

mkdir [Option] DIRECTORY
Stands for "MaKe a new DIRectory".
-m : MODE as permission mode, rwx rwx rwx. 
-p : No error if existing. Make parent directories as needed.

rmdir [Option] DIRECTORY
Stands for "ReMove a DIRectory". 
-p : Remove directory and its ancestor. Eg) "rmdir -p a/b/c" would equal to "rmdir a a/b a/b/c"

File manipulation


cat [Option] [FILE]
"ConCATenate" files and print on the standard output. Eg) cat myfile.txt

more [Option] FILE
COMMAND | more [Option]
Display output on a screen at a time.
-p : Instead of doing not scroll, clear the whole screen and display text.
-c : Instead of doing not scroll, paint each screen from the top, clearing the remainder of each line as it is displayed. 
+NUM : Start at line number NUM.

less [Option]
COMMAND | less [Option]
Display output on a screen at a time.
-e : Quit at EndOfFile.
-n : Use line numbers.
-x : Set tab stops.

touch [Option] FILE
Make files or update the access and modification times of each file to the current time.
-a TIME : Change only the access time. 
-d DATE : Change DATE including Month, Day, and Time.

find [Option] [Path] [Expression]
Search for files or Expression in a directory hierarchy, Path. Default path is the current directory.
-print : Print the full file name on the standard output.
-name : File name.
-size : File size units of space, rounding up.

grep [Options] STRING [File]
Search for STRING in each File. STRING is a basic regular expression (BRE) as default and File can be defined. 
-A NUM : Print NUM lines of trailing context after matching lines. 
-a TEXT : Process a binary file as it is were text.
-B NUM : Print NUM lines of leading context before matching lines. 
-C NUM : Print NUM lines of output context. 
-c COUNT : Suppress normal output.
-i : Ignore case distinctions in both STRING and Files. 
-n NUM : Prefix each line of output with the line number within its input file. 
-R, -r : Recursive.

awk [Option] 'PATTERN' FILE
Stands for "Aho (Hello), Weinberger and Kernighan". It is not a simple command but an interpreter for the AWK Programming Language, which is useful in finding and replacing text within file(s). PATTERN can be expressed as a simple text or combination with operators. Eg) awk 'NR % 2 == 0' data

Process Control


ps [Option]
Stands for "Process Status", which report the processes currently run by the current user. 
-A : Select all processes, which is identical to -e.
-a : Select all processes except both session leaders and non-associated processes to a terminal. 
-d : Select all processes except session leaders.
-e : Select all processes, which is identical to -A.
-f : Do with full list.
PID : List up process ID, PID.

top 
Keep listing the currently running processes, stored by CPU usage, which look like the one in Task Manager of Windows.

fg PID
Stands for 'ForeGround'. Stop process to the foreground or bring a background.

bg PID
Stands for 'BackGround'. Send the process to the background. 
Note : COMMAND& would run COMMAND in the background. 

batch COMMAND
Run COMMAND when the system load is low. I may log out, which let the process keep running.

kill PID
Force a process shutdown. PID can be got using ps command. 

Network applications


ifconfig [Options] [INTERFACE]
ifconfig [Options] INTERFACE ADDRESS
Configure a network interface. INTERFACE should be the name of the interface, for example eth0. If flag indicates up, which is activated otherwise, it says down.
-a : Display all.
-s : Display a short list. 
mtu N : Set MTU of an interface. 
dstaddr addr : Set a remote IP address.
netmask addr : Set the IP network mask for this interface. 
address : IP address to be assigned to the interface. 

ethtool DEVNAME
Query or control network driver and hardware settings. Eg) ethtool eth0

ip [Options] OBJECT [COMMAND] [Options]
-h : Human readable. 
-s : Statistics mode.
-t : Display time stamp. 
OBJECT
address : IPv4 or IPv6 address on a device.
maddress : Multicast address.
monitor : Watch for netlink messages.
route : routing table entry.
Eg) "ip addr show", "ip addr add 1.2.3.4/24 brd + dev eth0", "iroute show", "ip route add default via 1.2.3.254"

ftp [HOST]
Execute FTP access to the HOST, which can be IP address or host name. 

nslookup [Option] [HOST]
A program to query the Internet name servers interactively.

ping [HOST]
Send ping commands several times to the HOST, which can be a host name or an IP address. 

route
route add TARGET
route del TARGET
Show or manipulate the IP routing table. 

netstat

Print network connections, routing tables, interface statistics. 


Administration


su 
Stands for "Super Users". Switch user ID to root privilege user, which is called superuser. Since no password is created for root user as default in Ubuntu, it is not allowed to be accessed without making a password for the root user. 
It sounds like similar to sudo command but su -c would be identical to sudo command. 
-c COMMAND : Execute COMMAND as root user.

sudo
Stands for "Super Users DO". Execute commands as root privilege user or superuser. Since root privilege was given to default user for example, 'taehee' on my system, the password for user 'taehee' user would be required. Once you have a message starting with "Only root may..",  sudo should be front of other executable command. 
It might be confusing but sudo -i would be identical to su command. 
Here is great posting simply explaining differences between su and sudo at  
www.linux.com/blog/how-use-sudo-and-su-commands-linux-introduction.
-i : Run the shell specified by the target user's password.

adduser [Options] USER
adduser SYSTEM [Options] USER
adduser [Options] USER GROUP
addgroup [Options] GROUP
addgroup SYSTEM [Options] GROUP
Add user or group to certain group.
-q : Do not give process information to stdout.
-c FILE : Use FILE as configuration file.

passwd [Options] [USER]
Change user password. Once it is sent, the system will ask you a new password.

chmod [Option] MODE FILE
chmod [Option] FILE
Change a current permission mode of each FILE to another permission MODE; read (r), write (w), and execute (x). And these three are repeated per user, group, and all (world), which means total 9. Then it could be like rwxrwxrwx, or rwxr--r--. 
MODE 
It can be added with +, removed with -, or set with =. Eg) chmod a-x file, chomod u+x testfile, chmod =rwx, g+s file
-R : Recursive.

fdisk [Options] DEVICE
Manipulate disk partition table of the DEVICE.

mount
mount -a [Options]
mount [Options] DEVICE|DIR
Mount a file system.

umount DEVICE
umount -a [Options]
Unmount file systems. 
-a : All of file systems.
-f : Force an unmount. 

tar [Option] [FILE]
Tar archiving utility. Tape archive (compress or uncompressed depending on options)
-A FILE : Append tar files to an archive.
-c FILE : Create a new archive.
-d : Find differences between archive and file system.
-r : Append files to the end of an archive. 
-t : List the contents of an archive. 
-x : Extract files from an archive.

zip [FILE]
Package and compress archive files. 
-A : Adjust self-extracting .exe files. 
-d : Delete entries in zip file.
-m : Move into zip file from OS files. 
-r : Recurse into directories.
-0 : Store only.
-q : Quiet operation without showing progress.

unzip [Options] FILE
Unzip FILE.