CLI Magic: For geek cred, try these one-liners

本文介绍了一系列实用的Linux命令行技巧,包括文件操作、进程管理、用户列表整理及邮件报告生成等,通过巧妙组合如grep、awk、sort等工具,提升系统管理效率。

CLI Magic: For geek cred, try these one-liners

By Sergio Gonzalez Duran on July 23, 2008 (9:00:00 AM)

Share    Print    Comments   

In this context, a one-liner is a set of commands normally joined through a pipe (|). When joined by a pipe, the command on the left passes its output to the command on the right. Simple or complex, you can get useful results from a single line at the bash command prompt.

For example, suppose you want to know how many files are in the current directory. You can run:

  
ls | wc -l

That's a very simple example -- you can get more elaborate. Suppose you want to know about the five processes that are consuming the most CPU time on your system:

  
ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6

The ps command's o lets you specify the columns that you want to be shown. sort -r does a reverse order sort with the second column (pcpu) as reference (k2). head gets only the first six lines from the ordered list, which includes the header line. You can place pcpu as the first column and then omit the k2 option because sort by default takes the first column to do the sort. That illustrates how you may have to try several approaches on some one-liners; different versions and ways to manipulate the options may produce different results.

A common situation for Linux administrators on servers with several users is to get quick ordered user lists. One simple way to get that is with the command:

  
cat /etc/passwd | sort

If you just need the username, the above command returns too much information. You can fix it with something like this:

  
cat /etc/passwd | sort | cut -d":" -f1

The sorted list is passed to cut, where the d option indicates the field's delimiter character. cut breaks into pieces each line, and the first field f1 is the one that you need to display. That's better; it shows only usernames now. But you may not want to see all the system usernames, like apache, bin, and lp. If you just want human users, try this:

  
cat /etc/passwd | sort | gawk '$3 >= 500 {print $1 }' FS=":"

gawk evaluates each line from the output piped to it. If the third field -- the UID -- is equal or greater than 500 (most modern distros start numbering normal users from this number) then the action is done. The action, indicated between braces, is to print the first field, which is the username. The separator for field in the gawk command is a colon, as specified by the FS option.

Now suppose you have a directory with lots of files with different extensions, and you want to back up only the .php files, calling them filename.bkp. The next one-liner should do the job:

  
for f in *.php; do cp $f $f.bkp; done

This command loops through all the files in the current directory looking for those with .php extensions. Each file's name is held in the $f variable. A simple copy command then does the backup. Notice that in this example we used a semicolon to execute the commands one after another, rather than piping output between them.

What about bulk copy? Consider this:

  
tar cf - . | (cd /usr/backups/; tar xfp -)

It creates a tar package recursevely on the current directory, then pipes this package to the next command. The parenthesis creates a temporary subshell, changes to a different directory, then extracts the content of the package, which is the whole original directory. The p option on the last tar command preserves file properties like time and permissions. After completion, the shell context will be at the original directory.

A variant on the previous one-liner lets you do the same kind of backup on a remote server:

  
tar cf - . | ssh smith@remote.server tar xfp - -C /usr/backup/smith

Here, the command establishes an SSH remote session and untars the package with the C option, which changes the directory, in this case to /usr/backup/smith, where the extraction will be made.

grep and gawk and uniq, oh my!

Text processing is a common use for one-liners. You can accomplish marvelous things with the right set of commands. In the next example, suppose you want a report on incoming email messages that look like this:

  

cat incoming_emails
2008-07-01 08:23:17 user1@example.com
2008-07-01 08:25:20 user2@someplace.com
2008-07-01 08:32:41 somebody@server.net
2008-07-01 08:35:03 spam not recived, filtered
2008-07-01 08:39:57 user1@example.com
...

You are asked for a report with an ordered list of who received incoming messages. Many recipients would be repeated in the output of the cat command. This one-liner resolves the problem:

  
grep '@' incoming_email | gawk '{print $3}' | sort | uniq

grep filters the lines that contains a @ character, which indicates an email address. Next, gawk extracts the third field, which contains the email address, and passes it to the sort command. Sorting is needed to group the same recipients together because the last command, uniq, omits repeated lines from the sorted list. The output is shown below. Most text processing one-liners use a combination of grep, sed, awk, order, tr, cut, uniq, and other related commands.

  

somebody@server.net
user1@example.com
user2@someplace.com

If you like any of these one-liners but think they're too long to type often, you can create an alias for the command and put it in your .bashrc file. When you log in your session, anything inside this file will be run, so your personal aliases would be ready at anytime.

  
alias p5="ps -eo pcpu,user,pid,cmd | sort -r | head -6"

You can certainly create better and simpler variations of all of the commands in this article, but they're a good place to start. If you are a Linux system administrator, it's good practice to collect, create, and modify your own one-liners and keep them handy; you never know when are you going to need them. If you have a good one-liner, feel free to share it with other readers in a comment below.

Sergio Gonzalez Duran is a Linux administrator, systems developer, and network security counselor who also teaches Linux courses and publishes the Spanish-oriented Linux and open source Web site linuxtotal.com.mx.

阅读(252) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
当使用 `curl -X POST -d @/my_secret.txt` 出现 `Failed to open /my_secret.txt` 和 `option -d: error encountered when reading a file` 错误,可能有以下几种原因及解决办法: ### 文件路径问题 - **原因**:指定的文件路径 `/my_secret.txt` 可能不正确。在不同的操作系统中,文件路径的表示方式有所不同,而且当前工作目录也会影响文件的查找。 - **解决办法**:使用绝对路径或者确保当前工作目录正确。可以使用 `pwd` 命令查看当前工作目录,使用 `ls` 命令查看当前目录下的文件。如果文件不在当前目录,可以使用正确的绝对路径。例如,如果文件在 `/home/user/secrets` 目录下,命令应该改为: ```bash curl -X POST -d @/home/user/secrets/my_secret.txt "http://019a2aaf-95b5-71b1-b556-55ed8b176b38.geek.ctfplus.cn/aa__%5E%5E.php?filename=php://input" ``` ### 文件权限问题 - **原因**:当前用户可能没有权限读取 `/my_secret.txt` 文件。 - **解决办法**:检查文件的权限,可以使用 `ls -l /my_secret.txt` 命令查看文件的权限设置。如果没有读取权限,可以使用 `chmod` 命令修改文件权限。例如,为所有用户添加读取权限: ```bash chmod +r /my_secret.txt ``` ### 文件不存在问题 - **原因**:指定的文件 `/my_secret.txt` 可能并不存在。 - **解决办法**:确认文件是否存在,可以使用 `ls` 命令查看文件是否在指定路径下。如果文件不存在,需要检查是否文件名拼写错误或者文件是否已经被删除。 ### 转义字符问题 - **原因**:如果文件名中包含特殊字符,可能需要进行转义。 - **解决办法**:使用引号将文件名括起来,确保特殊字符被正确处理。例如: ```bash curl -X POST -d "@/path/to/my secret.txt" "http://example.com" ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值