Generic Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

1. Overview

In Linux, everything is a file. So, file manipulations – creating a file, removing a directory, etc. – are very common operations in Linux.

In this tutorial, let's see some useful file manipulation commands and learn how to use them.

Note that we'll only focus on the manipulation of files themselves, not their content.

2. The touch Command

The touch command can be used to create files without any content.

To create empty files, we just touch filenames:

$  ls -l
total 0

$  touch file1.txt file2.txt file3.txt
$  ls -l
total 0
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file1.txt
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file2.txt
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file3.txt

Except for creating empty files, the touch command can help us to update the access time and the modification time of a file.

We can use the -a option to update the access time for file.txt:

$  touch -a --date='1992-08-08 17:17:59' file1.txt
$  ls -u --full-time 
total 0
-rw-r--r-- 1 kent kent 0 1992-08-08 17:17:59.000000000 +0200 file1.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file2.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file3.txt

In the above example, we gave the -u option to the ls command in order to show the access time in the file list.

The -m option is for changing the modification time of a file.

For example, let's change the modification time of the file3.txt to some time in the '70s:

$  touch -m --date='1976-11-18 18:19:59' file3.txt
$  ls --full-time
total 0
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file1.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file2.txt
-rw-r--r-- 1 kent kent 0 1976-11-18 18:19:59.000000000 +0100 file3.txt

3. The mkdir Command

touch is handy for creating empty files. To do the same for directories, we use mkdir.

The syntax of the mkdir command is quite similar to the above touch command.

Let's see an example of how to create three directories in one shot:

$  mkdir one two three
$  ls -l
total 0
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 one/
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 three/
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 two/

The mkdir command will complain if the directory we're about to create exists already.

For instance, we'll get an error if we attempt to create another directory called “one”:

$  mkdir one
mkdir: cannot create directory ‘one’: File exists

A very convenient option on the mkdir command is -p. The -p option allows us to create parent directories as necessary and not complain if the directories exist.

Let's create some sub-directories under the existing one directory – we'll list our result with tree:

$  mkdir -p one/one.1/one.1.1  one/one.2/one.2.1
$  tree -d
.
├── one
│   ├── one.1
│   │   └── one.1.1
│   └── one.2
│       └── one.2.1
├── three
└── two

4. The rm Command

The rm command does the opposite of creating files and directories:  It removes them.

To remove files with rm is easy, we just add the filenames we want to remove after the rm command.

For example, let's say want to remove the three .txt files we created earlier with touch:

$  ls
file1.txt  file2.txt  file3.txt
$  rm file1.txt file2.txt file3.txt
$  ls
total 0

By default, rm does not remove directories. We can make use of the -d option to remove empty directories.

In the next example, let's try to remove a directory we just created: two:

$  rm -d two
$  tree -d
.
├── one
│   ├── one.1
│   │   └── one.1.1
│   └── one.2
│       └── one.2.1
└── three

If we apply the same command on the directory one, we'll get an error since the directory one is not empty, it has sub-directories:

$  rm -d one
rm: cannot remove 'one': Directory not empty

If we want to remove directories and their contents recursively, we should use the rm‘s -r option:

$  rm -r one
$  tree -d
.
└── three

The rm command will prompt for confirmation removal if a file is write-protected:

$  ls -l
total 0
drwxr-xr-x 2 kent kent  80 Oct 27 16:55 ./
drwxr-xr-x 5 kent kent 100 Oct 27 11:56 ../
-r--r--r-- 1 kent kent   0 Oct 27 16:55 readOnly1
-r--r--r-- 1 kent kent   0 Oct 27 16:54 readOnly2

$  rm readOnly1
rm: remove write-protected regular empty file 'readOnly1'? y

$  ls -l
total 0
-r--r--r-- 1 kent kent 0 Oct 27 16:54 readOnly2

However the -f option overrides this minor protection and removes the file forcefully.

Let's remove the other write-protected file with the -f option:

$  rm -f readOnly2 
$  ls -l
total 0

The rm command works normally silently. So, we should be very careful while executing the rm command, particularly with the -r and the -f options. Once the files or directories get deleted, it's really hard to recover them again.

5. The cp Command

The cp command is used to copy files or directories.

Let's take a look at how to make a copy of the file file1.txt with cp:

$  ls
file1.txt  file2.txt
$  cp file1.txt file1_cp.txt
$  ls
file1_cp.txt  file1.txt  file2.txt

Often we want to copy multiple files in Linux. To do that, we just pass the names of the files followed by the destination directory to the cp command:

$  tree -F
.
├── targetDir/
├── file1_cp.txt
├── file1.txt
└── file2.txt

1 directory, 3 files

$  cp file1.txt file2.txt file1_cp.txt targetDir
$  tree                                       
.
├── targetDir/
│   ├── file1_cp.txt
│   ├── file1.txt
│   └── file2.txt
├── file1_cp.txt
├── file1.txt
└── file2.txt

1 directory, 6 files

Of course, we can do the same by file globbing:

$ cp *.txt targetDir

Another everyday usage of file copying would be coping some source directory and all contents under it to a target directory.

To do that, we pass the -R option, and cp will recursively copy the source directory:

$  tree -F
.
└── srcDir/
    ├── dir1/
    │   └── file1.txt
    └── dir2/
        └── file2.txt

3 directories, 2 files

$  cp -R srcDir targetDir
$  tree -F               
.
├── srcDir/
│   ├── dir1/
│   │   └── file1.txt
│   └── dir2/
│       └── file2.txt
└── targetDir/
    ├── dir1/
    │   └── file1.txt
    └── dir2/
        └── file2.txt

6 directories, 4 files

Another very useful option to the cp command is –preserve.

We can pass the –preserve option along with the attributes we want to preserve.

By default mode, ownership and timestamps will be preserved.

Let's say we have a file “guestFile“:

$  ls -l
total 0
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 guestFile

The ls result shows that:

  • the file is owned by user guest and group guest 
  • the modification time of the file was 2019-10-27 18:35

Now we change to another user, for example, the user root.

$  su root
Password: 
root#

Then, we copy the file twice; once with and once without the –preserve option:

root# cp guestFile withoutPreserve
root# cp --preserve guestFile withPreserve 
root# ls -l
total 0
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 guestFile
-rw-r--r-- 1 root  root  0 Oct 27 18:46 withoutPreserve
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 withPreserve

Thus, without the –preserve option, the original ownership and timestamps were not kept. While with the option, those attributes were preserved.

6. The mv Command

We can use the mv command to move files or directories.  The command syntax of mv is similar to cp. 

Let's take a look at how to use the mv command to move a file or a directory:

$  tree -F
.
├── oldDir/
└── oldFile

1 directory, 1 file

$  mv oldFile newFile
$  mv oldDir newDir
$  tree -F
.
├── newDir/
└── newFile

1 directory, 1 file

So, we've moved the oldFile and the oldDir to the newFile and the newDir. In fact, we have just renamed the file and the directory.

Renaming a file or directory is a common usage of the mv command. 

We can move multiple files to a target directory as well:

$  tree -F
.
├── srcFile1
├── srcFile2
├── srcFile3
└── targetDir/

1 directory, 3 files
$  mv srcFile1 srcFile2 srcFile3 targetDir 
$  tree -F
.
└── targetDir/
    ├── srcFile1
    ├── srcFile2
    └── srcFile3

1 directory, 3 files

Just as we learned in the cp command section, we can use file globbing for the mv command too.

The following command is equivalent to what just tried:

$ mv srcFile* targetDir

7. Conclusion

In this tutorial, we’ve talked about several everyday file manipulation commands by examples.

Armed with these useful handy commands we'll manipulate files efficiently in Linux.

Generic bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

Leave a Reply

avatar
  Subscribe  
Notify of