Subscribe to the feed
Linux 

In case you missed the first segment of this two-part series, we looked at 10 commands to get you started at the terminal. Now, we are going to explore 10 additional commands that you can use to continue your command line journey. Specifically, I want to look at the commands used to manipulate files. We are going to copy, move, and rename files, plus a few commands for reading files in different ways.

Commands to know

1. cp file1 file2

The copy command is used to copy the contents of one file to another. You can also use this command to copy a file from one directory into another.

[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

As you see, I copied the podman_pulling article contents to the article file. Now, the other thing you can do with this command is copy an existing file into another directory. For example, if I wanted to copy the podman_pulling article to my home directory, I would do something like this:

[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

You can see that podman_pulling is now available in my /home/tcarrigan directory.

[ You might also like: 10 handy systemd commands: A reference ]

2. mv file1 file2

The move command allows a user to move or rename a file. To move a file, you would use the following:

[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

I moved the podman_pulling article from my home directory over to /article_submissions/my_articles. Now, let’s say we needed to rename the article from "podman_pulling" to "rootless podman." How would we do that?

[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 file link

The link command in this context will allow us to create a soft link or symbolic link to another file or directory. For more information on this topic, check out my previous article, Linking Linux. For now, I am just going to demonstrate how to use the soft link command.

[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

This creates a link in my home directory to the /home/tcarrigan/article_submissions/community_content/podman_pulling file, which means that you can read podman_article, and it will open the file inside the community_content directory.

4. touch file

Use the touch command to create or update a file (here, I am creating new_file in the my_articles directory).

[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

Note the timestamp of the file created is 16:47. I am now going to use the touch command to update the timestamp to the current time (7:35pm).

[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 > file

This command is used to input standard output into a file. I will input "Hello World" into the file test_file.

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

Use Ctrl+D* to finish editing the file. To view your work, you can use the standard cat filename syntax.

[tcarrigan@server ~]$ cat test_file 
Hello World

6. more file

The more command will allow the user to view the contents of a file one screenful at a time. Here we are going to look at the /article_submissions/my_articles/creating_physical_volumes file.

#  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 **

You can go line by line with the Enter key or page by page with Space. Another helpful option is the -number option that allows you to specify the number of lines displayed per page.

To view 10 lines at a time, you might use more -10 filename. You can use the + sign to specify which line to start from: more +10 filename.

7. less file

The less command allows the user the same functionality as more. However, it is faster because it does not load the entire file but instead allows the user to parse the file using the arrow keys. This is especially noticeable in large log files.

[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. head file

The head command allows a user to output the first 10 lines of any file. I will use the example of 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. tail file

The tail command allows the user to view the last 10 lines of any file. Here we will look at /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 file

The -f variant of the tail command is an entirely different take on the original command. This flag allows the user to see the file as it’s being written. This is incredibly useful for troubleshooting startup/shutdown issues for applications and systems alike.

 

Now you know

If you followed along with this tutorial, you should have another 10 commands in your terminal arsenal. From moving, copying, creating, and updating to various ways to read file contents, you should feel comfortable moving about the filesystem and manipulating the files and directories that you come across. If this stuff seems complicated, just keep practicing, and you’ll be a more confident user in no time. For more tips, tricks, and tools, check back with us and Enable Sysadmin.

[ Download now: A sysadmin's guide to Bash scripting. ] 


About the author

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech