Working with Files

In this section we will discuss on Working with Files Linux, files are the fundamental units of a storage that contains some data or information.These are organized within a hierarchical structure called the file system. These files can represent various types of data, such as text files, configuration files, executable files, device dependent files etc..
Each files are case-sensitive which means if a file ‘test.sh’ is already existing in a directory and we try to create another file with the name as ‘Test.sh’ then we will be able to create file because Linux consider this as a new file.

Top commands for Files in Linux

  1. touch
  2. File case sensitive
  3. rm
  4. cp
  5. mv
  6. rename

The touch command

The touch command is used to create an empty files in Linux machine. In this example, we have created a file named test.sh.

Working with Files

The touch -t command is used to create or modify a file with timestamp in Linux. To add timestamp in a file requires a specific permission as a regular user we cannot add.

In the below example, we created a file named test.sh with timestamp where date is 21st June, 2023 and timing is 11:30.

As we discussed above, all files are case-sensitive which means it we have to type the correct name of the file. To understand this better, we will create two files along with different contents as below:

  1. File-1 as test.sh and content is This is a case-sensitive test file with small t.
  2. File-2 as Test.sh and content is This is a case-sensitive test file with capital T.

The rm command

The rm command is used to remove a file from Linux machine. As we know that we don’t have Recycle Bin in Linux machine like Windows to recover a deleted files. However, we can use third-party tool as a Trash in Linux.

The command rm -i stands for remove interactively which means it will ask for a confirmation before deleting a file in Linux machine. It is a best practice to use this command while working in production environment.

The command rm -rf stands for remove recursive forcefully. This command is used to delete a file or directory forcefully while containing some data into it. If a file or directory having some contents and trying to delete it, generally it will throw an error so we need to use -rf command to delete it forcefully.

The cp command

The command cp stands for copy. This is used to copy files or directories withing the Linux file system.

We can also copy files from one directory to another. In this example, in testdir1 doesn’t contains any files or directories as of now. We tried copying test.sh to this directory by using cp command and providing its destination path.

The command cp -r stands for copy recursive. This command is used to copy a complete directory which may contains multiple files or directories. In this example, we copied directory testdir1 to testdir2 along with its all contents.

The command cp -i stands for copy interactive. This command is also used to copy contents from one location to another but with some added feature that is interactive. Here, interactive means if a file or directory is already existing at destination then it will ask for a confirmation if we want to overwrite it. In the below example, we tried copying test.sh into testdir1 directory but one more file was already existing there with the same name so it asked us if we want to overwrite it.

The mv command

The mv command stands for move. This command is used to move or rename files or directories. It allows us to change the location of a file to another within the file system. In the below example, we tried copying ‘test.sh’ file to a newly created directory tesdir3.

We can also use mv command to rename a file. In the below example, we rename the file test1.sh to test2.sh.

We can use mv command to rename a directory in Linux file system. In the below example, we renamed directory testdir3 to testdir4.

Conclusion on Working with Files in Linux

So, in this section we covered Working with Files in Linux. It is a way of managing and manipulating data. The ability to create, read, write, copy, move, and delete files provides users with powerful tools for handling information efficiently. By effectively working with files, users can organize data, process information, and automate tasks whether it’s managing configuration files, analyzing log files, or scripting data processing workflows, mastering file operations in Linux empowers users to effectively utilize the file system. If you wish to learn more on this topic, please follow this here.

If you want to learn about Directory based commands, please click here.