Linux command line 2 - Manipulating files and directories

LHT

This article mainly references the book The Linux Command Line (2nd Edition). The shell tool used is Xshell, and the operating system is CentOS 7.6. This means that some shortcuts may not work in other shell tools, and there may be slight differences in files across different Linux operating systems. Please analyze based on your actual situation.

Since CentOS 7.6 is EOL, consider using Rocky Linux or Alma Linux instead.

Working with Files and Directories

Although using a graphical file manager can easily accomplish file copying, moving, renaming, and other operations, command-line programs are much more powerful and flexible.

For example, how to copy all HTML files in a directory to the target directory (these files do not exist in the target directory or are newer than the ones in the target directory). To complete this task, it is quite difficult to use a file manager, but very easy to use the command line.

1
cp -u *.html destination

Wildcards

Wildcards help us quickly specify a group of files.

Wildcard Meaning
* Matches any number of characters (including zero or one)
? Matches any single character (not zero)
[characters] Matches any single character within the set
[!characters] Matches any single character not within the set
[[:class:]] Matches any single character in the specified character class

Here are some common character classes:

Character Class Meaning
[:alnum:] Matches any letter or digit
[:alpha:] Matches any letter
[:digit:] Matches any digit
[:lower:] Matches any lowercase letter
[:upper:] Matches any uppercase letter

Using wildcards, you can select files based on complex criteria. Here are some examples:

Pattern Matches
* All files
g* Files that start with “g”
b*.txt Files that start with “b”, followed by zero or more characters, and ending with “.txt”
Data??? Files that start with “Data” and are followed by exactly three characters
[abc]* Files that start with “a”, “b”, or “c”
BACKUP.[0-9][0-9][0-9] Files that start with “BACKUP.” followed by exactly three digits
[[:upper:]]* Files that start with an uppercase letter
[![:digit:]]* Files that do not start with a digit
*[[:lower:]123] Files that end with a lowercase letter or “1”, “2”, or “3”

Any command that accepts filenames as parameters can use wildcards.

mkdir - Create Directory

1
mkdir directory...

The ellipsis indicates that the command can accept multiple arguments.

1
mkdir dir1 dir2 dir3

This will create three directories named dir1, dir2, and dir3.

cp - Copy Files and Directories

The cp command has two usages:

Copy a single file or directory “item1” to file or directory “item2”:

1
cp item1 item2

Copy multiple items (files or directories) to a directory:

1
cp item... directory

Common Options and Examples

Option Meaning
-a, –archive Copy files and directories along with their attributes, including ownership and permissions. Normally, the copy has the default attributes of the user performing the operation.
-i, –interactive Prompt the user for confirmation before overwriting an existing file. If this option is not specified, the cp command will overwrite files by default.
-r, –recursive Recursively copy directories and their contents. This option is necessary when copying directories (or you can use the -a option).
-u, –update When copying files from one directory to another, only copy files that do not exist in the target directory or files that are newer than those in the target directory.
-v, –verbose Display detailed command operation information.

Note that the cp command overwrites files by default. To receive a prompt before overwriting, use the -i option.

Command Result
cp file1 file2 Copy the contents of file1 to file2. If file2 exists, its contents will be overwritten by file1. If file2 does not exist, it will be created.
cp -i file1 file2 Same as above, except if file2 exists, the user will be prompted to confirm before it is overwritten.
cp file1 file2 dir1 Copy file1 and file2 to directory dir1. The directory dir1 must exist.
cp dir1/* dir2 Use a wildcard to copy all files from dir1 to dir2. The directory dir2 must exist.
cp -r dir1 dir2 Copy the contents of dir1 to dir2. If dir2 does not exist, it will be created. After the operation, the contents of dir2 will be the same as those of dir1. If dir2 exists, the directory dir1 (and its contents) will be copied into dir2.

When copying files or some files from a directory to a target directory, the target directory must exist. Typically, we create the directory first, then copy the files.

mv - Move and Rename Files

mv: can be used to move files or rename them.

There are two usages:

1
mv item1 item2
1
mv item... directory

The meanings are similar to those of the cp command, so we won’t go into detail.

Common Options and Examples

Option Meaning
-i or –interactive Prompt the user for confirmation before overwriting an existing file. If this option is not specified, the mv command will overwrite files by default.
-u or –update When moving files from one directory to another, only move files that do not exist in the target directory or files that are newer than those in the target directory.
-v or –verbose Display detailed information during the mv operation.
Command Result
mv file1 file2 Move file1 to file2. If file2 exists, its contents will be overwritten by file1. If file2 does not exist, it will be created. In both cases, file1 will no longer exist.
mv -i file1 file2 Same as above, except if file2 exists, the user will be prompted to confirm before it is overwritten.
mv file1 file2 dir1 Move file1 and file2 to directory dir1. The directory dir1 must exist.
mv dir1 dir2 If dir2 does not exist, it will be created, and the contents of dir1 will be moved to dir2, and dir1 will be deleted. If dir2 exists, dir1 (and its contents) will be moved into dir2.

The considerations are similar to those of the cp command.

rm - Delete Files and Directories

Delete one or more files or directories:

1
rm item...

Common Options and Examples

Option Meaning
-i, –interactive Prompt the user for confirmation before deleting an existing file. If this option is not specified, rm will delete files silently.
-r, –recursive Recursively delete files. This means that if you delete a directory that contains subdirectories, the subdirectories will also be deleted. To delete a directory, you must specify this option.
-f, –force Ignore nonexistent files and do not show any prompts. This option overrides the --interactive option.
-v, –verbose Display detailed information during the rm operation.
Command Result
rm file1 Silently delete the file
rm -i file1 Same as above, except it will prompt the user for confirmation before deleting the file.
rm -r file1 dir1 Delete file file1, directory dir1, and the contents of dir1.
rm -rf file1 dir1 Same as above, except if file1 or dir1 does not exist, rm will still proceed.

Use Delete Commands with Caution

Be especially careful when using the rm command. Once a file is deleted, it cannot be recovered. Before deleting a file, it’s best to check its contents to make sure you really want to delete it.

Pay special attention when using wildcards. Consider this classic example:

Suppose you only want to delete HTML files in a directory. You enter rm *.html, which is correct. But if you accidentally add a space between the * and .html, like this: rm * .html, this rm command will delete all files in the directory and complain that there are no files named .html.

💡 Tip: When using the rm command with wildcards, it’s a good practice to first test the wildcard with the ls command. This way, you can see exactly which files will be deleted. Then, press the up arrow key to recall the last command, replacing ls with rm.

The rm command can be used with the -i option, which is especially useful when deleting multiple files by their extension. With this option, the system will prompt you to confirm the deletion of each file individually. To delete a file, you must type y and press Enter. If you just press Enter or input any other character, the file will not be deleted.

More Usage Examples

rm command, Linux rm Command Explanation: Used to delete specified files and directories - Linux Command Search Engine

For the previously mentioned cp and mv commands, you can also find relevant options and usage examples on this website.

To create a hard link:

1
ln file link

To create a symbolic link, where “item” can be a file or directory:

1
ln -s item link

Each file has at least one hard link, which provides the file a name. Every additional hard link is essentially an extra directory entry pointing to the same file. Hard links come with two major limitations:

  1. Hard links cannot reference files on a different file system. This means that you cannot create hard links across different disk partitions.
  2. Hard links cannot reference directories. There is no visual distinction between a hard link and the file itself. Deleting a hard link only removes the link, not the file’s data, as long as there are other links pointing to that file.

Symbolic links are special types of files that contain a textual pointer to another file or directory, much like shortcuts in Windows, though symbolic links predate Windows shortcuts by many years.

A symbolic link points to a file. Writing to a symbolic link modifies the target file, but deleting a symbolic link only deletes the link, not the file itself. If the target file is deleted before the symbolic link, the link remains, but it will be broken, meaning it points to nothing. Broken links are often displayed in a different color (e.g., red in ls output).

Similarities

  • Both are created using the ln command. To create a symbolic link, you add the -s option.
  • Both maintain synchronization between all linked instances, meaning a change in one link affects all others.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@lht example]# ls
hello.txt
[root@lht example]# ln hello.txt hardlink
[root@lht example]# ls
hardlink hello.txt
[root@lht example]# ln -s hello.txt symboliclink
[root@lht example]# ls
hardlink hello.txt symboliclink
[root@lht example]# vim hello.txt
[root@lht example]# cat hello.txt
hello world !!!
[root@lht example]# cat hardlink
hello world !!!
[root@lht example]# cat symboliclink
hello world !!!

In this example, editing hello.txt with vim updates the content of all three files (the original, the hard link, and the symbolic link).

Differences

  • Symbolic links can cross file systems, while hard links cannot.

  • Symbolic links can link to directories, but hard links cannot.

  • Symbolic links create a new file with a different inode number, while hard links share the same inode number as the original file. Hard links act as copies but do not take up extra space.

  • Symbolic links can reference non-existent files or directories, creating a broken link if the target is not present.

    For example:

    1
    2
    3
    [root@lht example]# ln -s nonexistentfile badlink
    [root@lht example]# ls -l
    lrwxrwxrwx 1 root root 15 Jun 15 11:06 badlink -> nonexistentfile

When thinking about hard links, consider that a file consists of two parts: the data portion (the file’s content) and the name portion (directory entry). Creating a hard link adds an extra name pointing to the same data. As a result, multiple hard links can reference the same file data.

You can use the ls -i command to display inode information:

1
2
[root@lht example]# ls -i
917519 hardlink 917519 hello.txt 922927 symboliclink -> hello.txt

The inode number is the same for the hard link and the original file, but different for the symbolic link.

Additional Details

The size of a symbolic link is the number of bytes in the path it points to. For example, the symbolic link symboliclink -> hello.txt has a size of 9 bytes because “hello.txt” is 9 characters long.

When you create a hard link, the file’s inode number (its unique identifier in the file system) is shared between multiple directory entries. You can see the number of hard links in the second column of the ls -l output. Creating a new hard link increases this count, and deleting one decreases it. A file is only physically removed from the file system when its link count reaches zero.

  • Title: Linux command line 2 - Manipulating files and directories
  • Author: LHT
  • Created at : 2023-01-02 08:00:00
  • Link: https://blog.327774.xyz/2023/01/02/linux/Linux command line 2 - Manipulating files and directories/
  • License: This work is licensed under CC BY-NC-SA 4.0.