Linux 常用命令 Tips

1.wget用-O参数可以指定下载文件的名字,-c参数可以继续上次未完成的任务。


2.关于tar和zip的区别

tar only makes a single file out of multiple files, it doesn't do compression unless combined a compression program such as gzip or bzip2 (which you can call from within tar by using the -z or -j options, respectively). zip combines both the archiving and compression in one program.


tar解压缩文件,用

tar -zxvf name.tar.gz 

默认解压到当前目录,如果要解压到其他目录,用-C参数

tar -zxvf name.tar.gz -C destDirectory

3.知道一个文件名字,但不知道所在路径,我们可以通过locate或find命令查找,下面是两者的man page,两者的主要区别在于实时性,locate速度快,但是不能反映文件最新变化,find相对来说慢,但是能够准确的查找文件。

Locate  reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.If --regex is not specified, PATTERNs can contain globbing characters.  If any PATTERN contains no globbing characters, locate behaves  as  if  the pattern were *PATTERN*.By  default, locate does not check whether files found in database still exist (but it does require all parent directories to exist if the database  was built with --require-visibility no).  locate can never report files created after the most recent update of the relevant database.

可以发现,locate通过检索自己的数据库得出结果,因此速度很快,但是这种方法最大的缺点就是,不能反映文件的最新变化(新建的文件检索不到或者是已经删除的文件还可以检索到),需要通过updatedb命令更新数据库,locate才能得到最新的文件变化信息。

Find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known  (the  left  hand  side  is false for and operations, true for or), at which point find moves on to the next file name.

find通过字符匹配的方式查找文件,可以使用正则表达式,实时查找指定目录下的文件。


4.linux中经常会遇到修改环境变量,让其只起一次作用的情况,以LD_PRELOAD为例,我们只想临时更改加载的共享库,那么可以用如下的形式:

LD_PRELOAD=libpath/libname execpath/execname

这条命令执行完以后,环境变量LD_PRELOAD的值还是以前的

另外,在调试的时候,经常会用自己编写的标准库函数重载默认的标准库函数。这时,自己编写的标准库函数名和参数列表以及返回值类型一定要和标准库函数一致,只有这样,在设置了LD_PRELOAD环境变量后,系统在链接的时候才知道用新版本函数覆盖标准库版本。要不然,会出现重名函数(但是参数列表不同,或者返回值类型不同),这在C语言中是不允许的!!!


5.统计目录中各种文件的个数

find . -maxdepth 1 -type f | wc -l

find命令加上maxdepth参数,指定搜索深度,“1”表示只在当前目录中搜索,不搜索子目录,type参数,指定文件类型。find的输出结果送给wc统计行数,即为当前目录中regular file的个数。wc可以统计文件中字符数,字节数,单词数,行数等信息。


6.What are *-devel packages?

The *-devel packages (usually called *-dev in Debian-based distributions) are usually all the files necessary to compile code against a given library.

For running an application using the library libfoo only the actualy shared library file (*.so.*, for example libfoo.so.1.0) are needed (plus possibly some data files and some version-specific symlinks).

When you actually want to compile a C application that uses that library you'll need the header files (*.h, for example foo.h) that describe the interface of that application as well as a version-less symlink to the shared library (*.so, for example libfoo.so -> libfoo.so.1.0). Those are usually bundled in the *-devel packages.

Sometimes the *-devel packages also include statically compiled versions of the libraries (*.a, for example libfoo.a) in case you want to build a complete stand-alone application that doesn't depend on dynamic libraries at all.

Other languages (such as Java, Python, ...) use a different way of noting the API of a library (effectively including all the necessary information in the actual library) and thus usually need no separate *-devel packages (except maybe for documentation and additional tools).

7.源码包中各种文件的作用。

README描述了项目的情况,包括版权声明,版本更新情况等。

INSTALL文件,里面包含了怎样安装软件或库,还有项目目录下所有文件主要作用的介绍,这对开发者比较重要,可以了解项目结构

CREDITS文件,列出了对项目有贡献人员的名单。

CHANGES文件,描述了各个release之间的变化。


8.linux中,man手册可以查看文件系统中各个目录的作用,命令为:man hier


9.linux中其他用户目录的快速表示法为,下面的目录就显示了username用户目录下的内容,只要在用户名前边加一个波浪号即可,不用再写/home/username了

ls ~username/


10.利用whatis查询指定内容在man page哪些section出现,man介绍如下

Each  manual  page has a short description available within it.  whatis searches the manual page names and displays the manual page descriptions of any name matched.


whatis getopt
输出如下

Getopt::Std (3pm)    - Process single-character switches with switch clustering
getopt (1)           - parse command options (enhanced)
getopt (3p)          - command option parsing
getopt (3)           - Parse command-line options


11.linux中经常会遇到这种情况,我们想删除文件夹下除了文件a之外的所有文件,可以用如下命令解决

rm -rf !(a)

如果想删除除了a和b之外的所有文件,可以用如下命令

rm -rf !(a|b)

删除除了(所有后缀名为cpp和文件a.c)之外的文件

rm -rf !(*.cpp|a.c)


12.关于systemd

//查询name是否开机启动
systemctl is-enabled name.service

//设置服务开机启动
systemctl enable name.service

//取消服务开机启动
systemctl disable name.service

//启动服务
systemctl start name.service

//停止服务
systemctl stop name.service

//重启服务
systemctl restart name.service

//重新加载服务配置文件
systemctl reload name.service

//显示启动失败的任务
systemctl --failed



当前的httpd开机不会自动启动,因为Loaded中状态时disabled


13.fedora 19中如何设置脚本开机自动启动

首先建立文件/etc/rc.d/rc.local

touch /etc/rc.d/rc.local

给这个文件加上执行属性

chmod +x  /etc/rc.d/rc.local

编辑rc.local内容,把自己想要执行的脚本放到里面,第一行记得加上

#!/bin/sh

启动rc-local.service服务

systemctl start rc-local.service

重启即可看到自己的脚本已经运行了


14.ps命令常用参数解析


linux进入控制台后,通过ps查看pid和ppid即可发现,在bash中启动的命令,都是bash的子进程,我们称这样的情况为processes associated with a terminal。一般情况下,系统服务daemon属于processesNOT associated with a terminal


-a选项可以查看所有associated with a terminal的进程

-A/-e都可以查看所有的进程,包括其他用户的进程,甚至是root的进程,一般与o选项一起使用,可以指定查看的列,比如以下命令会查看当前系统所有进程的进程id,父进程id,用户名,隶属于哪个terminal,命令名

ps -eo pid,ppid,euser,tty,cmd

我在tty2和tty3同时登录两个用户,分别是user1和user2,然后两个terminal分别运行程序a和程序b,观察进程id,发现程序

-C指定启动进程命令的名字

-p指定进程id,多个进程用逗号分开

-t指定terminal

--ppid指定父进程id


15.fedora自带的man手册不全,没有系统调用等page,需要自己安装

yum install man-pages.noarch

16.dd命令的几个用法

1)清空一个文件或分区,然后再使其变为swap分区

dd if=/dev/zero of=tmpswap bs=1k count=1000000
chmod 600 tmpswap
mkswap tmpswap
swapon tmpswap
清空文件可以用下面的命令

cat /dev/null > filename

也可以用random和urandom随机数产生器来填满一个文件,关于/dev/random和/dev/urandom这两个设备文件的区别,有下面好的解释

On most Linux systems, /dev/random is powered from actual entropy gathered by the environment. If your system isn't delivering a large amount of data from /dev/random, it likely means that you're not generating enough environmental randomness to power it.

I'm not sure why you think /dev/urandom is "slower" or higher quality. It reuses an internal entropy pool to generate pseudorandomness - making it slightly lower quality - but it doesn't block. Generally, applications that don't require high-level or long-term cryptography can use /dev/urandom reliably.

Try waiting a little while then reading from /dev/urandom again. It's possible that you've exhausted the internal entropy pool reading so much from /dev/random, breaking both generators - allowing your system to create more entropy should replenish them.

关键就是random产生的随机数质量高,但是容易阻塞,urandom随机数质量低,因为它可能会采用以前生成的随机数,但是速度快,不阻塞

对安全性要求不是特别高的话,大可以放心的用urandom,目前很多的软件也都是用urandom来产生随机数

dd if=/dev/random of=filename bs=1024 count=10

用bs和count来控制复制的数量(count控制复制多少块,bs控制每块包含多少字节)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值