Basic Shell Commands
Several basic shell commands are essential for managing a host with SSH.
1.ls
Lists files in a directory. Different types of files have different colors; for example, blue indicates a folder, and white indicates a file.
[root@restoretmp steph]# ls
file.html public_html test
[root@restoretmp steph]#
Parameters:
ls -a lists all files, including hidden files
[root@restoretmp steph]# ls -a
. .. .bash_logout .bash_profile .bashrc file.html public_html test
[root@restoretmp steph]#
ls -l lists owner, size, file permissions, and modification date
ls -R recursively lists the contents of all subdirectories
- cd change directory
cd /home/steph will switch to /home/steph
cd .. move up one directory
cd return to the root directory
- Create new files and folders
touch new-filename create a new file
mkdir new-directory create a new directory
4.Delete files and directories
rm filename delete the file named filename
rmdir foldername delete the directory named foldername
5.Change folder permissions
chmod 755 foldername will change the permissions of the folder named foldername to 755
6.Change user ownership
chown steph:steph file.html changes the owner of file.html to steph:steph
- Compress and extract files
gzip and gunzip
Compress:
gzip filename
The file will be compressed and saved as filename.gz
Decompress:
gunzip filename.gz
filename.gz will be deleted and replaced by filename
You can get detailed command documentation with man gip and man gunzip.
zip and unzip
To use zip to compress files, type the following command at the shell prompt:
zip -r filename.zip filesdir
In this example, filename.zip represents the file you create, and filesdir
represents the directory where you want to place the new zip file. The -r
option specifies that you want to recursively include all files contained in the filesdir
directory.
To extract the contents of a zip file, type the following command:
unzip filename.zip
You can use the zip
command to process multiple files and directories at the same time by listing them one by one, separated by spaces:
zip -r filename.zip file1 file2 file3 /usr/work/school
The above command compresses file1, file2, file3, and the contents of the /usr/work/school
directory (assuming this directory exists), and then puts them into the
filename.zip file.
Use FTP and Wget for uploading and downloading
1. Use FTP to upload files from the server to another server
[root]$ ftp /*start the FTP client
ftp> open 202.103.1.2 /*open the remote server IP
Connected to 202.103.1.2.
220 ProFTPD 1.2.9 Server ready.
Name (218.93.205.112:qqroot): user /*enter username
331 Password required for oran.
Password: /*enter password
230 User oran logged in.
put idcspy.tar.gz idcspy.tar.gz
/*This is the key part: put is the upload command. The first idcspy.tar.gz is the local filename, and the second is the remote filename. In other words, it uploads the local idcspy.tar.gz to the remote FTP and names it idcspy.tar.gz. This way, SSH can upload it by itself. You can then do other things.
- Download files with Wget
Usage:
The syntax for using wget is:
wget [parameter list] URL
First, let’s introduce the main parameters of wget:
·
-b: run wget in the background, with the log written to the “wget-log” file in the current directory;
· -t [nuber of
times]: number of attempts; when wget cannot establish a connection with the server, it will try to connect this many times
. For example, “-t
120” means try 120 times. When this option is set to “0”, it means try infinitely many times until the connection succeeds. This
setting is very useful when the other server suddenly shuts down or the network is suddenly interrupted, because it can continue
downloading unfinished files after things return to normal;
·
-c: resume from a breakpoint. This is also a very useful setting, especially when downloading relatively large files. If the transfer is
unexpectedly interrupted midway, then when the connection is restored it will continue from where it left off, rather than starting over
from the beginning. To use this option, the remote server must also support resuming from a breakpoint. Generally speaking, Web/FTP servers
based on UNIX/Linux all support resumed transfers;
· -T [number of
seconds]: timeout period; specifies how long to wait without a response from the remote server before disconnecting
, and then starting the next attempt. For example, “-T
120” means that if no data is received from the remote server after 120 seconds, it will try reconnecting. If the network speed is
relatively fast, this time can be set shorter; conversely, it can be set longer. Generally it should not exceed 900, and usually
should not be less than 60. Around 120 is generally appropriate;
· -w [number of seconds]: how many seconds to wait between two attempts. For example, “-w
100” means to wait
100 seconds between attempts;
· -Y on/off: connect through / not through a proxy server;
· -Q
[byetes]: limit the total size of downloaded files so it does not exceed a certain amount. For example, “-Q2k” means no more than 2K
bytes, and “-Q3m” means no more than 3M bytes. If nothing is added after the number, then bytes are used as the
unit. For example, “-Q200” means no more than 200 bytes;
·
-nd: do not download the directory structure; place all files downloaded from the specified directories on the server into the current directory;
· -x: exactly the opposite of the “-nd” setting; create the full directory structure. For example, “wget -nd
http://www.gnu.org” will create a “www.gnu.org” subdirectory in the current directory, and then follow the server’s
actual directory structure level by level until all files have been transferred;
·
-nH: do not create a directory named after the target host’s domain name; download the target host’s directory structure directly into the current
directory;
· –http-user=username
· –http-passwd=password: If the Web server requires a specified username and password, use these two items to set them;
· –proxy-user=username
· –proxy-passwd=password: If the proxy server requires a username and password, use these two options
;
· -r: Create the server-side directory structure locally;
· -l [depth]: The depth for downloading the remote server’s directory structure, for example, “-l
5” downloads directory structures or files with a directory depth less than or equal
to 5;
·
-m: An option for mirroring a site. If you want to create a mirror of a site, use this option; it will automatically set
other appropriate options to facilitate site mirroring;
·
-np: Download only the contents of the specified directory on the target site and its subdirectories. This is also a very useful option. We
assume that someone’s personal homepage contains a link to other people’s personal homepages on this site, and we only want to download
this person’s personal homepage. If this option is not set, it is even possible to download the entire site, which is obviously
not what we usually want;
ü How to set the proxy server used by wget
wget can use the user settings file “.wgetrc” to read many settings. Here we mainly use this file to
set the proxy server. Whichever user logs in, the “.wgetrc” file in that user’s home directory takes
effect. For example, if the “root” user wants to use “.wgetrc” to set the proxy server, “/root/.wgert” takes
effect. Below is the contents of a “.wge
trc” file. Readers can refer to this example to write their own “wgetrc” file:
http-proxy = 111.111.111.111:8080
ftp-proxy = 111.111.111.111:8080
The meaning of these two lines is that the proxy server IP address is: 111.111.111.111, and the port number is: 80. The first line specifies
the proxy server used by the HTTP protocol, and the second line specifies the proxy server used by the FTP protocol.
Part Two:
$ wget –help
GNU Wget 1.9.1, a non-interactive network file download tool.
Usage: wget [options]… [URL]…
Arguments required for long options are also required when using short options.
Startup:
-V, –version Display Wget's version and exit.
-h, –help Print this help.
-b, -background Go into the background after startup.
-e, -execute=COMMAND Run commands in '.wgetrc' style.
Logging and input files:
-o, –output-file=file Write log messages to the specified file.
-a, –append-output=file Append log messages to the end of the specified file.
-d, –debug Print debugging output.
-q, –quiet Quiet mode (no output).
-v, –verbose Verbose output mode (default).
-nv, –non-verbose Turn off verbose mode, but do not enter quiet mode.
-i, –input-file=file Download URLs found in the specified file.
-F, –force-html Treat the input file as HTML.
-B, –base=URL When using the -F -i file option, prepend the specified URL to relative links.
Download:
-t, –tries=number Set the number of retries (0 means unlimited).
–retry-connrefused Retry even if the connection is refused.
-O –output-document=file Write data to this file.
-nc, –no-clobber Do not overwrite existing files, and do not write new files by
appending .# (# is a number) to the filename.
-c, –continue Continue downloading a partially downloaded file.
–progress=type Select how download progress is displayed.
-N, –timestamping Do not retrieve the file unless the remote file is newer.
-S, –server-response Display server response messages.
–spider Do not download any data.
-T, –timeout=seconds Set the timeout for reading data (in seconds).
-w, –wait=seconds Wait the specified number of seconds between retrieving different files.
–waitretry=seconds Wait a while between each retry (from 1 second up to the specified number of seconds).
–random-wait Wait a while between retrieving different files (from 0 seconds up to 2*WAIT seconds).
-Y, –proxy=on/off Turn the proxy server on or off.
-Q, –quota=size Set the quota for received data.
–bind-address=address Use the specified local address (hostname or IP) for the connection.
–limit-rate=rate Limit the download rate.
–dns-cache=off Disable DNS lookups from cache.
–restrict-file-names=OS Restrict characters in filenames to those allowed by the specified OS (operating system).
Directories:
-nd –no-directories Do not create directories.
-x, –force-directories Force the creation of directories.
-nH, –no-host-directories Do not create directories containing the remote host name.
-P, –directory-prefix=name Create the specified directory before saving files.
–cut-dirs=number Ignore the specified number of directory levels in the remote directory.
HTTP options:
–http-user=user Set the HTTP username.
–http-passwd=password Set the HTTP password.
-C, –cache=on/off (Do not) use data from the server cache (enabled by default).
-E, –html-extension Append the .html extension to all files with MIME type text/html.
–ignore-length Ignore the “Content-Length” header field.
–header=string Add the specified string to the header.
–proxy-user=user Set the proxy server username.
–proxy-passwd=password Set the proxy server user password.
–referer=URL Include the “Referer: URL” header in the HTTP request.
-s, –save-headers Save HTTP headers to the file.
-U, –user-agent=AGENT Identify as AGENT instead of Wget/VERSION.
–no-http-keep-alive Disable HTTP keep-alive (persistent connections).
–cookies=off Disable cookies.
–load-cookies=file Load cookies from the specified file before the session starts.
–save-cookies=file Save cookies to the specified file after the session ends.
–post-data=string Use the POST method and send the specified string.
–post-file=file Use the POST method and send the contents of the specified file.
HTTPS (SSL) options:
–sslcertfile=file Optional client certificate.
–sslcertkey=key file Optional “key file” for this certificate.
–egd-file=file EGD socket filename.
–sslcadir=directory Directory containing the CA hash table.
–sslcafile=file File containing the CA.
–sslcerttype=0/1 Client-Cert type 0=PEM (default) / 1=ASN1 (DER)
–sslcheckcert=0/1 Check the server certificate against the provided CA
–sslprotocol=0-3 Select the SSL protocol; 0=auto-select,
1=SSLv2 2=SSLv3 3=TLSv1
FTP options:
-nr, –dont-remove-listing Do not remove “.listing” files.
-g, –glob=on/off Set whether to expand filenames containing wildcards.
–passive-ftp Use “passive” transfer mode.
–retr-symlinks In recursive mode, download the files pointed to by links (except
those linking to directories).
Recursive download:
-r, –recursive Download recursively.
-l, –level=number Maximum recursion depth (inf or 0 means unlimited).
–delete-after Delete files after downloading.
-k, –convert-links Convert absolute links to relative links.
-K, –backup-converted Back up file X as X.orig before converting it.
-m, –mirror Equivalent to the options -r -N -l inf -nr.
-p, –page-requisites Download all files needed to display a complete web page, such as images.
–strict-comments Enable strict (SGML) handling of HTML comments.
Options related to accept/reject during recursive download:
-A, –accept=list List of accepted file patterns, separated by commas.
-R, –reject=list List of rejected file patterns, separated by commas.
-D, –domains=list List of accepted domains, separated by commas.
–exclude-domains=list List of excluded domains, separated by commas.
–follow-ftp Follow FTP links in HTML files.
–follow-tags=list HTML tags to follow, separated by commas.
-G, –ignore-tags=list HTML tags to ignore, separated by commas.
-H, –span-hosts Allow recursion to span to other hosts.
-L, –relative Follow relative links only.
-I, –include-directories=list List of directories to download.
-X, –exclude-directories=list List of directories to exclude.
-np, –no-parent Do not search parent directories.
Complete Vi Command Reference—Editing Files
Commands for entering vi
vi filename: Open or create a file, and place the cursor at the beginning of the first line
vi +n filename: Open a file, and place the cursor at the beginning of line n
vi + filename: Open a file, and place the cursor at the beginning of the last line
vi +/pattern filename: Open a file, and place the cursor at the first string matching pattern
vi -r filename: Recover filename after a system crash occurred while editing with vi last time
vi filename….filename: Open multiple files and edit them one by one
Cursor movement commands
h: move the cursor left by one character
l: move the cursor right by one character
space: move the cursor right by one character
Backspace: move the cursor left by one character
k or Ctrl+p: move the cursor up one line
j or Ctrl+n: move the cursor down one line
Enter: move the cursor down one line
w or W : move the cursor right by one word to the beginning of the word
b or B : move the cursor left by one word to the beginning of the word
e or E : move the cursor right by one word to the end of the word
): move the cursor to the end of the sentence
(: move the cursor to the beginning of the sentence
}: move the cursor to the beginning of the paragraph
{: move the cursor to the end of the paragraph
nG: move the cursor to the beginning of line n
n+: move the cursor down n lines
n-: move the cursor up n lines
n:movethe cursortothe end oflinenH:movethe cursortothetopof the screenlineM:movethe cursortothemiddleof the screenlineL:movethe cursortothebottomof the screenline0:movethe cursortothe beginning ofthecurrentline: move the cursor to the end of the current line
Screen scrolling commands
Ctrl+u: Scroll up half a screen toward the beginning of the file
Ctrl+d: Scroll down half a screen toward the end of the file
Ctrl+f: Scroll forward one full screen toward the end of the file
Ctrl+b: Scroll back one full screen toward the beginning of the file
nz: Scroll line n to the top of the screen; if n is omitted, scroll the current line to the top of the screen.
Text insertion commands
i: Before the cursor
I: At the beginning of the current line
a: After the cursor
A: At the end of the current line
o: Open a new line below the current line
O: Open a new line above the current line
r: Replace the current character
R: Replace the current character and the characters after it until ESC is pressed
s: Starting at the current cursor position, replace the specified number of characters with the entered text
S: Delete the specified number of lines and replace them with the entered text
ncw or nCW: Change the specified number of words
nCC: Change the specified number of lines
Deletion commands
ndw or ndW: Delete n words starting at the cursor position
do: Delete to the beginning of the line
d$: Delete to the end of the line
ndd: Delete the current line and the next n-1 lines
x or X: Delete one character; x deletes the character after the cursor, while X deletes the character before the cursor
Ctrl+u: Delete the text entered in input mode
Search and replace commands
/pattern: Search for pattern from the cursor position toward the end of the file
?pattern: Search for pattern from the cursor position toward the beginning of the file
n: Repeat the previous search command in the same direction
N: Repeat the previous search command in the opposite direction
:s/p1/p2/g: Replace all p1 in the current line with p2
:n1,n2s/p1/p2/g: Replace all p1 with p2 from line n1 to line n2
:g/p1/s//p2/g: Replace all p1 in the file with p2
Option settings
all: List all option settings
term: Set the terminal type
ignorance: Ignore case in searches
list: Display tab characters (Ctrl+I) and end-of-line markers ($)
number: Display line numbers
report: Display the number of changes made by line-oriented commands
terse: Display brief warning messages
warn: Display the NO write message when switching to another file if the current file has not been saved
nomagic: Allow special characters without a preceding “” in search patterns
nowrapscan: Prevent vi from continuing the search from the other end when it reaches either end of the file
mesg: Allow vi to display messages written to your terminal by other users with write
Last-line mode commands
:n1,n2 co n3: Copy the content from line n1 to line n2 to below line n3
:n1,n2 m n3: Move the content from line n1 to line n2 to below line n3
:n1,n2 d: Delete the content from line n1 to line n2
:w: Save the current file
:e filename: Open file filename for editing
:x: Save the current file and exit
:q: Quit vi
:q!: Quit vi without saving the file
:!command: Execute the shell command command
:n1,n2 w!command:
Use the content from line n1 to line n2 in the file as input to command and execute it,
If n1 and n2 are not specified, it means using the entire file content as input to command
:r!command: Put the output of command at the current line
Register operations
“?nyy:
Save the current line and the next n lines to register ?, where ? is a letter and n is a number
“?nyw:
Save the current line and the next n words to register ?, where ? is a letter and n is a number
“?nyl:
Save the current line and the next n characters to register ?, where ? is a letter and n is a number
“?p:
Take the content from register ? and put it at the cursor position. Here, ? can be a letter or a number
ndd:
Delete the current line and the next n-1 lines, and place the deleted content into delete register 1