File Operation Commands in LINUX

To learn how to do file operations for your projects this article will guide you through the basic commands in LINUX. Along with the examples shown you will be able to use them effectively in your work and do the necessary operations quickly.

open

The open command allows the user to open a file or directory using this syntax

open <filename>

If you a creating the file for the first time you can use the command touch <filename>

open command in LINUX

Here I have created a file called test1.txt using touch command. Using open command the file opens in the text editor

mv

The mv command is very useful to move a file or otherwise use to rename a file. The syntax for this command is by specifying the file current path, and its new path as shown below:

touch sample
mv sample new_sample

The sample file is moved to new_sample.

From the example you can see I created a file sample.txt and renamed it to new_sample.txt using move command. You can use verbose option by giving -v to see what the command does.

mv command can also be used to move a file to another folder, here I want to move new_sample.txt to Random folder which is the destination folder.

cp

The cp command copies a file from the source file to target file. The syntax for the copy command is as shown below:

cp sourcefile target

In this case I copy sample1.txt to the target file final_sample.txt similarly we can copy the entire directory but we need to specify the option -r.

If we want to copy the contents of a directory or any nested directories we need -R means copy directories recursively. Random is a folder and I want to make its copy and name it as Random_copy. To do this I should use cp -R source_directory destination_directory.

head

The head command outputs the beginning part of files. By default it prints 10 lines of a file. But we can specify the number lines using -n as an option

Raven.txt contains more than 100 lines but by default when we use head command it prints the first 10 lines.

tail

The tail command outputs the lines at the end of the file. By default tail command also prints the 10 lines from end of the file.

Tail command has an option -f which does not stop when the end of file is reached, but rather to wait for additional data to be appended to the input. It keeps printing out the end of the files even after new lines have been appended. This will be useful when we read log files or output files which keeps changing and we can monitor them. The syntax is shown below:

tail -f log_file.txt

Redirecting standard output

The > sign is used to redirect an output to store it to a file.

cat

The cat command helps to view content of a file, create single or multiple files, concatenate files and redirect output to the another file. The syntax is as shown below:

cat <filename>

The file contents contains the list of the files and folders. By using cat file name we can see the lines inside the file. cat command is very useful where we can concatenate multiple files together and take that output and save it into another file.

From the example I have two files flowers.txt and colors.txt , and I have used the cat command to concatenate the contents inside both the file and redirect it to another file called together.txt

Thank you for reading. Hope this article was helpful. Happy Coding!