生命开始情不情愿都要走完一生

2008年2月16日星期六

LINUX COMMANDS

ssh - Log into remote servers

* Usage: ssh -p<port> <username>@<hostname>
* Example: ssh -p1234 theo@production
o Logs into the server named production on port 1234

scp - Copies files to/from remote servers

* Usage: scp -P<port> <source> <target>
* Example: scp -P1234 /home/theo/myfile.txt production@/home/jsmith
o Copies myfile.txt from /home/theo to the server named production under /home/jsmith


df

Display overall disk utilization information for mounted filesystems on file. Usually, file is a device file for a partition, such as /dev/hda1. The file may also be the mount point or any file beneath the mount point. If file is omitted, information for mounted filesystems on all devices in /etc/fstab are displayed.

df [options] [file [file...]]
-h Displays results in a human-readable format, including suffixes such as M (megabytes) and G (gigabytes).
-i Displays information on remaining inodes rather than the default disk space information.

du

Display disk utilization information for directories. If directories are omitted, the current working directory is searched.

-a Shows all files, not just directories.
-c Produces a grand total for all listed items.
-h Displays results in a human-readable format, including suffixes such as M (megabytes) and G (gigabytes).
-s Prints a summary for each of the directories specified, instead of totals for each subdirectory found recursively.
-S Excludes subdirectories from counts and totals, limiting totals to directories.

Mount
mount [command_line_options] device
mount [command_line_options] directory
mount [command_line_options] device directory
Used to mount filesystems onto the filesystem hierarchy. The first and second forms consult /etc/fstab and mount the filesystem located on device or intended to be attached to directory, respectively. In both cases, information necessary to complete the mount operation is taken from /etc/fstab. The third form is independent of /etc/fstab and mounts the filesystem on device at mount point directory.
The mount command accepts two kinds of options: command-line and mount. The command-line options provide general direction for the mount command. The mount options are used to specify additional information about the device being mounted.

-a
Mounts all of the partitions specified in /etc/fstab, except those with the noauto option.

-h
Displays help on the mount command.

-o mount_options
Specifies mount options on the command line.

-r
Mounts the filesystem as read-only.

-t fstype
Specifies that the filesystem to be mounted is of type fstype. This option is typically used interactively when no entry for the mount exists in /etc/fstab.

-v
Sets verbose mode.

-w
Mounts the filesystem read/write mode.


mount -t vfat /dev/fd0 /mnt/floppy --软盘载入 VFAT支持长文件名
mount -t iso9660 /dev/cdrom /mnt/cdrom
mount /dev/sdc


ls

ls -a to reveal hidden files

ls -l for long listings,

---------------------------------------------------------------

-rw-r--r-- 1 dearhwj 11311 6 Aug 4 06:22 1.txt
-rw-r--r-- 1 dearhwj 11311 1 Aug 4 06:22 2.txt
-rw-r--r-- 1 dearhwj 11311 0 Aug 7 21:52 a.out
----------------------------------------------------------------

第1栏是文件的权限标志;第2栏给出的是这个文件的链接个数;第3栏是文件所有者的用户名;第4栏是这个用户所在的用户组组名;第5栏给出文件的长度;两栏是这个文件或者子目录创建或者最后一次被修改时的日期和时间;而最后一栏很明显就是这个文件的文件名。


ls -k:即--block-size=1K。

ls -i 显示inode numbers

ls -m 也可以使用- m参数把文件用逗号分隔显示在

ls -x 也可以用- x参数按水平对齐的方式而不是缺省的垂直对齐方式列出文

ls -X 用扩展名排序

ls -R,--recursive:同时列出所有子目录层。

ls -F - F参数使得ls命令可以在显示子目录的时候通过特殊标记对内容加以区分

Character

Meaning

*

Executable

/

Directory

@

Symbolic link

|

FIFO

=

Socket


ls --color

Color

Meaning

Default shell text color

Regular file

Green

Executable

Blue

Directory

Magenta

Symbolic link

Yellow

FIFO

Magenta

Socket

Red

Archive (.tar, .zip, .deb, .rpm)

Magenta

Images (.jpg, .gif, .png, .tiff)

Magenta

Audio (.mp3, .ogg, .wav)


ls -R 它将逐层进入所有的子目录并把其中的内容全部显示出来.一个比较好的办法是把- d参数和- R参数一起使用,这样可以只列出有限的子目录层次下的文件。
ls -r 缺省的是按字幕排序,-r反转
ls -rX 按扩展名排序并反转
ls -t 按时间进行排序,
ls -tr 按时间反转排序
ls -S 根据文件大小排序
ls -Sr 根据文件大小反序排列
ls -h 方便阅读文件大小


cd
cd ~ 进入用户目录
cd 进入用户目录
cd - 进入前次目录

mkdir

mkdir -p a/b/c 循环创建目录


mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a} 使用一个命令来定义复杂的目录树

mkdir temp2 temp3 temp3 # 同时创建多个目录

mkdir -m 设置访问权限 mkdir -m 444 personal

mkdir -v 显示创建目录的详细过程


rmdir

删除文件夹(必须是空的文件夹)

rmdir -p temp5/parent/child - p参数。可以使用这个参数来删除某个子目录的全部继承结构


rm

删除文件


rm file1 file2 file3 #同时删除多个文件


export

export PATH=$PATH:/usr/sbin:/sbin

For these variables to be available, they must be exported, at which time they become environment variables. Environment variables are passed on to programs and other shells, and together they are said to form the environment in which the programs execute. PATH is always made into an environment variable. Exporting a shell variable to turn it into an environment variable is done using the export command.

When a variable is exported to the environment, it is passed into the environment of all child processes. That is, it will be available to all programs run by your shell.


set
显示所有的变量(局部的和export的)
env
只显示export的变量
export
只显示export的变量
unset
unset& 删除变量

touch

一般会在两种情况下用到它。第一种情况是建立文件,第二种情况是更新文件的修改日期

创建文件

-------------------------------------------------------------------

touch text1.txt

用 > text2.txt 也可以创建一个0 size的文件 ,但touch还可以用来修改时间。

---------------------------------------------------------------------

使用touch命令的- t参数加上一个代表年月日时的数字把某个文件的建立日期或者时间设置为一个具体的时间,如下所示:
# touch -t 1225130000 newfile2



touch -a "filename" 修改访问时间

touch -m "filename" 修改“修改时间”


ps

sunbox# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 20:15:23 ? 0:14 sched
root 1 0 0 20:15:24 ? 0:00 /sbin/init
root 2 0 0 20:15:24 ? 0:00 pageout
root 3 0 0 20:15:24 ? 0:00 fsflush
daemon 240 1 0 20:16:37 ? 0:00 /usr/lib/nfs/statd

第一列列出用于运行该进程的用户身份,第二列列出进程的 ID,第三列列出该进程的父进程 ID。最后一列是进程的描述,通常是所运行的二进制文件的名称。每个进程都被分配一个标识符,称为进程标识符(Process Identifier,PID)。进程还有父进程,在大多数情况下就是启动它的进程的 PID。

父 PID (PPID) 的存在意味着这是一个由别的进程创建的进程。最初创建进程的原始进程名为 init,它始终被赋予 PID 1。init 是将在内核启动时启动的第一个实际进程。启动系统的其余部分是 init 的工作。init 和其他具有 PPID 0 的进程属于内核。

-f "Forest" mode, which is used to display process family trees.

-l Long format, which includes priority, parent PID, and other information.
-u User format, which includes usernames and the start time of processes.
-w Wide output format, used to eliminate the default output line truncation. Useful for the -f option.
-x Include processes without controlling terminals. Often needed to see daemon processes and others not started from a terminal session.
-C cmd Display instances of command name cmd.
-U userDisplay processes owned by username user.


pstree
pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process
trees rooted at processes owned by that user are shown.
----------------------------------------------------
pstree | more
----------------------------------------------------

top
-b
Run in batch mode. This is useful for sending output from top to other programs or to a file. It executes the number of iterations specified with the -n option and terminate. This option is also useful if top cannot display on the terminal type you are using.
-d delay
Specify the delay in seconds between screen updates. The default is five seconds.
-i
Ignore idle processes, listing only the "interesting" ones taking system resources.
-n num
Display num iterations and then exit, instead of running indefinitely.
-q
Run with no delay. If the user is the superuser, run with highest possible priority. This option causes top to update continuously and will probably consume any idle time your CPU had. Running top -q as superuser will seriously affect system performance and is not recommended.
-s
Run in secure mode. Some of top's interactive commands can be dangerous if running as the superuser. This option disables them.
快捷方式
默认按CPU的使用率来排序,可以按C、M、T实现按CPU、内存和时间的排序。
Ctrl-L
Refresh the screen.
h
Generate a help screen.
k
Kill a process. You will be prompted for the PID of the process and the signal to send it.
n
Change the number of processes to show. You will be prompted to enter an integer number. The default is 0, which indicates that the screen should be filled.
q
Quit the program.
r
Change the priority of a process (renice). You will be prompted for the PID of the process and the value to nice it to. Entering a positive value causes a process to lose priority. If the superuser is running top, a negative value may be entered, causing a process to get a higher than normal priority. This command is not available in secure mode.

find

-----------------------------------------------------

如果想查找子目录/ u s r / b i n中最近一百天没有使用过的程序,可以采用下面的命令:
# find /usr/bin -type f -atime +100 -print
- atime参数后面跟着代表日期的一个数字,用来查找在过去几天访问过的文件

如果想在子目录/ u s r / b i n中查找建立不到一天的
新文件或者二十四小时内修改过的文件,可以使用下面的参数:
# find /usr/bin -type f -mtime -1 -print
- mtime参数后面跟一个表示天数的数字(本例中是一天或者更短的时间),用来查找在过去几天修改过的文件

-lname 查找 某个文件的软链接


find /home -inum 90469 根据 inode 来查找某个inode的 hard link

find / –links 2 查找特定link数的文件

find / -group grpoupname:搜索所有组为grpoupname的文件。

find / -user用户名:搜索所有文件属主为用户名(ID或名称)的文件。

find / -size n:搜索文件大小是n个block的文件。

------------------------------------------------------------------------------------------

locate

Searches with the locate command are almost instantaneous. And locate searches don’t require the full filename. The drawback is that the locate
command database is normally updated only once each day, as documented in the /etc/cron.daily/slocate.cron script.


locate命令的数据库文件会在每天凌晨的五点五十分自动更新。如果想为locate命令的数据库文件立刻生成一个最新的版本,可以使用updated b命令。以根操作员的身份登录进入系统,或者使用s u命令取得根操作员的权限之后,在提示符下敲入:
# updatedb

updatedb

Refresh (or create) the slocate database in /var/lib/slocate/slocate.db.

-e directories

Exclude a comma-separated list of directories from the database. updatedb is typically executed periodically via cron

whereis
whereis命令可以迅速地找到文件,而且它还可以提供这个文件的二进制可执行文件、源代码文件和使用手册页存放的位置
------------------------------------------------------
# whereis find
find: /usr/bin/find /usr/man/man1/find.1.gz
------------------------------------------------------
可以采用下面的方法让w h e r e i s命令只查找这个程序的二进制可执行文件:
-----------------------------------------------------------
# whereis -b find
find: /usr/bin/find
---------------------------------------------------------------
whereis命令检索的子目录是固定编写在它的程序中的。虽然这看起来有点象是个缺陷,但把搜索限制在固定的子目录如/usr/man、/usr/bin或/usr/sbin中可以加快文件查找的进度。

whereis -m 只查找man page
whereis -s 只查找source

whatis
whatis命令能够从某个程序的使用手册页中抽出一行简单的介绍性文字,帮助你迅速了解这个程序的具体功能。例如,如果希望了解w h o命令有什么作用,可以输入下面的内容:
# whatis who
-------------------------------------
who ( 1 ) - show who is logged on
-------------------------------------

#whatis sync
-------------------------------------
sync (1) - flush file system buffers #结果中的(1) (2) (3p) (8) 对应到了man的section
sync (2) - commit buffer cache to disk
sync (3p) - schedule file system updates
sync (8) - synchronize data on disk with memory
--------------------------------------



apropos
apropos命令在这个数据库文件中按照给出的关键字进行搜索。如果能保证whatis数据库文件及时更新,就可以使用apropos命令来帮助找到需要的程序.


which
The which command tells you which version of a command will run if you just type its name.The which command is also a speedy way to tell if a command is on your system.If the command is on your system and in your PATH, you'll be told where to find it; if the command doesn't exist, you're back on the command line with nothing.

which -a 找到所有的路径

cat
可以使用cat命令和重定向操作符(“>”)来合并文件。例如,如果想把文件test.txt和文件test2.txt合并到另外一个叫做test3.txt的文件中去,可以使用下面的方法:
----------------------------------------
cat test1.txt test2.txt > test3.txt
----------------------------------------

使用cat命令和重定向符“ > >”可以用来追加
------------------------------------------
cat test1.txt >> test2.txt
------------------------------------------

cat命令可以读取标准输入,所以可以使用c a t命令建立一个文件并通过键盘直接向这个文件中输入内容按下C t r l + D组合键关闭这个文件.
----------------------------------
# cat > myfile.txt
----------------------------------

cat -n myfile.txt 输出行号




less and more

With the more filename command, you can scroll through the text of a file, from start to finish, one screen at a time. With the less filename command, you can scroll in both directions through the same text with the PAGE UP and PAGE DOWN keys. Both commands support vi-style searches.

如果想向下翻一页,按空格键。如果想向上翻一页,按B键。
如果使用了惊叹号( ! ),还可以从more/less 命令中去执行其他的命令
-------------------------------------------------------
more test1.txt
........................
! wc programs.txt
............................
---------------------------------------------------------
还 可以使用less搜索命令在一个文本文件中进行快速查找。先按下斜杠键( / ),再输入一个单词或者词组的一部分。l e s s命令会在文本文件中进行快速查找,并把找到的第一个搜索目标高亮度显示。如果希望继续查找,请按下斜杠键( / ),再按下回车键。如果想退出阅读,请按下Q键,就返回到shell命令行。


To view information about the file while you're in less, press =



less -N text1.txt 显示行号



键入v 可以在 less和 vim 之间切换

cp

cp -r tempdir1 tempdir2 - r参数。可以使用这个参数把一个子目录拷贝到另外一个子目录中去 用-R也可以

cp -a //same as -dpR 可以使用这个参数把一个子目录拷贝到另外一个子目录中去 用-R也可以

cp -p tempdir2/tempdir1/tempfile1 tempdir3 -p 把文件连同它的子目录结构一起拷贝过去

cp "sourcefilepath" . 拷贝到当前路径
cp -d same as --no-dereference --preserve=link
--no-dereference never follow symbolic links 只拷贝链接,不拷贝链接指向的内容
When a symbolic link is encountered with cp, the contents of the file to which the link points are copied unless the -d option is specified. This "no dereference" operator causes cp to copy the links themselves instead. For example, consider a directory dir1 containing a symbolic link, which is recursively copied to other directories with and without the -d option:
-----------------------------------------------------
# ls -l dir1
total 13
lrwxrwxrwx 1 root root 19 Jan 4 02:43 file1 -> /file1
-rw-r--r-- 1 root root 10240 Dec 12 17:12 file2
# cp -r dir1 dir2
# ls -l dir2
total 3117
-rw-r--r-- 1 root root 3164160 Jan 4 02:43 file1
-rw-r--r-- 1 root root 10240 Jan 4 02:43 file2
# cp -rd dir1 dir3
# ls -l dir3
total 13
lrwxrwxrwx 1 root root 19 Jan 4 02:43 file1 -> /file1
-rw-r--r-- 1 root root 10240 Jan 4 02:43 file2
-----------------------------------------------------







mv

While you can’t rename a file in Linux, you can move it. The mv command essentially puts a different label on a file. Unless you’re moving the file to a different partition, everything about the file, including the inode number, remains the same.

If a target file or directory does not exist, source is renamed to target. If a target file already exists, it is overwritten with source. If target is an existing directory, source is moved into that directory. If source is one or more files and target is a directory, the files are moved into the directory.


ln

Hard links include a copy of the file. As long as the hard link is made within the same partition, the inode numbers are identical. You could delete a hard-linked file in one directory, and it would still exist in the other directory. On the other hand, a soft link serves as a redirect; when you open up a file created with a soft link, you’re directed to the original file. If you delete the original file, the file is lost. While the soft link is still there, it has nowhere to go.

Symbolic links

A symbolic link is simply a pointer to another filename. When Linux opens a symbolic link, it reads the pointer and then finds the intended file that contains the actual data. Symbolic links can point to other filesystems, both local and remote, and they can point to directories. They are clearly listed as being a link with the ls -l command by displaying a special "l" (a lowercase L) in column one, and they have no file protections of their own (the actual file's permissions are used instead). A symbolic link can point to a filename that does not actually exist. Such a symbolic link is said to be broken or stale.

Hard links

A hard link is not really a link at all. It is simply another directory entry for an existing file. The two directory entries have different names but point to the same inode and thus to the same actual data, ownership, permissions, and so on. In fact, when you delete a file, you are only removing a directory entry (in other words, one hard link to the file). As long as any directory entries remain, the file's inode is not actually deleted. In fact, a file is not deleted until its link count drops to zero (and the file is no longer open for reading or writing).

Hard links have two important limitations. First, because all of the links to a file point to the same inode, any hard links must by definition reside on the same filesystem. Second, hard links cannot point to directories. However, hard links take no disk space beyond an additional directory entry.

ln [options] file link
ln [options] files directory

-f

Overwrite (force) existing links, or existing files in the destination directory.

-i

Prompt interactively before overwriting destination files.

-s

Create a symbolic link rather than a hard link.

Sort

By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line.



tac


wc
The wc command, short for word count, can return the number of lines, words, and characters in a file. The wc options are straightforward; for example, wc -w filename returns the number of words in that file.



BASH快捷方式

  • CTRL+A 移动到当前行的起始处。
  • CTRL+E 移动到当前行的末尾。
  • CTRL+K 删除当前行中光标所在位置后的所有字符。
  • CTRL+P(表示上一条),或向上箭头键,移动到历史缓冲区中的上一条命令。 同!!
  • CTRL+N(表示下一条),或向下箭头键,移动到历史缓冲区中的下一条命令。
  • CTRL+R(表示反向搜索)对以前的命令进行搜索。

您还可以使用 history 命令来显示以前的命令,每条命令都带有它在历史记录缓冲区中的编号。然后,您可以使用该编号,以感叹号 (!) 作为开始,执行缓冲区中一条特定的命令,比如 !382。通过使用感叹号加上一个负数,您还可以对历史记录缓冲区进行相对索引。例如,!-2 执行历史记录缓冲区中的倒数第二条命令。因为历史记录缓冲区可以跨越登录会话,所以它为前面的命令的重放提供了一种功能强大的技术。通过输入 set -o option-name ,您可以很容易地设置这些选项;而通过输入 set +o option-name ,您可以很容易地撤销这些选项。例如,set -o emacs 打开了 Emacs 键绑定选项。


没有评论: