Advance Linux Commands: File operation Command and tasks

A Linux file system is a structured collection of files on a disk drive or a partition. A partition is a segment of memory and contains some specific data. In our machine, there can be various partitions of the memory.

What is the Linux command to view what's written in a file? πŸ“„β“

A cat command is the most commonly used command to view the contents of a file. The syntax to use cat is quite simple, as shown below:

cat <filename>
In file name you can put the name which file you want to view.

Example - I have created avinash.txt file with some content.

and I want to view content of the avinash.txt file with cat command.

output -

Execute the command The content of the file will be displayed on your screen.

View File Contents By Line Numbers

The nl is another command in Linux which displays the contents of a file. It stands for β€˜Number lines’, as the output displayed by this command is numbered lines.

syntax :

nl <file name>

example :

output :

The nl command has even more options to format the output numbering in various ways. However, to simply display the contents, it can be used as discussed above.

View File Contents Using More or Less Commands

The β€˜more’ or β€˜less’ are pretty much the same command in Linux. They display a file on the terminal one page at a time and are thus very useful in displaying large files.

For smaller files, β€˜more’ works the same as the cat command, whereas β€˜less’ works the same for both larger and smaller files.

Syntax :

$ more <filename with extension>

$ less <filename with extension>

The output of β€˜less’ is displayed on an overlay screen, and can be exited by pressing β€˜q’. Thus the output is not permanently written to the screen.

View File Contents Using head or tail Commands

The head command is used to print the first 10 lines of a file, instead of printing the whole file. Similar tail prints the last 10 lines of a file.

For example, the following respectively print the first and last 10 lines of the huge logfile β€˜/var/log/syslogβ€˜.

$ tail <file name with extension>

If you need to print a different number of top or bottom lines of the file, use the argument β€˜-n<no_of_lines>’.

$ head -n5 /var/log/syslog

$ tail -n5 /var/log/syslog

How can you change the access permissions of files? πŸ”’β“

We will now address how to change various file permissions in Linux.

With the chmod command, you can easily modify the access permissions of files. It's an incredibly powerful command that allows you to control who can read, write, and execute a file. To change the permissions, use the following syntax:

chmod <permissions> <filename>

Replace <permissions> with the desired permission settings (e.g., 644 or +x) and <filename> with the name of the file you wish to modify. Execute the command and the access permissions will be updated accordingly.

The first step is to use the ls command to identify the default file permissions associated with the targeted file.

output :

The output portion -rw-rw-r from the above implies that this file has read and write permissions but no executable (x) permission. To make this script executable to both user and group, we will use the

Let us confirm that the file is now executable.

Change File’s Owner Permission to Read Only

A user will only read the file and neither write nor execute it.

output :

Change Group Permission to Read Only

Members of a user group will only read the file and not be able to write in it.

output :

Give File’s Owner Write Permission

A user will be able to write to the file.

output :

Change Group Permission to Write

Members of a group will be able to write to file.

Applying for Multiple File Permissions

Let us for instance assume we want the file owner to have to execute permission and the associated group has to write and execute permissions, we will implement the following command:

When dealing with directories and want to recursively make file permission changes to them, use the chmod command with the -R option.

In the above case, the user and group can read the directory but cannot add (write) files/folders.

Which command can you use to check the commands you have run till now? βŒ¨οΈβ“

To keep track of the commands you have executed in your current session, the history command comes to the rescue. Simply type history in your terminal, and a list of previously executed commands, along with their corresponding numbers, will be displayed. You can also recall specific commands using their respective numbers.

How do you remove a directory/folder using a Linux command? πŸ—‘οΈβ“

When you need to delete a directory (or folder) in Linux, the rmdir or rm command comes in handy. Here's how you can remove a directory:

rmdir <directory name >

Replace <directoryname> with the name of the directory you want to remove. Keep in mind that the directory must be empty for the rmdir command to work. Alternatively, you can use the rm command with the -r option to remove a directory and its contents recursively:

rm -r <directory name >

Exercise caution when using the rm command, as it permanently deletes files and directories.

How can you create a fruits.txt file and view its content? πŸŽπŸ“β“

To create a file named "fruits.txt" and add content to it, follow these steps:

echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > fruits.txt

The above command uses echo to append the desired content to the file. The -e flag enables interpretation of escape sequences like \n for new lines. To view the content of the file, use the cat command mentioned earlier:

cat fruits.txt

Execute these commands, and you'll have a file called "fruits.txt" with the specified content.

How can you add content to the devops.txt file? πŸ“β“

To add the fruits to the "devops.txt" file, follow these steps:

echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" >> devops.txt

The >> operator is used to append content to an existing file. Execute the command, and the fruits will be added to the "devops.txt" file, each on a separate line.

How can you show only the top three fruits from the file? πŸ₯‡πŸŽπŸŒπŸ’❓

If you want to display only the top three fruits from the "devops.txt" file, you can use the head command:

head -n 3 devops.txt

The -n option specifies the number of lines you want to display. In this case, it's set to 3. Execute the command, and you'll see the first three fruits listed.

How can you show only the bottom three fruits from the file? πŸ₯‰πŸŠπŸˆπŸ‡β“

To display only the bottom three fruits from the "devops.txt" file, we can use the tail command:

tail -n 3 devops.txt

Similar to the head command, the -n option determines the number of lines to display. Execute the command, and you'll find the last three fruits listed.

How can you create another file named Colors.txt and view its content? πŸŽ¨πŸ“β“

To create a file called "Colors.txt" and view its content, use the following commands

echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt

The above command creates the file "Colors.txt" and adds the specified colors to it. To view the content of the file, use the cat command:

cat Colors.txt

Execute these commands, and you'll have a new file named "Colors.txt" with the provided colors as its content.

How can you find the difference between the fruits.txt and Colors.txt files? πŸπŸ“πŸ”€πŸŽ¨β“

To compare the contents of two files and find the differences, the diff command is perfect:

diff fruits.txt Colors.txt

By executing this command, you'll see a line-by-line comparison between the "fruits.txt" and "Colors.txt" files, highlighting any discrepancies.

That's a wrap for this exhilarating journey through essential Linux commands for file operations! With these newfound skills, you can unleash your potential and achieve remarkable efficiency in handling files and directories. πŸŒŸπŸ’» So go ahead, experiment, and embrace the command line's power! πŸ’ͺπŸš€

πŸŒŸπŸ’»πŸ’ͺπŸš€ those who keep learning, will keep rising in life πŸŒŸπŸ’»πŸ’ͺπŸš€

Β