Guide to Useful File Manipulation Commands


Introduction

File manipulation commands are some of the most commonly used and important tools available to users. Whether you're a software developer, data analyst, or just an everyday computer user, file manipulation commands allow you to quickly and easily navigate, manipulate, and manage files on your system.

Let’s explore some of the most useful file manipulation commands that you can use in a terminal. These commands are essential for anyone who works with files on a regular basis and can greatly improve your efficiency and productivity. By mastering these file manipulation commands, you can become a more efficient and productive computer user.

Approach 1: Using ls to List Files in a Directory

The ls command is utilized to display the contents of a directory, and by default, it lists all the files and directories that exist within the current directory. However, you can specify a different directory as an argument to the ls command.

$ ls Desktop/cbl
1 1.cbl  2  2.cbl  3.cbl  4.cbl

Here are some common options that we can use with the ls command −

  • -l − long format, displays the file type, permissions, number of hard links, owner, group, size, and modification time for each file.

  • -a − all files, including hidden files and directories that start with a dot.

  • -h − human-readable, displays file sizes in a more human-readable format.

$ ls -lha

The command "ls -lha" lists all the files and directories in the current directory in a long format, including hidden files and directories, and displaying the file sizes in a human-readable format.

total 108K
drwxr-xr-x 17 papan papan 4.0K Mar  4 02:09 .
drwxr-xr-x  3 root  root  4.0K Feb 17 20:53 ..
drwxrwxr-x  2 papan papan 4.0K Feb 25 01:13 .aws

Approach 2: Using cp to Make a Copy of a File

This command is useful when you need to duplicate a file to another location or make a backup of a file.

$ cd Dekstop/cbl
$ ls
1.cbl  2  2.cbl  3.cbl  4.cbl

$ cp 2.cbl /home/papan/Documents
$ ls /home/papan/Documents
2.cbl

The cp command copies files from one location to another and overwrites existing files with the same name in the destination directory.

Approach 3: Using mv to Move Files

The mv command is used to move a file from one location to another or rename a file. It needs two arguments: the source file and the destination file.

$ mv 3.cbl /home/papan/Documents
$ ls /home/papan/Documents
3.cbl

As we can see, "mv" command is used to move files. It takes two arguments: the source file and the destination file or directory. The command moves the source file or directory to the specified destination.

Approach 4: Creating a New File Using the touch

The touch command is primarily used to create a new file with a specified name, and this file is initially empty.

Let’s create a new text file in a specified folder using “touch” command −

$ touch 1.txt
ls
1.txt

In case the file named "1.txt" does not exist, it will be created upon executing the corresponding command. If the file is already there, the timestamp of the file will be updated to the current time.

The touch command can also be used to create multiple files at once.

$ touch 2.txt 3.txt
ls
1.txt  2.txt  3.txt

This command will create two new files.

Approach 5: Using rm to Remove Files

This command is utilized to delete a file from the system.

To delete a file using the "rm" command, you simply need to specify the name of the file as an argument. For example −

$ cd Desktop/test
$ ls
1.txt  2.txt
$ rm 1.txt
$ ls
2.txt

If the file is write-protected or is part of a directory with write permission disabled, we will need to use the "-f" option to remove it and "-i" option command stands for "interactive mode".

$ rm -fi 2.txt
rm: remove regular empty file '2.txt'? Y

Approach 6: Using cat to Display File Contents

This command is used to show all the contents of a file. It displays the entire file in the terminal.

$ cat 1.txt
Hello World
Welcome to India.

The cat command can also concatenate the contents of multiple files and save the output to a new file −

$ cat 1.txt 2.txt 3.txt > combined.txt
$ cat combined.txt
Hello World
Welcome to India.
This is tutorialpoint article.
Do you know Linux?
Yes I do know.

This command will concatenate the contents of 1.txt, 2.txt, and 3.txt, and save the output to a new file called combined.txt.

Approach 7: Using head to Display First Few Lines

By default head command displays the first 10 lines of a file, but you can specify a different number of lines using the -n option.

$ head -n 3 combined.txt
Hello World
Welcome to India.
Good morning 

This command shows the first three lines of the combined.txt file.

Approach 8: Using tail to Display Last Few Lines

By default tail command displays the last 10 lines of the file, but you can specify a different number of lines using the -n option.

$ tail -n 2 combined.txt
I am fine.
Hello this is Somdeb.

This command shows the last two lines of the combined.txt file.

Conclusion

In conclusion, understanding and using file manipulation commands can be extremely helpful when working with files on a computer. The commands listed in this article are just a few of the many commands available for managing files on a Linux or Unix-based system. By mastering these commands, we can save time and effort while working with huge files.

Updated on: 29-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements