【Linux常用命令】head——把一个文件的前n行拷贝到另一个文件上
Created|Updated|编程文摘
|Post Views:
在日常开发工作中偶尔需要查看文件前 n 行的内容,如果文件的大小为几个 GB 时,直接打开文件会很慢,这个时候我们可以通过复制文件的前 n 行到另一个文件上进行查看。
在 Linux 环境中可以使用 head 命令来复制一个文件的前 n 行到另一个文件上。如以下命令表示复制文件 nginx_app_log_20230320.log 中前 10 万行内容到 /tmp 目录下的 app0320_top10w.log 文件中:
1 | head -n 100000 nginx_app_log_20230320.log > /tmp/app0320_top10w.log |
(END)
Author: Johnson Lin
Copyright Notice: All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Related Articles
2023-04-26
Ubuntu 20.04 apt upgrade Error: Could not read response to hello message from hook
当我运行 sudo apt upgrade 命令以更新 Ubuntu 子系统(WSL)中的软件包时,出现了以下错误: 123456789101112131415161718$ sudo apt upgradeReading package lists... DoneBuilding dependency treeReading state information... DoneCalculating upgrade... DoneThe following packages were automatically installed and are no longer required: ant ant-optional gdisk gir1.2-packagekitglib-1.0 jruby-openssl junit4 libappstream4 libasm-java libatasmart4 libbcpkix-java libbcprov-java libblockdev-crypto2 libblockdev-fs2 libblockdev-loop2 libbl...
2023-04-20
如何查看通过 systemctl 命令启动的服务的日志
systemctl 是 Linux 系统服务管理工具,它可以用来启动、停止、重启、启用或禁用系统服务。当我们启动一个服务时,systemctl 会记录服务的启动日志,我们可以通过查看这些日志来排查服务启动时的问题。 查看 systemctl 服务日志的步骤如下: 找到服务的完整名称。系统服务的名称通常以 .service 结尾,例如 httpd.service、mysql.service 等。 使用 journalctl 命令查看服务的日志。命令格式为: 1journalctl -u <服务名称> 将 <服务名称> 替换为服务的完整名称。例如,要查看 httpd 服务的日志,运行: 1journalctl -u httpd.service journalctl 会输出服务的启动日志、运行日志和停止日志。 (END)

2024-06-05
Finding the Hostname in Linux: A Guide to Common Methods
Finding the hostname in Linux can be done in a few different ways, depending on your specific needs and the Linux distribution you’re using. Here are a few common methods: 1. Using the hostname CommandThe simplest way to find the hostname of your Linux system is to use the hostname command in your terminal. Simply open a terminal window and type:1hostnameThis command will display the hostname of your system.For example:12$ hostnamepresto-node-10 2. Using the uname CommandWhile the uname comma...
2023-04-25
Ubuntu 上运行 sudo add-apt-repository 命令时出现 command not found 错误
如果在 Ubuntu 上运行 sudo add-apt-repository 命令时出现 command not found 错误,说明你的系统中缺少 software-properties-common 包。 你可以通过以下命令来安装它: 1sudo apt install software-properties-common 安装完成后,sudo add-apt-repository 命令就可以正常使用了。 该包提供了一些管理 apt 源的实用工具,比如: add-apt-repository - 用于添加 apt 源 remove-apt-repository - 用于删除 apt 源 apt-add-repository - 同上,别名 apt-key - 用于管理 apt key apt-cache gencaches - 用于生成 apt 缓存 许多第三方工具都需要使用到这个包提供的工具来添加它们自己的 apt 源。 (END)
2023-04-27
Ubuntu 20.04 安装 Python3.7
对于 Ubuntu 用户来说,安装 Python 3.7 的最简单方法是使用 deadsnakes PPA。它提供了对 Python 的最新更新和任何其他所需软件包的访问。 安装 Python3.7添加 PPA 源: 1sudo add-apt-repository ppa:deadsnakes/ppa -y 添加完 PPA 源后,需要运行 apt 更新: 1sudo apt update 安装 Python3.7: 1sudo apt install python3.7 验证 Python 版本: 1python3.7 --version 至此,就已经成功安装了 Python 3.7。 如果还想为 Python 3.7 安装额外的工具,例如调试模块、开发者模块、虚拟环境模块、Distutils 模块、lib2to3 工具模块、DBM.GNU 模块和 Tkinter 模块,可以运行以下命令: 1sudo apt install python3.7-dbg python3.7-dev python3.7-venv python3.7-distutils python3.7-lib2...
2024-08-14
Troubleshooting "chattr: Operation not permitted while setting flags"
I’m trying to set an immutable attribute on some files in my home directory, but I keep encountering issues: chattr: Operation not permitted while setting flags on /data/ops/app.conf.12$ chattr +i /data/ops/app.confchattr: Operation not permitted while setting flags on /data/ops/app.confFrom the chattr man page: A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file’s metadata can not be modified, and the file ...