linux命令详解2-文件管理,查看文件命令

1. cat命令(tac)

  ----连接文件,并打印出来(顺序打印)与tac命令类似只不过,tac命令是逆序打印

【语法】

cat [OPTION]... [FILE]...

【选项】

-A:显示所有的文件内容
-b:输出非空文件的行号,类似-n
-e:类似-vE
-E:在每一行的末尾加上$符号
-n:输出总行数
-s:suppress repeated empty output lines
-t:类似-vT
-T:显示tab
-v:显示不能打印的字符
--help:显示help文档
--version:显示版本信息

【实例】

sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ cat views.py
from django.template import loader,Context
from django.http import HttpResponse
from blog.models import BlogPost # Create your views here.
def archive(request):
posts = BlogPost.objects.all()
t = loader.get_template("archive.html")
c = Context({'posts':posts})
return HttpResponse(t.render(c)) #显示两个文件的内容
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ cat views.py models.py
from django.template import loader,Context
from django.http import HttpResponse
from blog.models import BlogPost # Create your views here.
def archive(request):
posts = BlogPost.objects.all()
t = loader.get_template("archive.html")
c = Context({'posts':posts})
return HttpResponse(t.render(c))
from django.db import models
from django.contrib import admin class BlogPost(models.Model):
title = models.CharField(max_length=)
body = models.TextField()
timestamp = models.DateTimeField()
##order method
class Meta:
ordering = ('-timestamp','title') class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title','timestamp') class Author(models.Model):
name = models.CharField(max_length=)
class Book(models.Model):
title = models.CharField(max_length=)
genre = models.CharField(max_length=)
num_pg = models.IntegerField()
author = models.ManyToManyField(Author) def __unicode__(self):
return self.title
class SmithBook(Book):
authors = models.ManyToManyField(Author,limit_choices_to={'name+endswith':'Smith'})
admin.site.register(BlogPost,BlogPostAdmin) ##显示非空行的行号
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ cat -b views.py
     1    from django.template import loader,Context
     2    from django.http import HttpResponse
     3    from blog.models import BlogPost      4    # Create your views here.
     5    def archive(request):
     6        posts = BlogPost.objects.all()
     7        t = loader.get_template("archive.html")
     8        c = Context({'posts':posts})
     9        return HttpResponse(t.render(c)) #显示文件中所有行的行号
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ cat -n views.py
     1    from django.template import loader,Context
     2    from django.http import HttpResponse
     3    from blog.models import BlogPost
     4    
     5    # Create your views here.
     6    def archive(request):
     7        posts = BlogPost.objects.all()
     8        t = loader.get_template("archive.html")
     9        c = Context({'posts':posts})
    10        return HttpResponse(t.render(c)) #在每行的末尾加上$符号
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ cat -E views.py
from django.template import loader,Context$
from django.http import HttpResponse$
from blog.models import BlogPost$
$
# Create your views here.$
def archive(request):$
    posts = BlogPost.objects.all()$
    t = loader.get_template("archive.html")$
    c = Context({'posts':posts})$
    return HttpResponse(t.render(c))$
#使用<符号输出文件内容,使用>符号,向文件中写内容,以ctrl+d结束,保存在文件中。
sunny@sunny-ThinkPad-T450:~/test/file$ cat<a.txt
line1
sunny@sunny-ThinkPad-T450:~/test/file$ cat>b.txt
this is lineb
sunny@sunny-ThinkPad-T450:~/test/file$ cat<b.txt
this is lineb
#如果文件内容太多,不能全部打印在屏幕上,则可使用下面的语句
sunny@sunny-ThinkPad-T450:~/PycharmProjects/eagle-eye$ cat run_tests.sh | more #使用tac倒序打印文件内容
sunny@sunny-ThinkPad-T450:~/PycharmProjects/eagle-eye$ tac run_tests.sh | more

2. more命令

  ----打印文件内容

【语法】

     more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]

【选项】(常用)

+num:从某一行开始打印
-num:一页最多显示num行
q:退出查看文件模式(ctrl+c)

【实例】

#一页最多显示5行
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ more - views.py
from django.template import loader,Context
from django.http import HttpResponse
from blog.models import BlogPost # Create your views here.
--更多--(%) #从第5行开始显示
sunny@sunny-ThinkPad-T450:~/PycharmProjects/mysite/blog$ more + views.py
# Create your views here.
def archive(request):
posts = BlogPost.objects.all()
t = loader.get_template("archive.html")
c = Context({'posts':posts})
return HttpResponse(t.render(c))

3.tail命令

  ----输出文件的最后部分,默认最后的10行

【语法】

tail [OPTION]... [FILE]...

【选项】

tail filename:显示文件的倒数10行
tail -n filename:显示倒数n行,代替10
tail -f filename: output appended data as the file grows; -f, --follow, and --fol‐
low=descriptor are equivalent

4. head命令(与tail命令类似)

  ----显示文件的前面的部分,默认是前10行

【语法】

 head [OPTION]... [FILE]...

【选项】

head filename:显示文件的前10行
head -n filename:显示前n行,代替10

5. less命令(类似more)

  ---- 查看文件,与more类似,可以使用pageup,pagedown进行翻页查看

上一篇:Linux基本权限


下一篇:eureka2.0页面404报错问题--之坑