GNU/Linux >> Linux の 問題 >  >> Linux

初心者向けLinux:ファイルを操作するためのさらに10個のコマンド

この2部構成のシリーズの最初のセグメントを見逃した場合のために、ターミナルで開始するための10個のコマンドを調べました。次に、コマンドラインの旅を続けるために使用できる10個の追加コマンドについて説明します。具体的には、ファイルを操作するために使用されるコマンドを調べたいと思います。ファイルのコピー、移動、名前の変更に加えて、さまざまな方法でファイルを読み取るためのいくつかのコマンドを使用します。

知っておくべきコマンド

1。 cp file1 file2

コピー コマンドは、あるファイルの内容を別のファイルにコピーするために使用されます。このコマンドを使用して、あるディレクトリから別のディレクトリにファイルをコピーすることもできます。

[tcarrigan@server community_content]$ ls -l
total 36
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article2
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article3
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article4
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Apr  7  2020 podman_pulling
-rw-rw-r--. 1 tcarrigan tcarrigan 8824 Apr  7  2020 real_sysadmins
[tcarrigan@server community_content]$ cp podman_pulling article
[tcarrigan@server community_content]$ ls -l
total 40
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:11 article
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article2
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article3
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article4
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Apr  7  2020 podman_pulling
-rw-rw-r--. 1 tcarrigan tcarrigan 8824 Apr  7  2020 real_sysadmins

ご覧のとおり、 podman_pullingをコピーしました 記事への記事の内容 ファイル。ここで、このコマンドを使用して実行できるもう1つの方法は、既存のファイルを別のディレクトリにコピーすることです。たとえば、 podman_pullingをコピーしたい場合 私のホームディレクトリへの記事、私はこのようなことをします:

[tcarrigan@server community_content]$ cp podman_pulling /home/tcarrigan/
** Navigate to /home/tcarrigan **
[tcarrigan@server ~]$ ls -l
total 8
drwxrwxr-x. 4 tcarrigan tcarrigan   50 Feb  6  2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Pictures
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 podman_pulling
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Videos

そのpodman_pullingを見ることができます /home/tcarriganで利用できるようになりました ディレクトリ。

[次のこともお勧めします:10個の便利なsystemdコマンド:リファレンス]

2。 mv file1 file2

移動 コマンドを使用すると、ユーザーはファイルを移動または名前変更できます。ファイルを移動するには、次を使用します。

[tcarrigan@server ~]$ mv podman_pulling article_submissions/my_articles/
[tcarrigan@server ~]$ ls -l article_submissions/my_articles/
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 podman_pulling

podman_pullingを移動しました ホームディレクトリから/article_submissions/my_articlesへの記事 。ここで、記事の名前を「 podman_pulling」から変更する必要があるとします。 "から"ルートレスポッドマン 。"どうすればよいですか?

[tcarrigan@server my_articles]$ mv podman_pulling rootless_podman
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

3。 ln-sファイルリンク

リンク このコンテキストでコマンドを実行すると、別のファイルまたはディレクトリへのソフトリンクまたはシンボリックリンクを作成できます。このトピックの詳細については、以前の記事 Linking Linux をご覧ください。 。ここでは、ソフトリンクコマンドの使用方法を説明します。

[tcarrigan@server ~]$ ln -s article_submissions/community_content/podman_pulling podman_article
[tcarrigan@server ~]$ ls -l
total 0
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb  6  2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Pictures
lrwxrwxrwx. 1 tcarrigan tcarrigan 52 Oct 27 13:51 podman_article -> article_submissions/community_content/podman_pulling
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Videos

これにより、ホームディレクトリに/home/tcarrigan/article_submissions/community_content/podman_pullingへのリンクが作成されます。 ファイル。これは、 podman_articleを読み取ることができることを意味します community_content内のファイルが開きます ディレクトリ。

4。タッチファイル

タッチを使用する ファイルを作成または更新するコマンド(ここでは、 new_file を作成しています my_articlesで ディレクトリ)。

[tcarrigan@server my_articles]$ touch new_file
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan    0 Oct 28 16:47 new_file
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

作成されたファイルのタイムスタンプは16:47であることに注意してください。 タッチを使用します タイムスタンプを現在の時刻(午後7時35分)に更新するコマンド。

[tcarrigan@server my_articles]$ touch new_file 
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan    0 Oct 28 19:35 new_file
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

5。 cat>ファイル

このコマンドは、標準出力をファイルに入力するために使用されます。ファイルtest_fileに「HelloWorld」を入力します

[tcarrigan@server ~]$ cat > test_file
Hello World 

Ctrl + D *を使用します ファイルの編集を終了します。作品を表示するには、標準のcat filenameを使用できます 構文。

[tcarrigan@server ~]$ cat test_file 
Hello World

6。その他のファイル

もっと コマンドを使用すると、ユーザーは一度に1画面ずつファイルの内容を表示できます。ここでは、/article_submissions/my_articles/creating_physical_volumesを見ていきます。 ファイル。

#  How to create a physical volume in Linux using LVM
by Tyler Carrigan

Physical volumes `PV` are the base 'block' that you need in order to manipulate a disk using Logical Volume Manager `LVM`. Now, let's not rush ahead. What exactly is a physical volume? What in the world i
s LVM? In short, LVM is a type of storage virtualization that allows operators far more flexibility in storage management than standard partitioning. A physical volume is any physical storage device, such
 as an Hard Disk Drive `HDD`, Solid State Drive `SSD`, or partition, that has been initialized as a physical volume with LVM. Without properly initialized physical volumes, you cannot create Volume Groups
 or logical volumes. 

So lets get started! First, there are a few considerations.

Dont try to pinpoint the exact amount of space you need down to the nearest byte. The reason for this is that LVM places labels on the physical volumes `UUID` as well as metadata storage. While this doesn
t take up very much space, understand that if you initialize a 1Gb PV, you do **not** have 1Gb of *usable* space.
** Output Omitted **

Enterを使用して1行ずつ移動できます キーまたはスペースのあるページごと 。もう1つの便利なオプションは、 -number 1ページに表示される行数を指定できるオプション。

一度に10行を表示するには、more -10 filenameを使用できます。 。 +を使用できます 開始する行を指定するための記号:more +10 filename

7。ファイルが少ない

少ない コマンドを使用すると、ユーザーはもっとと同じ機能を使用できます 。ただし、ファイル全体をロードするのではなく、ユーザーが矢印キーを使用してファイルを解析できるため、より高速です。これは、大きなログファイルで特に顕著です。

[tcarrigan@server my_articles]$ sudo less /var/log/messages
Oct 26 19:50:47 server dbus-daemon[939]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.15' (uid=0 pid=1155 comm="/usr/sbin/NetworkManager --no-daemon " label="system_u:system_r:NetworkManager_t:s0")
Oct 26 19:50:47 server systemd[1]: Starting Network Manager Script Dispatcher Service...
Oct 26 19:50:47 server dbus-daemon[939]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Oct 26 19:50:47 server systemd[1]: Started Network Manager Script Dispatcher Service.
** Output Omitted **

8。ヘッドファイル

コマンドを使用すると、ユーザーは任意のファイルの最初の10行を出力できます。 creating_physical_volumesの例を使用します 。

[tcarrigan@server my_articles]$ head Creating_physical_volumes
#  How to create a physical volume in Linux using LVM
by Tyler Carrigan

Physical volumes `PV` are the base 'block' that you need in order to manipulate a disk using Logical Volume Manager `LVM`. Now, let's not rush ahead. What exactly is a physical volume? What in the world is LVM? In short, LVM is a type of storage virtualization that allows operators far more flexibility in storage management than standard partitioning. A physical volume is any physical storage device, such as an Hard Disk Drive `HDD`, Solid State Drive `SSD`, or partition, that has been initialized as a physical volume with LVM. Without properly initialized physical volumes, you cannot create Volume Groups or logical volumes. 

So lets get started! First, there are a few considerations.

Dont try to pinpoint the exact amount of space you need down to the nearest byte. The reason for this is that LVM places labels on the physical volumes `UUID` as well as metadata storage. While this doesnt take up very much space, understand that if you initialize a 1Gb PV, you do **not** have 1Gb of *usable* space. 

Also, although LVM allows you to create physical volumes using multiple partitions, it is recommended that you use a single partition for a PV. This is for a couple of reasons. 
[tcarrigan@server my_articles]$

9。テールファイル

テール コマンドを使用すると、ユーザーは任意のファイルの最後の10行を表示できます。ここでは、/var/log/messagesを見ていきます。 。

[tcarrigan@server my_articles]$ sudo tail /var/log/messages
[sudo] password for tcarrigan:  
Oct 28 20:17:47 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9080279) is greater than comparison timestamp (9080269).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:17:47 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9080279.  Working around...
Oct 28 20:17:48 server dbus-daemon[948]: [system] Activating via systemd: service name='net.reactivated.Fprint' unit='fprintd.service' requested by ':1.500' (uid=0 pid=5259 comm="sudo tail /var/log/messages " label="unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023")
Oct 28 20:17:48 server systemd[1]: Starting Fingerprint Authentication Daemon...
Oct 28 20:17:49 server dbus-daemon[948]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 20:17:49 server systemd[1]: Started Fingerprint Authentication Daemon.
Oct 28 20:17:53 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9086680) is greater than comparison timestamp (9086677).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:17:53 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9086680.  Working around...
Oct 28 20:18:00 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9093426) is greater than comparison timestamp (9093424).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:18:00 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9093426.  Working around...
[tcarrigan@server my_articles]$ 

10。 tail-fファイル

-f テールのバリエーション コマンドは、元のコマンドとはまったく異なるものです。このフラグを使用すると、ユーザーはファイルが書き込まれているときにファイルを確認できます。これは、アプリケーションとシステムの起動/シャットダウンの問題のトラブルシューティングに非常に役立ちます。

わかりました

このチュートリアルを実行した場合、ターミナルの兵器庫にさらに10個のコマンドがあるはずです。移動、コピー、作成、更新から、ファイルの内容を読み取るさまざまな方法まで、ファイルシステム内を移動したり、遭遇したファイルやディレクトリを操作したりするのに慣れているはずです。このようなことが複雑に思える場合は、練習を続けるだけで、すぐに自信を持ってユーザーになれます。その他のヒント、コツ、ツールについては、私たちに確認して、システム管理者を有効にしてください。

[今すぐダウンロード:Bashスクリプトのシステム管理者ガイド。 ]


Linux
  1. 2020年にLinuxを使用する5つの理由

  2. 一般的なLinuxコマンドのチートシート

  3. 例を挙げて説明する初心者向けの20の基本的なLinuxコマンド

  1. 効果的なプロセス管理のための8つのLinuxコマンド

  2. ネットワーク診断用の10のLinuxコマンド

  3. 初心者のためのLinux:ターミナルで始めるための10のコマンド

  1. LinuxファンのためのFreeDOSコマンド

  2. Linuxの検索コマンド

  3. 初心者向けの基本的な Linux コマンドのトップ