Answers:
直截了当awk
:
$ ls / | awk 'NR==4'
etc
With zsh
和Glob限定词:
ls -d -- *([7])
要么
print -rl -- *([7])
从名称排序的文件列表中打印第7个文件。
ls -d -- *(om[7])
要么
print -rl -- *(om[7])
将按时间o
顺序输出文件列表中的第7个文件m
(最新的第一个文件-类似于从ls -t
输出中提取第7个条目)。
理想情况下,如果要处理ls
输出,则应使用ls -q | command(s)
where -q
打印?
而不是时髦的char(例如换行符)之类的东西,并且command(s)
仅处理N行输出,打印第行,然后停止。
带有尾巴和头部(为清楚起见添加了nl)。让我们获取第五行:
ychaouche@ychaouche-PC ~ $ ls | nl
1 total 8.3M
2 -rw-r--r-- 1 ychaouche ychaouche 20K Jul 8 15:53 2.docx
3 -rw-r--r-- 1 ychaouche ychaouche 20K Jul 8 15:53 3.docx
4 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 10:11 AUDIO
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
6 drwxr-xr-x 6 ychaouche ychaouche 4.0K May 17 13:49 BZR
7 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
8 drwxr-xr-x 2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
9 -rw-r--r-- 1 ychaouche ychaouche 34K Dec 2 2014 dget_problem.ods
10 drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul 7 10:00 DOCUMENTS
11 drwx------ 2 ychaouche ychaouche 4.0K Apr 6 11:39 Downloads
12 drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
13 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
14 -rw-r--r-- 1 ychaouche ychaouche 690 Jan 25 14:25 IT_Alias.vcf
15 drwxr-xr-x 4 ychaouche ychaouche 4.0K Dec 2 2014 MACONF
16 -rw-r--r-- 1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
17 drwxr-xr-x 4 ychaouche ychaouche 4.0K Jan 5 2015 MUSIQUE
18 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 2 16:29 NOTES
19 -rw-r--r-- 1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
20 -rw-r--r-- 3 ychaouche ychaouche 62K Apr 2 2014 php-mode.el
21 drwxr-xr-x 2 ychaouche ychaouche 4.0K Apr 8 13:25 Pictures
22 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jan 6 2015 Projects
23 -rw-r--r-- 1 ychaouche ychaouche 353 Jan 12 2015 proxylog.dat
24 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
25 -rw-r--r-- 1 ychaouche ychaouche 21 Nov 23 2014 registrationcode
26 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
27 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
28 drwxr-xr-x 8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
29 drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
30 drwxr-xr-x 5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
31 drwxr-xr-x 7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
32 drwxr-xr-x 3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
33 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 12:24 WTMP
ychaouche@ychaouche-PC ~ $
这将跳到第五行:
ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
6 drwxr-xr-x 6 ychaouche ychaouche 4.0K May 17 13:49 BZR
7 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
8 drwxr-xr-x 2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
9 -rw-r--r-- 1 ychaouche ychaouche 34K Dec 2 2014 dget_problem.ods
10 drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul 7 10:00 DOCUMENTS
11 drwx------ 2 ychaouche ychaouche 4.0K Apr 6 11:39 Downloads
12 drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
13 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
14 -rw-r--r-- 1 ychaouche ychaouche 690 Jan 25 14:25 IT_Alias.vcf
15 drwxr-xr-x 4 ychaouche ychaouche 4.0K Dec 2 2014 MACONF
16 -rw-r--r-- 1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
17 drwxr-xr-x 4 ychaouche ychaouche 4.0K Jan 5 2015 MUSIQUE
18 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 2 16:29 NOTES
19 -rw-r--r-- 1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
20 -rw-r--r-- 3 ychaouche ychaouche 62K Apr 2 2014 php-mode.el
21 drwxr-xr-x 2 ychaouche ychaouche 4.0K Apr 8 13:25 Pictures
22 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jan 6 2015 Projects
23 -rw-r--r-- 1 ychaouche ychaouche 353 Jan 12 2015 proxylog.dat
24 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
25 -rw-r--r-- 1 ychaouche ychaouche 21 Nov 23 2014 registrationcode
26 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
27 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
28 drwxr-xr-x 8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
29 drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
30 drwxr-xr-x 5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
31 drwxr-xr-x 7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
32 drwxr-xr-x 3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
33 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 12:24 WTMP
ychaouche@ychaouche-PC ~ $
然后,显然头-1
ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5 | head -1
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
ychaouche@ychaouche-PC ~ $
ls "$@"
传递到的更一般。当然,使用后要取出来。lsn
ls
shift
$1