You are currently viewing Basic Linux File Commands

Photo from Pexels by element5

I remember when I was first starting out with Linux – I would only look up instructions on how to do specific tasks.  I didn’t invest much time at all trying to gain any foundational understanding.

However, after reading enough material, I began to recognize the same commands over and over.  At this point, I decided to research the most common Linux commands to gain familiarity with them; this way, I didn’t have to look them up every time I read an article which resulted in a dozen browser tabs.  It’s been over a decade since then and, now, it is time for me to contribute my own beginners version.

OVERVIEW

In this tutorial, we’ll cover essential Linux command line operations specifically for working with files and folders.  This will allow us to view and edit files & directories, which is necessary for most administrative work.

NOTE: System Specifics
All the following commands were tested on a CentOS 7 VM. If you are using a different system, your results may vary.

Outline

  • Viewing Files and Folders
    • pwd
    • ls
    • cd
    • cat
    • less
    • head
    • tail
  • Editing Files and Folders
    • touch
    • mkdir
    • mv
    • cp
    • rm

Prerequisites

VIEWING FILES AND FOLDERS

The following commands will cover different ways to move around and view both the filesystem and file content.

pwd

When you access or log in to a Linux system, typically, you start “in” a directory somewhere on the filesystem; from there, you may relocate to other directories.

NOTE: Directories/Folders
Directories and folders are the same thing: they are special files that may “contain” other files.

Wherever you currently are on the system, this is your working directory or CWD (“Current Working Directory”).  To display your CWD, use the `pwd` command, which stands for “print working directory”:

[penguin@centos07 ~]$ pwd
/home/penguin

NOTE: $PWD environment variable
While similar, the pwd command should not be confused with the $PWD (Present Working Directory) environment variable. The $PWD variable stores/remembers your CWD and you can view its contents with this command: echo $PWD. The output should match the pwd command.

ls

Now that you know which directory you’re in, it’s time to find out, or list, what’s in that directory (files, other directories, links, etc.).  `ls` is short for “list” and, as you might expect, this command is for listing files or directory contents.  When executed by itself, with no arguments, `ls` will list the files and directories from your CWD:

[penguin@centos07 ~]$ ls
Directory  file.txt  Folder  picture.png

You can also look inside specific directories by adding it as a parameter:

[penguin@centos07 ~]$ ls Directory/
AnotherDirectory  bar.csv  foo.txt
[penguin@centos07 ~]$ ls /home/penguin/Folder/
AnotherFolder  eggs.csv  spam.txt

The same goes for a singular file:

[penguin@centos07 ~]$ ls file.txt
file.txt
[penguin@centos07 ~]$ ls Directory/foo.txt
Directory/foo.txt
[penguin@centos07 ~]$ ls Folder/spam.txt
Folder/spam.txt

Moreover, you can define multiple files and directories in the same command.

  1. Two files:
[penguin@centos07 ~]$ ls Directory/bar.csv picture.png
Directory/bar.csv  picture.png
  1. Two directories (notice the added folder names and spacing for easier viewing):
[penguin@centos07 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  bar.csv  foo.txt

Folder/:
AnotherFolder  eggs.csv  spam.txt
  1. Three directories/files:
[penguin@centos07 ~]$ ls Directory/AnotherDirectory/ file.txt Folder/
file.txt

Directory/AnotherDirectory/:
findme.txt

Folder/:
AnotherFolder  eggs.csv  spam.txt

cd

If you wish to “move” to another directory, you can use the “change directory” command: cd. To move to a specific directory, simply define it as a parameter:

[penguin@centos07 ~]$ cd Directory/AnotherDirectory/
[penguin@centos07 AnotherDirectory]$ pwd
/home/penguin/Directory/AnotherDirectory
[penguin@centos07 AnotherDirectory]$ cd /tmp
[penguin@centos07 tmp]$ pwd
/tmp

If executed by itself, you’ll be taken to your home directory, which is usually something like /home/<UserName>:

[penguin@centos07 ~]$ cd
[penguin@centos07 ~]$ whoami
penguin
[penguin@centos07 ~]$ pwd
/home/penguin

cat

There can be many plaintext files on Linux systems; so, it’ll certainly be useful to see the contents of those files. For that, we’ll use the cat command – short for “concatenate”. Just type cat followed by the file you want to see – This will print/display the file contents:

[penguin@centos07 ~]$ cat file.txt
This is a test plaintext file.

The file does not have to be in the same directory that you’re in:

[penguin@centos07 ~]$ cat /etc/resolv.conf
# Generated by NetworkManager
search domain.lan
nameserver 8.8.8.8
nameserver 8.8.4.4

NOTE: Plaintext Files
Essentially, plaintext files are human-readable files: words or other alphanumeric characters. Non-plaintext files would be things like pictures, music, or binary files – They are not intended to be read directly; so, even though you can cat these files, they will be largely unreadable (likely a seemingly random mix of letters, numbers, and symbols).

less

Now, for files that are too large for the terminal, you can use the less command:

[penguin@centos7 ~]$ less /etc/services

From here, you will see the contents of the file in which you can scroll up and down using the arrow keys (or the j & k keys):

# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
...
smtp            25/tcp          mail
smtp            25/udp          mail
time            37/tcp          timserver
time            37/udp          timserver
/etc/services

When finished, press q to exit back to the CLI.

NOTE: more
There is a similar command, called more, that lets you view a file’s contents; however, I don’t find it as useful as the less command because it only lets you scroll down.

head

Again, with large files – like logs – if you only need to see the first ten lines, using the head command is optimal:

[penguin@centos7 ~]$ head /var/log/dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-1127.19.1.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Aug 25 17:23:54 UTC 2020
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-1127.19.1.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007dd63fff] usable
[    0.000000] BIOS-e820: [mem 0x000000007dd64000-0x000000007dd64fff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000007dd65000-0x000000007de90fff] usable

tail

Opposite to the head command, you can use the tail command to view the last ten lines of a file:

[penguin@centos7 ~]$ tail /var/log/dmesg
[    5.550208] AVX2 version of gcm_enc/dec engaged.
[    5.550212] AES CTR mode by8 optimization enabled
[    5.590367] ppdev: user-space parallel port driver
[    5.602758] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    5.602782] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[    5.668682] Adding 2097148k swap on /dev/mapper/centos-swap.  Priority:-2 extents:1 across:2097148k FS
[    5.702540] XFS (sda2): Mounting V5 Filesystem
[    5.722793] XFS (sda2): Ending clean mount
[    5.883336] type=1305 audit(1602948607.726:4): audit_pid=644 old=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:auditd_t:s0 res=1
[    6.565329] snd_intel8x0 0000:00:05.0: white list rate for 1028:0177 is 48000

EDITING FILES AND FOLDERS

While the previous section reviewed methods for seeing content, this section will be about creating and modifying content.

touch

The manual page for the touch command, on the Ubuntu Linux distribution, states that it “change[s] file timestamps”. Take the following example:

[penguin@centos7 ~]$ ls -l file.txt
-rw-rw-r--. 1 penguin penguin 26 Apr 12 2019 file.txt

NOTE: Timestamps
Depending on how old the file is and where it originated from, either the year or time will be displayed.

The dates that you see here (Apr 12 2019) is the timestamp. So, if you’d like to update the timestamp to the current date and time, use the touch command:

[penguin@centos7 ~]$ touch file.txt

If today is Oct 17 @7:52pm, then the output will change to the following:

[penguin@centos7 ~]$ ls -l file.txt
-rw-rw-r--. 1 penguin penguin 26 Oct 17 19:52 file.txt

While the previous situation is true, a more common use for the touch command is to quickly create a new, empty file:

[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  picture.png
[penguin@centos7 ~]$ touch new_file.txt
[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  new_file.txt  picture.png

mkdir

mkdir is short for “make directory” and, as you’d expect, it is used for creating a directory/folder. To do so, type mkdir followed by the directory you want to create: mkdir <NewDirectory>.

[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  new_file.txt  picture.png
[penguin@centos7 ~]$ mkdir MyNewFolder

Now, you should be able to see and use the new directory:

[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  MyNewFolder  new_file.txt  picture.png
[penguin@centos7 ~]$ cd MyNewFolder/
[penguin@centos7 MyNewFolder]$ pwd
/home/penguin/MyNewFolder

NOTE: rmdir
To delete a directory, use the rmdir (remove directory) command: rmdir <SomeFolder>. This command can only delete empty directories.

mv

If you need to move a file, then you would use the mv command. This is similar to a cut-and-paste process. You need at least three arguments here – First, specify (1) the command itself, followed by (2) the source file/directory, then (3) the destination file/directory: mv <SourceFileOrDirectory> <DestinationFileOrDirectory>.

Now, with the mv command, there are three basic combinations when defining the source and destination file/directory:

  1. Source File → Destination Directory
  2. Source Directory → Destination Directory
  3. Source File → Destination File

NOTE: source directory → destination file
When only considering the mv command, it’s not possible to move a directory into a single file. You’ll receive an error if you try.

1. Source File → Destination Directory
When defining a source file and destination directory, you’re merely moving that file into the directory: mv <SourceFile> <DestinationDirectory>.

[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  MyNewFolder  new_file.txt  picture.png
[penguin@centos7 ~]$ mv new_file.txt MyNewFolder/
[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  MyNewFolder  picture.png
[penguin@centos7 ~]$ ls MyNewFolder/
new_file.txt

Notice that you get the same result with or without the trailing slash on the directory.

[penguin@centos7 ~]$ ls
Directory  file.txt  Folder  MyNewFolder  picture.png
[penguin@centos7 ~]$ mv file.txt MyNewFolder
[penguin@centos7 ~]$ ls
Directory  Folder  MyNewFolder  picture.png
[penguin@centos7 ~]$ ls MyNewFolder
file.txt  new_file.txt

WARNING: Existing Destination Files
If you move the source file to a folder that already has a file of the same name, the source file will overwrite it. So, in the previous example, if MyNewFolder/file.txt existed prior to moving file.txt into MyNewFolder, the original MyNewFolder/file.txt file would be overwritten.

2. Source Directory → Destination Directory
Similarly, when defining a directory for both source and destination, you are simply moving the first directory into the second directory: mv .

[penguin@centos7 ~]$ ls
Directory  Folder  MyNewFolder  picture.png
[penguin@centos7 ~]$ ls Directory/ MyNewFolder/
Directory/:
AnotherDirectory  bar.csv  foo.txt

MyNewFolder/:
file.txt  new_file.txt
[penguin@centos7 ~]$ mv MyNewFolder Directory/
[penguin@centos7 ~]$ ls
Directory  Folder  picture.png
[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  foo.txt  MyNewFolder

Again, you receive the same result regardless of the slashes on either directory.

NOTE: Existing Destination Directories
If you move the source directory to a folder that already has a directory of the same name, one of two things will happen:
– (A) If the existing directory is empty, it will be overwritten by the source directory and its contents.
– (B) If the existing directory is not empty, you’ll receive an error.

3. Source File → Destination File
Now, when defining a file for both source and destination, depending on the destination, there are several different scenarios here: (a) same directory, same file; (b) same directory, different file; (c) different directory, same file; (d) different directory, different file.

(a) If the directory and file are the same as the source, nothing changes – you’ll see a message about the files being the same.

[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  foo.txt  MyNewFolder
[penguin@centos7 ~]$ mv Directory/foo.txt Directory/foo.txt
mv: ‘Directory/foo.txt’ and ‘Directory/foo.txt’ are the same file

(b) If the directory is the same as the source, but the file name is different, you’ve essentially just renamed the file.

[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  foo.txt  MyNewFolder
[penguin@centos7 ~]$ mv Directory/foo.txt Directory/spam.txt
[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  MyNewFolder  spam.txt

NOTE: Renaming Directories
Using mv to rename directories works in the same fashion: mv <OldDirectoryName> <NewDirectoryName>.

(c) If the directory is different from the source, but the file name is the same, the outcome is identical to the first combination above: “Source File → Destination Directory”; except, you’ve defined the file name twice, which is not wrong, but it is unnecessary. In the following example, mv picture.png Directory/picture.png has the same effect as mv picture.png Directory/.

[penguin@centos7 ~]$ ls
Directory  Folder  picture.png
[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  MyNewFolder  spam.txt
[penguin@centos7 ~]$ mv picture.png Directory/picture.png
[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  bar.csv  MyNewFolder  picture.png  spam.txt

(d) If both the directory and file are different from the source, the result is a combination of moving and renaming the file simultaneously.

[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  bar.csv  MyNewFolder  picture.png  spam.txt

Folder/:
AnotherFolder  eggs.csv  spam.txt
[penguin@centos7 ~]$ mv Directory/bar.csv Folder/foo.csv
[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  MyNewFolder  picture.png  spam.txt

Folder/:
AnotherFolder  eggs.csv  foo.csv  spam.txt

cp

When you need to duplicate a file, the easiest way is to use the “copy” command: cp. Similar to the mv command, you specify three arguments again – the command itself, followed by the source and destination files: cp <SourceFile> <DestinationFile>. Here, a copy of “picture.png” is made as “duplicate.png”:

[penguin@centos7 ~]$ cd Directory/
[penguin@centos7 Directory]$ ls
AnotherDirectory  MyNewFolder  picture.png  spam.txt
[penguin@centos7 Directory]$ cp picture.png duplicate.png
[penguin@centos7 Directory]$ ls
AnotherDirectory  duplicate.png  MyNewFolder  picture.png  spam.txt

NOTE: File Backups
Before editing an important file, a common way to make a quick backup is to use cp. Say you want to backup this file: /etc/ssh/sshd_config. You might execute the following command: cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.

You can also make copies to different directories:

[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  duplicate.png  MyNewFolder  picture.png  spam.txt

Folder/:
AnotherFolder  eggs.csv  foo.csv  spam.txt
[penguin@centos7 ~]$ cp Directory/spam.txt Folder/
[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  duplicate.png  MyNewFolder  picture.png  spam.txt

Folder/:
AnotherFolder  eggs.csv  foo.csv  spam.txt

When copying files, there are two ways to define the destination: (1) with the file name, or (2) without the file name. If you don’t specify the destination file name, it’s assumed to be the same as the source file name. Just like the mv command, cp Directory/spam.txt Folder/ is the same as cp Directory/spam.txt Folder/spam.txt.

rm

Finally, you’ll need a method for deleting files. For this you’ll use the “remove” command: rm. The syntax is simple enough – type rm followed by the file you want to delete: rm <FileToDelete>.

[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  duplicate.png  MyNewFolder  picture.png  spam.txt
[penguin@centos7 ~]$ rm Directory/spam.txt
[penguin@centos7 ~]$ ls Directory/
AnotherDirectory  duplicate.png  MyNewFolder  picture.png

WARNING: No Prompt when Deleting
When executing the rm command, depending on the command options and file permissions, the file(s) might be deleted without asking for confirmation; so, you’ll need to be careful with this command.

You can delete more than one file at a time, just type them all out:

[penguin@centos7 ~]$ ls
Directory  Folder
[penguin@centos7 ~]$ mv Directory/AnotherDirectory/findme.txt /home/penguin/
[penguin@centos7 ~]$ ls
Directory  findme.txt  Folder
[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  duplicate.png  MyNewFolder  picture.png

Folder/:
AnotherFolder  eggs.csv  foo.csv  spam.txt
[penguin@centos7 ~]$ rm findme.txt Directory/duplicate.png Folder/spam.txt
rm: remove write-protected regular empty file ‘findme.txt’? yes
rm: remove write-protected regular empty file ‘Directory/duplicate.png’? no
[penguin@centos7 ~]$ ls
Directory  Folder
[penguin@centos7 ~]$ ls Directory/ Folder/
Directory/:
AnotherDirectory  duplicate.png  MyNewFolder  picture.png

Folder/:
AnotherFolder  eggs.csv  foo.csv

CONCLUSION

Armed with these basic commands, you can now view and manipulate files/directories on Linux systems. I’ll admit that, starting out, I found these commands and scenarios often confusing and difficult to remember; however, like most things, the more you use the commands, the easier it becomes. I’ve been executing these commands for years; so, most of the time I don’t need to check my notes but, even today, prior to working on something critical, I’ll test the command(s) elsewhere.

You now have a reference for trying out these commands; as such, I suggest you do just that: practice these commands on a test system. Use different file names and folder locations; see how many commands you can use without looking at any notes; wait a day or so and execute them all over again. It takes some trial and error but, with time, you’ll become more and more comfortable with the commands.