Basic Linux Commands - Amazon Wiki
Basic Linux Commands - Amazon Wiki
php/Basic_Linux_Commands#mv
This page provide a list of useful commands and examples of their use within the Amazon
environment for training FC Support Technicians.
Note: Remember that on most systems you can read the manual
for all the details and parameters of the command you are
interested in:
man <command>
mv
The linux command for moving a $ile, also used for renaming a $ile.
mv /<path>/<from> /<path>/<to>
Useful Options
-f force (Do not prompt when overwriting $iles)
-i interactive (Asks if you want to overwrite $iles)
-v verbose (Expand on what was action was performed)
Example
To move the $ile day.txt to the /home/Training folder you do the following:
grep
grep <word> <path>/<$ile>
Useful Options
-f <$ile> read one or more patterns from the $ile
-i case insensitive (ignore the case of the word you are searching for).
1 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Example
If we have a text $ile called mac-address.txt stored in /home containing a list of MAC addresses:
Or we can exclude Andys devices from the list we can just use the -v and -i options:
cp
The linux command to copy a $ile.
cp /<path>/<from> /<path>/<to>
Useful Options
-i interactive (Asks if you want to overwrite $iles)
-v verbose (Expand on what was action was performed)
-r recursive (Copy sub directories and $iles also)
Example
To copy a directory and all its $iles and sub directories you use the following command:
# cp -r /home/important_notes /home/copy_of_important_notes
rm
rm stands for remove and is the linux command for deleting $iles.
rm /<path>/<$ilename>
2 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Useful Options:
-i Interactive, asks you to con$irm each $ile being deleted.
Example:
# rm /home/important_notes
pwd
pwd prints the current working directory.
Example:
If you change directory (cd) to /var/log and type pwd:
# pwd
/var/log
uptime
This command tells you the current time, length of time the host has been running (days then
hours:minutes:seconds), numbers of users currently logged on, and the load averages for the past 1,
5, and 15 minutes.
Example:
# uptime
13:47:33 up 5 days, 3:09, 7 users, load average: 0.65, 0.36,
0.18
rsync
The rsync command is useful as a $ile transfer or backup tool.
Useful Options:
-r recurse into directories
-v increase verbosity
-p preserve permissions
--links Recreate the symlink on the destination
--bwlimit=KBPS limit I/O bandwidth (KBytes per second)
--rsh=ssh remote shell (ssh is preferred at its a secure shell)
Examples: If you run the following command on server1, rsync will synchronise the all the $iles on
3 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
server1 in /mnt/backup with server 2's $iles in /home/england keeping the permissions the same,
and limiting the speed to 500MBytes per second:
To perform a local backup, which only copies the $iles that have changed since the previous backup,
you can use the following:
ssh
ssh stands for secure shell. For those familiar with telnet, you can think of this in exactly the same
way, except its encrypted.
ssh <username>@<hostname>
Useful Options:
-p port (So you can connect to a port other than ssh's normal port 22)
-v Verbose (print debugging messages).
Examples:
# ssh fc-login-gla1-101.gla1.amazon.com
Password:
Last login: Tue May 27 16:48:23 2008 from 10.67.9.206
#
--------------------------------------------------------------------
----------
[amazon]
Description:
This system is for amazon.com authorized use only, and is monitored
to detect improper use and other illicit activity.
#
--------------------------------------------------------------------
----------
Rebooted at 20:15PM 08/18/07 2.4.21-37a6smp
Rebooted at 03:22AM 09/30/07 2.4.21-37a6smp
Rebooted at 00:30AM 10/07/07 2.4.21-37a6smp
SWITCH eth0: gla1-co-acc-sw101.amazon.com GigabitEthernet1/0/41
england@login-gla1-101[gb-gla1]>
4 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
rpm
The rpm command is used to install, query and remove software installed using Red Hat Package
Management on Linux distributions including SUSE, OpenSUSE, Mandriva, Fedora, CENTOS and of
course Red Hat.
Examples:
To install the package gip-1.6.1.1-1.fc7.rf.i386.rpm:
5 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
fdisk
fdisk is a partitioning tool similar to the old FDISK.EXE tool for DOS/Windows.
Useful Commands:
6 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Examples:
For a new hard disk running p command will show something like this:
7 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
8 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Now we need a partition for /home (when asked for $irst and last cylinder I just hit enter which
accepts the defaults fdisk suggests):
9 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
5169):
Using default value 5169
Remember, once you have done this you will need to write the new partition table to disk by using the
w command.
External Link:
http://tldp.org/HOWTO/Partition/fdisk_partitioning.html
mke2fs
Now that we have created our partitions using fdisk, we can now format them using mke2fs (similar
to DOS/Windows format command). mke2fs can format partitions using either ext2 or ext3
10 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
$ilesystem. (ext2 - The second extended $ile system. ext3 -The third extended $ilesystem is a journaled
$ile system based on ext2).
Example Commands
To format /dev/sda1 as a ext2 $ilesystem you would type:
# mke2fs /dev/sda1
mke2fs -j /dev/sda5
e2label
e2label is used to change the label of a ext2/ext3 $ilesystem.
Example:
To label /dev/hda5 as home we would use:
We can then check this by using the same command but dropping the label:
# e2label /dev/sda5
home
less
less allow you to view the contents of large $iles or the output of commands without them scrolling
off the screen. It allows you to move up and down the text using the cursor keys and to search for text.
less <$ilename>
Useful Commands:
q Quit
/<word> Search for the speci$ied word
n Go to Next occurrence of the searched for word
g<n> Go to line of text <n>
space Scroll forward one page (you can also use the Page Up and Page Down keys)
11 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
ls
ls lists the $iles within a directory. You can think of ls as being the same as the DOS/Windows
command dir.
ls /<path>/
Useful Options:
-l Long listing of $iles
-a Show all $iles
-h Human readable numbers, shows $iles in kb/mb instead of bytes
-n Show numeric user and group id
-r Reverse the sort order
-R List subdirectories recursively
-S Sort by size
-t Sort by time
-1 List one $ile or directory per line
# ls -la /home/england/
total 304
drwx------ 35 england england 4096 2008-06-09 09:24 .
drwxr-xr-x 7 root root 4096 2008-06-03 09:20 ..
-rw------- 1 england england 14093 2008-06-06 17:24 .bash_history
-rw-r--r-- 1 england england 18 2008-02-29 14:27 .bash_logout
-rw-r--r-- 1 england england 176 2008-02-29 14:27 .bash_profile
-rw-r--r-- 1 england england 229 2008-05-20 10:29 .bashrc
To show the same information, in a more human readable form, sorted for time we can use:
# ls -lahS /home/england
-rw------- 1 england england 14K 2008-06-06 17:24 .bash_history
-rw-r--r-- 1 england england 229 2008-05-20 10:29 .bashrc
-rw-r--r-- 1 england england 176 2008-02-29 14:27 .bash_profile
-rw-r--r-- 1 england england 18 2008-02-29 14:27 .bash_logout
scp
scp stands for secure copy and can be used to copy $iles to or from a remote server. It is a secure
transfer unlike FTP as it is using ssh encryption.
or
12 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Useful Options:
-C Enable compression.
Example:
To copy my$iles.zip which are stored in /home/england/ to my current directory use:
# scp -C [email protected]:/home/england/myfiles.zip
/mnt/backup/myfiles.zip
[email protected]'s password:
myfiles.zip
100% 7912KB 36.6KB/s 03:36
vi
vi is a very powerful text editor which can easily have a whole book dedicated to it, so forgive me if
not everything is covered here! vi has two modes, a command mode and text entry mode.
vi /<path>/<$ilename>
Starting vi will The $irst thing you probably want to do when you $irst start vi is to $igure out how to
quit it ;o)
Exiting:
:q Quit (fails if anything has changed)
:q! Quit and throw away changes
Esc Exit mode
:w Write (save) the $ile, but don't exit
:wq Write (save) and quit
You can use the cursor and page up/down keys to move around a open document in Vi. If you pre$ix
any of these with a number the command will be repeated that many times. For example, type 4 and
the down cursor and the cursor moves down 4 lines. Press 2 and page up, and you will move up 2
pages.
To start to edit a $ile you need to change to "insert mode", this is done by typing i which should cause
the "-- INSERT --" to appear at the bottom of your screen. You can type away to your hearts content.
When you are $inished, hit Esc.
Basic Editing:
5yy Yank 5 lines, (p to paste them)
:3dd Deletes 3 lines
2G Jump to 2nd line
101G Jump to 101st line
G Jump to last line
yw yank word
p put (paste) the clipboard after cursor
dd delete (cut) a line
13 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
Search/Replace:
/pattern Search for pattern
?pattern Search backward for pattern
n Repeat search in same direction
N Repeat search in opposite direction
:%s/old/new/g Replace all old with new throughout $ile
:%s/old/new/gc Replace all old with new throughout $ile with con$irmations
sed
sed is a Stream EDitor.
Example:
If we have the $ile sedinputtext.txt containing:
I love Microsoft.
Microsoft never crashes.
Linux is great.
And we run the following (the > sends the out put of the command to sedoutputtext.txt):
14 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
I love Linux.
Linux never crashes.
Linux is great.
http://sed.sourceforge.net/sed1line.txt
awk
awk is used for pattern scanning and processing.
Example:
Lets say we want to get a list of logged on users and what time they logged in, and we aren't
interested in any other information. We can $ind out who is logged in by press w and enter:
# w
12:32:46 up 204 days, 10:04, 11 users, load average: 0.05, 0.12,
0.12
USER TTY FROM LOGIN@ IDLE JCPU PCPU
WHAT
grobert 10.67.13 10.67.13.42 5:59am 0.00s 0.00s 0.04s
/bin/zsh /home/grobert/.xse
mlow 10.67.14 10.67.14.118 8:02am 0.00s 0.00s 0.08s
/bin/zsh /home/mlow/.xsessi
mcneillk 10.67.18 10.67.18.168 11:22am 0.00s 0.00s 0.04s
/bin/zsh /home/mcneillk/.xs
glenwil 10.67.15 10.67.15.250 6:06am 0.00s 0.00s 0.05s
/bin/zsh /home/glenwil/.xse
To trim this down we can 'pipe' the information from w to awk using | and then just print $ields 1 and
4 (User and Login@):
15 of 16 9/18/2017, 1:58 PM
Basic Linux Commands - Amazon Wiki https://w.amazon.com/index.php/Basic_Linux_Commands#mv
External Link:
Awk is a powerful tool and deserves further reading:
http://www.student.northpark.edu/pemente/awk/awk1line.txt
External Reference
There is also a repository at Github (https://github.com/jlevy/the-art-of-command-line) with most
commonly used commands that are operating system agnostic.
16 of 16 9/18/2017, 1:58 PM