终端机必备工具包
Linux包含大量命令,但是我们从这里选择了37个最重要的命令。 学习这些命令,您将在Linux命令提示符下有更多的家。
下面的列表按字母顺序显示。 命令在列表中的位置并不代表其有用或简单。 有关命令用法的最终字词,请参阅其手册页。 man命令当然在我们的列表中,它是“ manual”的缩写。
1.别名
使用alias命令可以给命令或命令序列指定自己的名称。 然后,您可以输入您的短名称,外壳程序将为您执行命令或命令序列。
别名cls = clear
这将设置一个名为cls的别名。 这将是clear的另一个名称。 当您键入cls时,它将清除屏幕,就像您键入clear一样。 当然,您的别名可以保存一些按键。 但是,如果您经常在Windows和Linux命令行之间切换,则可能会发现自己在不知道您的意思的Linux机器上键入Windows cls命令。 现在它将知道。
别名比该简单示例要复杂得多。 这是一个叫做pf(用于过程查找)的别名,它有点复杂。 请注意在命令序列周围使用引号。 如果命令序列中包含空格,则这是必需的。 该别名使用ps命令列出正在运行的进程,然后通过grep命令将它们通过管道传输。 grep命令在ps的输出中查找与命令行参数$ 1相匹配的条目。
别名pf =“ ps -e | grep $ 1”
如果您想发现快门过程的进程ID(PID),或者想知道快门是否还在运行,则可以使用这样的别名。 键入pf,一个空格和您感兴趣的进程的名称:
pf快门
终端窗口中的别名命令
命令行上定义的别名将随终端窗口消失。 当您关闭它时,它们消失了。 要使您的别名始终可用,请将其添加到主目录中的.bash_aliases文件中。
2.猫
cat命令(“ concatenate”的缩写)将文件的内容列出到终端窗口。 这比在编辑器中打开文件快,而且没有机会意外更改文件。 要读取.bash_log_out文件的内容,请在主目录为当前工作目录时键入以下命令,默认情况下为:
猫.bash_logout
终端窗口中的cat .bash_logout命令
如果文件的长度超过终端窗口中的行数,则文本会以太快的速度拉动,以至于您无法阅读。 您可以通过更少的管道传递cat的输出,以使过程更易于管理。 使用更少的内容,您可以使用向上和向下箭头键,PgUp和PgDn键以及Home和End键在文件中前后滚动。 键入q从更少退出。
猫.bashrc | 减
猫.bashrc | 少在终端窗口
3.光盘
cd命令更改您的当前目录。 换句话说,它将您带到文件系统中的新位置。
如果要切换到当前目录内的目录,则只需键入cd和其他目录的名称即可。
光盘工作
如果要切换到文件系统目录树中其他位置的目录,请在目录路径前加一个/。
光盘/ usr / local / bin
要快速返回您的主目录,请使用〜(波浪号)字符作为目录名称。
光盘〜
这是另一种技巧:您可以使用双点符号..表示当前目录的父目录。 您可以键入以下命令进入目录:
光盘..
假设您在目录中。 父目录以及您当前所在的目录都在其中。要更改为其他目录之一,可以使用..符号来缩短输入的内容。
cd ../游戏
4. chmod
chmod命令在文件或文件夹上设置文件许可权标志。 这些标志定义谁可以读取,写入或执行文件。 当您使用-l(长格式)选项列出文件时,您会看到一串类似于
-rwxrwxrwx
如果第一个字符是-,则该项是文件;如果是-d,则该项是目录。 字符串的其余部分是三组,每组三个字符。 从左开始,前三个代表所有者的文件权限,中间三个代表组的文件权限,最右边的三个字符代表其他人的权限。 在每个集合中,r代表读取,w代表写入,x代表执行。
如果存在r,w或x字符,则授予文件许可权。 如果不存在该字母,而是显示-,则不会授予该文件许可权。
使用chmod的一种方法是将您希望授予的权限提供给所有者,组和其他人,为3位数字。 最左边的数字代表所有者。 中间数字代表组。 最右边的数字代表其他数字。 此处列出了您可以使用的数字及其代表的含义:
0:无权限
1:执行权限
2:写许可
3:写入和执行权限
4:读取权限
5:读取和执行权限
6:读写权限
7:读取,写入和执行权限
查看example.txt文件,我们可以看到所有三组字符都是rwx。 这意味着每个人都拥有对该文件的读取,写入和执行权限。
为所有者设置读取,写入和执行权限(在我们的列表中为7); 对该小组进行读写(在我们的清单中为6); 并读取并执行其他命令(在我们的列表中为5),我们需要在chmod命令中使用数字765:
chmod -R 765 example.txt
要为所有者设置读取,写入和执行(列表中的7)的权限,并为该组和其他用户设置读取和写入(列表中的6)的权限,我们需要在chmod中使用数字766 命令:
chmod 766 example.txt
5.弦
chown命令允许您更改文件的所有者和组所有者。 用ls -l列出example.txt文件,我们可以在文件描述中看到dave dave。 其中的第一个表示文件所有者的名称,在这种情况下为用户dave。 第二个条目显示组所有者的名称也是dave。 创建用户时,每个用户都有一个默认组。 该用户是该组的唯一成员。 这表明该文件未与任何其他用户组共享。
您可以使用chown更改文件的所有者或组,或两者。 您必须提供所有者和组的名称,以:字符分隔。 您将需要使用sudo。 要将dave保留为文件的所有者,但将mary设置为组所有者,请使用以下命令:
须藤chown dave:mary example.txt
要将所有者和组所有者都更改为mary,可以使用以下命令;
须藤chown mary:mary example.txt
要更改文件,使dave再次成为文件所有者和组所有者,请使用以下命令:
sudo chown dave:戴夫example.txt
6.卷曲
curl命令是从统一资源定位器(URL)或Internet地址检索信息和文件的工具。
curl命令可能不作为Linux发行版的标准部分提供。 如果您使用的是Ubuntu或其他基于Debian的发行版,请使用apt-get将此软件包安装到系统上。 在其他Linux发行版上,请改用Linux发行版的程序包管理工具。
须藤apt-get install curl
假设您要从GitHub存储库中检索单个文件。 没有官方支持的方法。 您不得不克隆整个存储库。 但是,使用curl,我们可以自己获取想要的文件。
此命令为我们检索文件。 请注意,您需要使用-o(输出)选项指定要保存的文件的名称。 如果不这样做,文件的内容将在终端窗口中快速滚动,但不会保存到计算机中。
卷曲https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
如果您不想看到下载进度信息,请使用-s(静默)选项。
curl -s https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
7. df
df命令显示计算机的已挂载文件系统上的大小,已用空间和可用空间。
最有用的两个选项是-h(人类可读)和-x(排除)选项。 易于阅读的选项以Mb或Gb而不是以字节为单位显示大小。 exclude选项允许您告诉df对您不感兴趣的文件系统进行打折。例如,使用snap命令安装应用程序时创建的squashfs伪文件系统。
df -h -x壁球
相关:如何从Linux终端查看可用磁盘空间和磁盘使用情况
8.差异
diff命令比较两个文本文件并显示它们之间的差异。 有许多选项可根据您的需求定制显示。
-y(并排)选项可并排显示行差异。 -w(宽度)选项使您可以指定用于避免回绕行的最大行宽。 在此示例中,这两个文件称为alpha1.txt和alpha2.txt。 --suppress-common-lines可防止diff列出匹配的行,从而使您可以专注于具有差异的行。
diff -y -W 70 alpha1.txt alpha2.txt --suppress-common-lines
相关:如何在Linux终端中比较两个文本文件
9.回声
echo命令将文本字符串打印(回显)到终端窗口。
下面的命令将在终端窗口上打印单词“ A string of text”。
回声文本字符串
echo命令可以显示环境变量的值,例如$ USER,$ HOME和$ PATH环境变量。 它们包含用户名,用户的主目录以及用户在命令行上键入内容时搜索匹配命令的路径的值。
回声$ USER
回声$ HOME
回声$ PATH
以下命令将导致发出哔哔声。 -e(转义代码)选项将转义的字符解释为“响铃”字符。
回声-e“ \ a”
echo命令在Shell脚本中也非常有用。 脚本可以使用此命令生成可见的输出,以指示脚本执行时的进度或结果。
10.退出
exit命令将关闭终端窗口,结束Shell脚本的执行,或退出SSH远程访问会话。
出口
11.找到
如果您不记得将文件放在何处,请使用find命令来查找已知存在的文件。 您必须告诉查找从哪里开始搜索以及它在寻找什么。 在此示例中,。 匹配当前文件夹,并且-name选项告诉find查找名称与搜索模式匹配的文件。
您可以使用通配符,其中*表示任意字符序列,? 代表任何单个字符。 我们正在使用* one *来匹配任何包含序列“ ones”的文件名。这将匹配骨头,石头和寂寞之类的词。
找 。 名称* ones *
如我们所见,find返回了匹配项列表。 其中之一是名为Ramones的目录。 我们可以告诉find将搜索限制为仅文件。 我们使用带有-f选项的-type选项来执行此操作。 f参数代表文件。
找 。 -type f -name * ones *
如果希望搜索不区分大小写,请使用-iname(不敏感名称)选项。
找 。 -iname *野生*
12.手指
finger命令为您提供了有关用户的简短信息,包括用户上次登录的时间,用户的主目录和用户帐户的全名。
13.免费
free命令为您提供计算机内存使用情况的摘要。 它对主随机存取存储器(RAM)和交换存储器都执行此操作。 -h(人类)选项用于提供人类友好的数字和单位。 没有此选项,数字以字节为单位。
免费-h
14. grep
grep实用程序搜索包含搜索模式的行。 当我们查看alias命令时,我们使用grep来搜索另一个程序ps的输出。 grep命令还可以搜索文件的内容。 在这里,我们正在当前目录的所有文本文件中搜索“ train”一词。
grep培训* .txt
输出列出文件名并显示匹配的行。 匹配的文本突出显示。
grep的功能和纯粹的用途绝对可以保证您查看其手册页。
15.组
groups命令告诉您用户是哪个组的成员。
团体戴夫
玛丽小组
16. gzip
gzip命令压缩文件。 默认情况下,它将删除原始文件,并保留压缩版本。 要同时保留原始版本和压缩版本,请使用-k(保留)选项。
gzip -k core.c
17.头
head命令为您提供文件的前10行的列表。 如果您希望看到更少或更多的行,请使用-n(数字)选项。 在此示例中,我们使用head,其默认值为10行。 然后,我们重复该命令,仅询问五行。
头-c
头-n 5 core.c
18.历史
history命令列出了您先前在命令行上发布的命令。 您可以通过键入感叹号来重复历史记录中的任何命令! 以及历史记录列表中的命令编号。
!188
键入两个感叹号将重复您先前的命令。
!!
19.杀死
kill命令允许您从命令行终止进程。 通过提供要杀死的进程的进程ID(PID)来执行此操作。 不要轻易杀死进程。 您需要有充分的理由这样做。 在此示例中,我们假装快门程序已锁定。
要找到快门的PID,我们将使用上面关于别名命令的部分中的ps和grep技巧。 我们可以搜索快门过程并获取其PID,如下所示:
ps -e | grep快门。
一旦确定了PID(在这种情况下为1692),就可以按以下步骤将其杀死:
杀死1692
20.更少
less命令使您无需打开编辑器即可查看文件。 它使用起来速度更快,而且您不会无意间修改文件。 使用更少的光标,您可以使用向上和向下箭头键,PgUp和PgDn键以及Home和End键在文件中前后滚动。 按Q键退出少。
要查看文件,请为其提供以下名称:
少core.c
您还可以将其他命令的输出通过管道传递到更少的命令中。 要查看ls的输出以获取整个硬盘的列表,请使用以下命令:
ls -R / | 减
使用/向前搜索文件并使用? 向后搜索。
21. ls
这可能是大多数Linux用户遇到的第一个命令。 它列出了您指定目录中的文件和文件夹。 缺省情况下,ls查找当前目录。 ls可以使用很多选项,我们强烈建议您查看其手册页。 这里提供一些常见的示例。
要列出当前目录中的文件和文件夹:
ls
要使用详细列表列出当前目录中的文件和文件夹,请使用-l(长)选项:
ls -l
要使用对人类友好的文件大小,请包括-h(人类)选项:
ls -lh
要包含隐藏文件,请使用-a(所有文件)选项:
ls -lha
22.男人
man命令在less中显示命令的“手册页”。 手册页是该命令的用户手册。 由于man较少使用来显示手册页,因此可以使用较少的搜索功能。
例如,要查看chown的手册页,请使用以下命令:
男子长发
使用向上和向下箭头或PgUp和PgDn键滚动浏览文档。 按q退出手册页或按h寻求帮助。
23. mkdir
mkdir命令允许您在文件系统中创建新目录。 您必须将新目录的名称提供给mkdir。 如果新目录将不在当前目录内,则必须提供新目录的路径。
要在当前目录中创建两个名为“****”和“报价”的新目录,请使用以下两个命令:
mkdir****
mkdir报价
要在“****”目录中创建一个名为“ 2019”的新目录,请使用以下命令:
mkdir****/ 2109
如果要创建目录,但是其父目录不存在,则可以使用-p(父项)选项来使mkdir也创建所有必需的父目录。 在以下命令中,我们将在“ quotes”目录中的“ yearly”目录中创建“ 2019”目录。 “ yearly”目录不存在,但是我们可以让mkdir一次创建所有指定的目录:
mkdir -p行情/年度/ 2019
还创建了“每年”目录。
24.MV
mv命令允许您在目录之间移动文件和目录。 它还允许您重命名文件。
要移动文件,您必须告诉mv该文件在哪里以及要将其移动到何处。 在此示例中,我们从“〜/ Document / Ukulele”目录中移动了一个名为apache.pdf的文件,并将其放置在当前目录中,该目录由single表示。 字符。
mv〜/ Documents / Ukulele / Apache.pdf。
要重命名文件,您可以将其“移动”到具有新名称的新文件中。
mv Apache.pdf The_Shadows_Apache.pdf
只需一步就可以完成文件移动和重命名操作:
mv〜/ Documents / Ukulele / Apache.pdf ./The_Shadows_Apache.pdf
25. passwd
passwd命令使您可以更改用户密码。 只需键入passwd即可更改您自己的密码。
您也可以更改其他用户帐户的密码,但是必须使用sudo。 系统将要求您输入两次新密码。
须藤密码玛丽
26.平
ping命令使您可以验证与其他网络设备的网络连接。 它通常用于帮助解决网络问题。 要使用ping,请提供其他设备的IP地址或计算机名称。
ping 192.168.4.18
ping命令将一直运行,直到您使用Ctrl + C停止它为止。
在终端窗口中ping
这是这里发生的事情:
IP地址为192.168.4.18的设备正在响应我们的ping请求,并正在发送回64字节的数据包。
Internet控制消息协议(ICMP)的序列号使我们能够检查丢失的响应(丢失的数据包)。
TTL数字是数据包的“生存时间”。 数据包每次经过路由器时,(假设)递减一。 如果达到零,则将数据包丢弃。 这样做的目的是防止网络环回问题淹没网络。
时间值是从计算机到设备再返回的往返时间。 简单地说,这次越低越好。
要要求ping运行特定次数的ping尝试,请使用-c(计数)选项。
ping -c 5 192.168.4.18
要听到ping的声音,请使用-a(声音)选项。
ping -a 192.168.4.18
27.ps
ps命令列出了正在运行的进程。 使用不带任何选项的ps会导致它列出当前shell中正在运行的进程。
ps
终端窗口中的ps命令
要查看与特定用户有关的所有进程,请使用-u(用户)选项。 这可能是一长串的清单,因此为了方便起见,请尽量减少传递。
ps -u dave | 减
终端窗口中的ps命令
要查看正在运行的每个进程,请使用-e(每个进程)选项:
ps -e | 减
28。
pwd命令非常好用,很简单,它从根目录/目录打印工作目录(当前目录)。
密码
终端窗口中的pwd命令
29.关机
shutdown命令使您可以关闭或重新引导Linux系统。
使用不带参数的关机将在一分钟内关闭计算机。
关掉
终端窗口中的关闭命令
要立即关闭,请使用now参数。
现在关闭
现在关闭
您还可以计划关闭,并将挂起的关闭通知所有已登录的用户。 要让关闭命令知道何时关闭,请为其提供时间。 从现在起可以是固定的分钟数,例如+90,也可以是精确的时间,例如23:00。 您提供的任何文本消息都会广播给已登录的用户。
23:00关机今晚23:00关机,保存您的工作并在此之前注销!
使用消息关闭23:00
要取消关闭,请使用-c(取消)选项。 在这里,我们计划从现在开始关闭15分钟,然后改变了主意。
关闭+15在15分钟内关闭!
关闭-c
关闭-c取消命令
相关文章如何使用命令行重新启动或关闭Linux
30. SSH
使用ssh命令建立与远程Linux计算机的连接并登录到您的帐户。 要建立连接,您必须提供您的用户名以及远程计算机的IP地址或域名。 在此示例中,用户mary以192.168.4.23登录到计算机。 建立连接后,将要求她输入密码。
ssh玛丽@ 192.168.4.23
终端窗口中的ssh命令
她的用户名和密码已经过验证并被接受,并且已经登录。请注意,她的提示已从“ Nostromo”更改为“ howtogeek”。
Mary发出w命令以列出“ howtogeek”系统上的当前用户。 她被列为从pts / 1(是伪终端从站)连接的。 也就是说,它不是直接连接到计算机的终端。
要关闭会话,mary类型将退出并返回到“ Nostromo”计算机上的外壳程序。
w
出口
31.须藤
当执行需要root或超级用户权限的操作(例如更改另一个用户的密码)时,需要sudo命令。
须藤密码玛丽
32.尾巴
tail命令为您提供文件的最后10行的列表。 如果您希望看到更少或更多的行,请使用-n(数字)选项。 在此示例中,我们使用tail,其默认值为10行。 然后,我们重复该命令,仅询问五行。
尾核
尾-n 5 core.c
33.焦油
使用tar命令,您可以创建一个可以包含许多其他文件的归档文件(也称为tarball)。 这使得分发文件集合更加方便。 您也可以使用tar从存档文件中提取文件。 通常要求tar压缩档案。 如果您不要求压缩,则归档文件将创建为未压缩状态。
要创建档案文件,您需要告诉tar档案文件中应包含哪些文件,以及您希望档案文件具有的名称。
在此示例中,用户将在当前目录的Ukulele目录中存档所有文件。
终端窗口中的ls命令
他们使用了-c(创建)选项和-v(详细)选项。 verbose选项通过将文件添加到存档中时在终端窗口中列出文件,从而提供一些视觉反馈。 -f(文件名)选项后跟所需的档案名称。 在这种情况下,它是songs.tar。
tar -cvf songs.tar夏威夷四弦琴/
终端窗口中的tar -cvf命令
将文件添加到存档文件时,这些文件将列在终端窗口中。
有两种方法可以告诉tar您希望压缩存档文件。 首先是-z(gzip)选项。 这告诉tar一旦创建存档,便使用gzip实用程序压缩存档。
通常将“ .gz”作为后缀添加到此类存档中。 这样一来,从其中提取文件的任何人都可以知道要传递给tar的命令以正确检索文件。
tar -cvzf songs.tar.gz夏威夷四弦琴/
终端窗口中的tar -cvzf命令
文件像以前一样被添加到存档文件中时,会在终端窗口中列出,但是由于压缩所需的时间,存档的创建将花费一些时间。
要创建使用高级压缩算法压缩的存档文件,从而提供较小的存档文件,请使用-j(bzip2)选项。
tar -cvjf songs.tar.bz2夏威夷四弦琴/
终端窗口中的tar -cvjf命令
再次,在创建归档文件时列出文件。 -j选项明显比-z选项慢。
如果要归档大量文件,则必须在-z选项(适当的压缩和合理的速度)之间选择,或者在-j选项(更好的压缩和较慢的速度)之间进行选择。
从下面的屏幕快照中可以看出,“。tar”文件是最大的文件,“。tar.gz”是较小的文件,而“ .tar.bz2”是最小的文件。
终端窗口中的ls命令
要从存档文件中提取文件,请使用-x(提取)选项。 -v(详细)和-f(文件名)选项的行为与创建档案时的行为相同。 使用ls确认要从中提取文件的档案类型,然后发出以下命令。
ls
tar -xvf songs.tar
终端窗口中的ls和tar -xvf命令
文件在提取时列出。 请注意,还将为您重新创建Ukulele目录。
要从“ .tar.gz”档案中提取文件,请使用-z(gzip)选项。
tar -xvzf songs.tar.gz
终端窗口中的tar -xvzf命令
最后,要从“ .tar.bz2”存档中提取文件,请使用-j选项而不是-z(gzip)选项。
tar -xvjf songs.tar.bz2
终端窗口中的tar -xvjf命令
相关文章如何在Linux上从.tar.gz或.tar.bz2文件提取文件
34.顶部
top命令向您实时显示与Linux计算机有关的数据。 屏幕顶部是状态摘要。
第一行向您显示计算机的运行时间,运行时间,登录的用户数量以及过去一分钟,五分钟和十五分钟的平均负载量。
第二行显示任务的数量及其状态:运行,停止,睡眠和僵尸。
第三行显示CPU信息。 这些字段的含义如下:
us:值是“用户空间”中CPU为用户执行进程所花费的CPU时间。
sy:值是运行系统“内核空间”进程所花费的CPU时间
ni:值是使用手动设置的nice值执行进程所花费的CPU时间
id:是CPU空闲时间的数量
wa:值是CPU等待I / O完成所花费的时间
嗨:处理硬件中断所花费的CPU时间
si:服务于软件中断的CPU时间
st:由于运行虚拟机而导致的CPU时间损失(“窃取时间”)
第四行显示了物理内存的总量,以及空闲,已使用,已缓冲或已缓存的内存量。
第五行显示交换内存的总量,以及空闲,已使用和可用的内存量(考虑了预期可从缓存中恢复的内存)。
终端窗口中的top命令
用户按下E键将显示内容更改为更易于理解的数字,而不是代表字节的长整数。
主显示中的列由以下组成:
PID:进程ID
USER:流程所有者的名称
PR:流程优先
NI:这个过程的价值
VIRT:进程使用的虚拟内存
RES:进程使用的常驻内存
SHR:进程使用的共享内存
S:进程状态。 请参阅以下此字段可以采用的值的列表
%CPU:自上次更新以来该进程使用的CPU时间的份额
%MEM:使用的物理内存份额
TIME +:任务使用的总CPU时间,以百分之一秒为单位
COMMAND:命令名称或命令行(名称+选项)
(命令列不适合屏幕截图。)
进程的状态可以是以下之一:
D:不间断的睡眠
R:跑步
S:睡觉
T:已追踪(已停止)
Z:僵尸
按Q键从顶部退出。
相关:如何在Linux上很好地设置进程优先级
35.乌南
您可以使用uname命令获取有关正在使用的Linux计算机的一些系统信息。
使用-a(全部)选项查看所有内容。
使用-s(内核名称)选项查看内核的类型。
使用-r(内核发行版)选项查看内核发行版。
使用-v(内核版本)选项查看内核版本。
优名
uname -s
匿名-r
ame -v
终端窗口中的uname命令
36. w
w命令列出当前登录的用户。
w
终端窗口中的w命令
37. whoami
使用whoami找出您作为谁登录或谁登录到无人Linux终端。
我是谁
终端窗口中的whoami命令
相关:如何确定Linux中的当前用户帐户
这就是您的工具包
学习Linux就像学习其他东西一样。 在熟悉这些命令之前,您需要进行一些练习。 一旦掌握了这些命令,就可以熟练掌握技能。
有个老笑话-大概和Unix一样古老-说您需要知道的唯一命令是man命令。 有一丝事实真相,但是有些手册页没有介绍就无法理解。 本教程应该为您提供所需的介绍。
原译文
The Essential Toolkit for the Terminal
Linux includes a large number of commands, but we’ve chosen 37 of the most important ones to present here. Learn these commands, and you’ll be much more at home at the Linux command prompt.
The below list is presented in alphabetical order. A command’s position in the list is not representative of its usefulness or simplicity. For the final word on a command’s usage, refer to its man pages. The man command is in our list, of course—it’s short for “manual.”
1. alias
The alias command lets you give your own name to a command or sequence of commands. You can then type your short name, and the shell will execute the command or sequence of commands for you.
alias cls=clear
This sets up an alias called cls . It will be another name for clear . When you type cls, it will clear the screen just as though you had typed clear . Your alias saves a few keystrokes, sure. But, if you frequently move between Windows and Linux command line, you can find yourself typing the Windows cls command on a Linux machine that doesn’t know what you mean. Now it will know.
Aliases can be much more intricate than that simple example. Here’s an alias called pf (for process find) that is just a little more complex. Note the use of quotation marks around the command sequence. This is required if the command sequence has spaces in it. This alias uses the ps command to list the running processes and then pipes them through the grep command. The grep command looks for entries in the output from ps that match the command line parameter $1 .
alias pf="ps -e | grep $1"
If you wanted to discover the process ID (PID) of the shutter process—or to find out if shutter was even running—you could use the alias like this. Type pf, a space, and the name of the process you are interested in:
pf shutter
alias command in terminal window
Aliases defined on the command line will die with the terminal window. When you close it, they are gone. To make your aliases always be available to you, add them to the.bash_aliases file in your home directory.
2. cat
The cat command (short for “concatenate”) lists the contents of files to the terminal window. This is faster than opening the file in an editor, and there’s no chance you can accidentally alter the file. To read the contents of your .bash_log_out file, type the following command while the home directory is your current working directory, as it is by default:
cat .bash_logout
cat .bash_logout command in a terminal window
With files longer than the number of lines in your terminal window, the text will whip past too fast for you to read. You can pipe the output from cat through less to make the process more manageable. With less you can scroll forward and backward through the file using the Up and Down Arrow keys, the PgUp and PgDn keys, and the Home and End keys. Type q to quit from less.
cat .bashrc | less
cat .bashrc | Less in a terminal window
3. cd
The cd command changes your current directory. In other words, it moves you to a new place in the filesystem.
If you are changing to a directory that is within your current directory, you can simply type cd and the name of the other directory.
cd work
If you are changing to a directory elsewhere within the filesystem directory tree, provide the path to the directory with a leading /.
cd /usr/local/bin
To quickly return to your home directory, use the ~ (tilde) character as the directory name.
cd ~
Here’s another trick: You can use the double dot symbol .. to represent the parent of the current directory. You can type the following command to go up a directory:
cd ..
Imagine you are in a directory. The parent directory has other directories in it, as well as the directory you’re currently in. To change into one of those other directories, you can use the .. symbol to shorten what you have to type.
cd ../games
4. chmod
The chmod command sets the file permissions flags on a file or folder. The flags define who can read, write to or execute the file. When you list files with the -l (long format) option you’ll see a string of characters that look like
-rwxrwxrwx
If the first character is a - the item is a file, if it is a d the item is a directory. The rest of the string is three sets of three characters. From the left, the first three represent the file permissions of the owner, the middle three represent the file permissions of the group and the rightmost three characters represent the permissions for others. In each set, an r stands for read, a w stands for write, and an x stands for execute.
If the r, w, or x character is present that file permission is granted. If the letter is not present and a - appears instead, that file permission is not granted.
One way to use chmod is to provide the permissions you wish to give to the owner, group, and others as a 3 digit number. The leftmost digit represents the owner. The middle digit represents the group. The rightmost digit represents the others. The digits you can use and what they represent are listed here:
0: No permission
1: Execute permission
2: Write permission
3: Write and execute permissions
4: Read permission
5: Read and execute permissions
6: Read and write permissions
7: Read, write and execute permissions
Looking at our example.txt file, we can see that all three sets of characters are rwx. That means everyone has read, write and execute rights with the file.
To set the permission to be read, write, and execute (7 from our list) for the owner; read and write (6 from our list) for the group; and read and execute (5 from our list) for the others we’d need to use the digits 765 with the chmod command:
chmod -R 765 example.txt
To set the permission to be read, write and execute (7 from our list) for the owner, and read and write (6 from our list) for the group and for the others we’d need to use the digits 766 with the chmod command:
chmod 766 example.txt
5. chown
The chown command allows you to change the owner and group owner of a file. Listing our example.txt file with ls -l we can see dave dave in the file description. The first of these indicates the name of the file owner, which in this case is the user dave. The second entry shows that the name of the group owner is also dave. Each user has a default group created when the user is created. That user is the only member of that group. This shows that the file is not shared with any other groups of users.
You can use chown to change the owner or group, or both of a file. You must provide the name of the owner and the group, separated by a : character. You will need to use sudo. To retain dave as the owner of the file but to set mary as the group owner, use this command:
sudo chown dave:mary example.txt
To change both the owner and the group owner to mary, you would use the following command;
sudo chown mary:mary example.txt
To change the file so that dave is once more the file owner and the group owner, use this command:
sudo chown dave:dave example.txt
6. curl
The curl command is a tool to retrieve information and files from Uniform Resource Locators (URLs) or internet addresses.
The curl command may not be provided as a standard part of your Linux distribution. Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.
sudo apt-get install curl
Suppose you want to retrieve a single file from a GitHub repository. There is no officially supported way to this. You’re forced to clone the entire repository. With curl however, we can retrieve the file we want on its own.
This command retrieves the file for us. Note that you need to specify the name of the file to save it in, using the -o (output) option. If you do not do this, the contents of the file are scrolled rapidly in the terminal window but not saved to your computer.
curl https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
If you don’t want to see the download progress information use the -s (silent) option.
curl -s https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
7. df
The df command shows the size, used space, and available space on the mounted filesystems of your computer.
Two of the most useful options are the -h (human readable) and -x (exclude) options. The human-readable option displays the sizes in Mb or Gb instead of in bytes. The exclude option allows you to tell df to discount filesystems you are not interested in. For example, the squashfs pseudo-filesystems that are created when you install an application with the snap command.
df -h -x squashfs
RELATED: How to View Free Disk Space and Disk Usage From the Linux Terminal
8. diff
The diff command compares two text files and shows the differences between them. There are many options to tailor the display to your requirements.
The -y (side by side) option shows the line differences side by side. The -w (width) option lets you specify the maximum line width to use to avoid wraparound lines. The two files are called alpha1.txt and alpha2.txt in this example. The --suppress-common-lines prevents diff from listing the matching lines, letting you focus on the lines which have differences.
diff -y -W 70 alpha1.txt alpha2.txt --suppress-common-lines
RELATED: How to Compare Two Text Files in the Linux Terminal
9. echo
The echo command prints (echoes) a string of text to the terminal window.
The command below will print the words “A string of text” on the terminal window.
echo A string of text
The echo command can show the value of environment variables, for example, the $USER, $HOME, and $PATH environment variables. These hold the values of the name of the user, the user’s home directory, and the path searched for matching commands when the user types something on the command line.
echo $USER
echo $HOME
echo $PATH
The following command will cause a bleep to be issued. The -e (escape code) option interprets the escaped a character as a ‘bell’ character.
echo -e "\a"
The echo command is also invaluable in shell scripts. A script can use this command to generate visible output to indicate the progress or results of the script as it is executed.
10. exit
The exit command will close a terminal window, end the execution of a shell script, or log you out of an SSH remote access session.
exit
11. find
Use the find command to track down files that you know exist if you can’t remember where you put them. You must tell find where to start searching from and what it is looking for. In this example, the . matches the current folder and the -name option tells find to look for files with a name that matches the search pattern.
You can use wildcards, where * represents any sequence of characters and ? represents any single character. We’re using *ones* to match any file name containing the sequence “ones.” This would match words like bones, stones, and lonesome.
find . -name *ones*
As we can see, find has returned a list of matches. One of them is a directory called Ramones. We can tell find to restrict the search to files only. We do this using the -type option with the f parameter. The f parameter stands for files.
find . -type f -name *ones*
If you want the search to be case insensitive use the -iname (insensitive name) option.
find . -iname *wild*
12. finger
The finger command gives you a short dump of information about a user, including the time of the user’s last login, the user’s home directory, and the user account’s full name.
13. free
The free command gives you a summary of the memory usage with your computer. It does this for both the main Random Access Memory (RAM) and swap memory. The -h (human) option is used to provide human-friendly numbers and units. Without this option, the figures are presented in bytes.
free -h
14. grep
The grep utility searches for lines which contain a search pattern. When we looked at the alias command, we used grep to search through the output of another program, ps . The grep command can also search the contents of files. Here we’re searching for the word “train” in all text files in the current directory.
grep train *.txt
The output lists the name of the file and shows the lines that match. The matching text is highlighted.
The functionality and sheer usefulness of grep definitely warrants you checking out its man page.
15. groups
The groups command tells you which groups a user is a member of.
groups dave
groups mary
16. gzip
The gzip command compresses files. By default, it removes the original file and leaves you with the compressed version. To retain both the original and the compressed version, use the -k (keep) option.
gzip -k core.c
17. head
The head command gives you a listing of the first 10 lines of a file. If you want to see fewer or more lines, use the -n (number) option. In this example, we use head with its default of 10 lines. We then repeat the command asking for only five lines.
head -core.c
head -n 5 core.c
18. history
The history command lists the commands you have previously issued on the command line. You can repeat any of the commands from your history by typing an exclamation point ! and the number of the command from the history list.
!188
Typing two exclamation points repeats your previous command.
!!
19. kill
The kill command allows you to terminate a process from the command line. You do this by providing the process ID (PID) of the process to kill. Don’t kill processes willy-nilly. You need to have a good reason to do so. In this example, we’ll pretend the shutter program has locked up.
To find the PID of shutter we’ll use our ps and grep trick from the section about the alias command, above. We can search for the shutter process and obtain its PID as follows:
ps -e | grep shutter.
Once we have determined the PID—1692 in this case—we can kill it as follows:
kill 1692
20. less
The less command allows you to view files without opening an editor. It’s faster to use, and there’s no chance of you inadvertently modifying the file. With less you can scroll forward and backward through the file using the Up and Down Arrow keys, the PgUp and PgDn keys and the Home and End keys. Press the Q key to quit from less.
To view a file provide its name to less as follows:
less core.c
You can also pipe the output from other commands into less. To see the output from ls for a listing of your entire hard drive, use the following command:
ls -R / | less
Use / to search forward in the file and use ? to search backward.
21. ls
This might be the first command the majority of Linux users meet. It lists the files and folders in the directory you specify. By default, ls looks in the current directory. There are a great many options you can use with ls , and we strongly advise reviewing its the man page. Some common examples are presented here.
To list the files and folders in the current directory:
ls
To list the files and folders in the current directory with a detailed listing use the -l (long) option:
ls -l
To use human-friendly file sizes include the -h (human) option:
ls -lh
To include hidden files use the -a (all files) option:
ls -lha
22. man
The man command displays the “man pages” for a command in less . The man pages are the user manual for that command. Because man uses less to display the man pages, you can use the search capabilities of less.
For example, to see the man pages for chown, use the following command:
man chown
Use the Up and Down arrow or PgUp and PgDn keys to scroll through the document. Press q to quit the man page or pressh for help.
23. mkdir
The mkdir command allows you to create new directories in the filesystem. You must provide the name of the new directory to mkdir. If the new directory is not going to be within the current directory, you must provide the path to the new directory.
To create two new directories in the current directory called “invoices” and “quotes,” use these two commands:
mkdir invoices
mkdir quotes
To create a new directory called “2019” inside the “invoices” directory, use this command:
mkdir invoices/2109
If you are going to create a directory, but its parent directory does not exist, you can use the -p (parents) option to have mkdir create all of the required parent directories too. In the following command, we are creating the “2019” directory inside the “yearly” directory inside the “quotes” directory. The “yearly” directory does not exist, but we can have mkdir create all the specified directories at once:
mkdir -p quotes/yearly/2019
The “yearly” directory is also created.
24. mv
The mv command allows you to move files and directories from directory to directory. It also allows you to rename files.
To move a file you must tell mv where the file is and where you want it to be moved to. In this example, we’re moving a file called apache.pdf from the “~/Document/Ukulele” directory and placing it in the current directory, represented by the single . character.
mv ~/Documents/Ukulele/Apache.pdf .
To rename the file, you “move” it into a new file with the new name.
mv Apache.pdf The_Shadows_Apache.pdf
The file move and rename action could have been achieved in one step:
mv ~/Documents/Ukulele/Apache.pdf ./The_Shadows_Apache.pdf
25. passwd
The passwd command lets you change the password for a user. Just type passwd to change your own password.
You can also change the password of another user account, but you must use sudo. You will be asked to enter the new password twice.
sudo passwd mary
26. ping
The ping command lets you verify that you have network connectivity with another network device. It is commonly used to help troubleshoot networking issues. To use ping, provide the IP address or machine name of the other device.
ping 192.168.4.18
The ping command will run until you stop it with Ctrl+C.
ping in a terminal window
Here’s what’s going on here:
The device at IP address 192.168.4.18 is responding to our ping requests and is sending back packets of 64 bytes.
The Internet Control Messaging Protocol (ICMP) sequence numbering allows us to check for missed responses (dropped packets).
The TTL figure is the “time to live” for a packet. Each time the packet goes through a router, it is (supposed to be) decremented by one. If it reaches zero the packet is thrown away. The aim of this is to prevent network loopback problems from flooding the network.
The time value is the duration of the round trip from your computer to the device and back. Simply put, the lower this time, the better.
To ask ping to run for a specific number of ping attempts, use the -c (count) option.
ping -c 5 192.168.4.18
To hear a ping, use the -a (audible) option.
ping -a 192.168.4.18
27. ps
The ps command lists running processes. Using ps without any options causes it to list the processes running in the current shell.
ps
ps command in a terminal window
To see all the processes related to a particular user, use the -u (user) option. This is likely to be a long list, so for convenience pipe it through less.
ps -u dave | less
ps command in a terminal window
To see every process that is running, use the -e (every process) option:
ps -e | less
28. pwd
Nice and simple, the pwd command prints the working directory (the current directory) from the root / directory.
pwd
pwd command in a terminal window
29. shutdown
The shutdown command lets you shut down or reboot your Linux system.
Using shutdown with no parameters will shut down your computer in one minute.
shutdown
shutdown command in a terminal window
To shut down immediately, use the now parameter.
shutdown now
shutdown now
You can also schedule a shutdown and inform any logged in users of the pending shutdown. To let the shutdown command know when you want it to shut down, you provide it with a time. This can be a set number of minutes from now, such as +90 or a precise time, like 23:00. Any text message you provide is broadcast to logged in users.
shutdown 23:00 Shutdown tonight at 23:00, save your work and log out before then!
shutdown 23:00 with message
To cancel a shutdown, use the -c (cancel) option. Here we have scheduled a shutdown for fifteen minutes time from now—and then changed our minds.
shutdown +15 Shutting down in 15 minutes!
shutdown -c
Shutdown -c cancel command
RELATED: How to Reboot or Shut Down Linux Using the Command Line
30. SSH
Use the ssh command to make a connection to a remote Linux computer and log into your account. To make a connection, you must provide your user name and the IP address or domain name of the remote computer. In this example, the user mary is logging into the computer at 192.168.4.23. Once the connection is established, she is asked for her password.
ssh mary@192.168.4.23
ssh command in a terminal window
Her user name and password are verified and accepted, and she is logged in. Notice that her prompt has changed from “Nostromo” to “howtogeek.”
Mary issues the w command to list the current users on “howtogeek” system. She is listed as being connected from pts/1, which is a pseudo-terminal slave. That is, it is not a terminal directly connected to the computer.
To close the session, mary types exit and is returned to the shell on the “Nostromo” computer.
w
exit
31. sudo
The sudo command is required when performing actions that require root or superuser permissions, such as changing the password for another user.
sudo passwd mary
32. tail
The tail command gives you a listing of the last 10 lines of a file. If you want to see fewer or more lines, use the -n (number) option. In this example, we use tail with its default of 10 lines. We then repeat the command asking for only five lines.
tail core.c
tail -n 5 core.c
33. tar
With the tar command, you can create an archive file (also called a tarball) that can contain many other files. This makes it much more convenient to distribute a collection of files. You can also use tar to extract the files from an archive file. It is common to ask tar to compress the archive. If you do not ask for compression, the archive file is created uncompressed.
To create an archive file, you need to tell tar which files to include in the archive file, and the name you wish the archive file to have.
In this example, the user is going to archive all of the files in the Ukulele directory, which is in the current directory.
ls command in the terminal window
They have used the -c (create) option and the -v (verbose) option. The verbose option gives some visual feedback by listing the files to the terminal window as they are added to the archive. The -f (filename) option is followed by the desired name of the archive. In this case, it is songs.tar.
tar -cvf songs.tar Ukulele/
tar -cvf command in a terminal window
The files are listed to the terminal window as they are added to the archive file.
There are two ways to tell tar that you want the archive file to be compressed. The first is with the -z (gzip) option. This tells tar to use the gzip utility to compress the archive once it has been created.
It is usual to add “.gz” as suffix to this type of archive. That allows anyone who is extracting files from it to know which commands to pass to tar to correctly retrieve the files.
tar -cvzf songs.tar.gz Ukulele/
tar -cvzf command in a terminal window
The files are listed to the terminal window as they are added to the archive file as before, but the creation of the archive will take a little longer because of the time required for the compression.
To create an archive file that is compressed using a superior compression algorithm giving a smaller archive file use the -j (bzip2) option.
tar -cvjf songs.tar.bz2 Ukulele/
tar -cvjf command in a terminal window
Once again, the files are listed as the archive is created. The -j option is noticeably slower than the -z option.
If you are archiving a great many files, you must choose between the -z option for decent compression and reasonable speed, or the -j option for better compression and slower speed.
As can be seen in the screenshot below, the “.tar” file is the largest, the “.tar.gz” is smaller, and the “.tar.bz2” is the smallest of the archives.
ls command in a terminal window
To extract files from an archive file use the -x (extract) option. The -v (verbose) and -f (filename) options behave as they do when creating archives. Use ls to confirm which type of archive you are going to extract the files from, then issue the following command.
ls
tar -xvf songs.tar
ls and tar -xvf commands in a terminal window
The files are listed as they are extracted. Note that the Ukulele directory is also recreated for you.
To extract files from a “.tar.gz” archive, use the -z (gzip) option.
tar -xvzf songs.tar.gz
tar -xvzf command in a terminal window
Finally, to extract files from a “.tar.bz2” archive use the -j option instead of the -z (gzip) option.
tar -xvjf songs.tar.bz2
tar -xvjf command in a terminal window
RELATED: How to Extract Files From a .tar.gz or .tar.bz2 File on Linux
34. top
The top command shows you a real-time display of the data relating to your Linux machine. The top of the screen is a status summary.
The first line shows you the time and how long your computer has been running for, how many users are logged into it, and what the load average has been over the past one, five, and fifteen minutes.
The second line shows the number of tasks and their states: running, stopped, sleeping and zombie.
The third line shows CPU information. Here’s what the fields mean:
us: value is the CPU time the CPU spends executing processes for users, in “user space”
sy: value is the CPU time spent on running system “kernel space” processes
ni: value is the CPU time spent on executing processes with a manually set nice value
id: is the amount of CPU idle time
wa: value is the time the CPU spends waiting for I/O to complete
hi: The CPU time spent servicing hardware interrupts
si: The CPU time spent servicing software interrupts
st: The CPU time lost due to running virtual machines (“steal time”)
The fourth line shows the total amount of physical memory, and how much is free, used and buffered or cached.
The fifth line shows the total amount of swap memory, and how much is free, used and available (taking into account memory that is expected to be recoverable from caches).
top command in a terminal window
The user has pressed the E key to change the display into more humanly digestible figures instead of long integers representing bytes.
The columns in the main display are made up of:
PID: Process ID
USER: Name of the owner of the process
PR: Process priority
NI: The nice value of the process
VIRT: Virtual memory used by the process
RES: Resident memory used by the process
SHR: Shared memory used by the process
S: Status of the process. See the list below of the values this field can take
%CPU: the share of CPU time used by the process since last update
%MEM: share of physical memory used
TIME+: total CPU time used by the task in hundredths of a second
COMMAND: command name or command line (name + options)
(The command column didn’t fit into the screenshot.)
The status of the process can be one of:
D: Uninterruptible sleep
R: Running
S: Sleeping
T: Traced (stopped)
Z: Zombie
Press the Q key to exit from top.
RELATED: How to Set Process Priorities With nice and renice on Linux
35. uname
You can obtain some system information regarding the Linux computer you’re working on with the uname command.
Use the -a (all) option to see everything.
Use the -s (kernel name) option to see the type of kernel.
Use the -r (kernel release) option to see the kernel release.
Use the -v (kernel version) option to see the kernel version.
uname -a
uname -s
uname -r
uname -v
uname command in a terminal window
36. w
The w command lists the currently logged in users.
w
w command in a terminal window
37. whoami
Use whoami to find out who you are logged in as or who is logged into an unmanned Linux terminal.
whoami
whoami command in a terminal window
RELATED: How to Determine the Current User Account in Linux
That’s Your Toolkit
Learning Linux is like learning anything else. You’re going to need some practice before become familiar with these commands. Once you have these commands at your fingertips, you’ll be well along the path to proficiency.
There’s an old joke—probably as old as Unix itself—that says the only command you need to know is the man command. There’s a glimmer of truth in that, but some of the man pages are impenetrable without an introduction. This tutorial should give you the introduction you need.