DEV Community

nadirbasalamah
nadirbasalamah

Posted on

File Operation with Linux Command

File Operations in Linux

File operations in Linux can be done by using many commands that already provided.

Create a new file and directory

To create a new file, use touch command. This is the example of command usage.

touch file.txt
Enter fullscreen mode Exit fullscreen mode

To create multiple files, can be done with the help of &&.

touch file_2.txt && touch file_3.txt
Enter fullscreen mode Exit fullscreen mode

To create a new directory, use mkdir command.

mkdir mydirectory
Enter fullscreen mode Exit fullscreen mode

Read a file

There are many ways to read a file, the first way is using installed text editor like vim, nano and so on. This is the example of read a file using nano text editor.

nano file.txt
Enter fullscreen mode Exit fullscreen mode

Nano screenshot

The second way of reading a file is using cat or more command.

cat file.txt
Enter fullscreen mode Exit fullscreen mode

The file's content is printed in the terminal when using cat command.

Edit a file

To edit a file can be done using text editor or related application with the file's type. In this example, the file with .txt extension is edited with text editor using nano.

  1. Open a file with nano command.
nano file.txt
Enter fullscreen mode Exit fullscreen mode
  1. Edit the file's content.
    Edit file with nano

  2. Save the file's changes with Ctrl + X then confirm by pressing Y. After that, hit Enter.
    Save file with nano

View files

To view a list of files can be done by using ls command.

ls screenshot

To view a list of files with specific information. use ll command. This command outputs all files with specific information.

ll screenshot

Here it is the information detail from ll command output.

-rw-r--r--            1              nadir       nadir     0    Jan  8 10:24   iamfile.txt
 access control     number of link  owner       group    size   time created   file name

Enter fullscreen mode Exit fullscreen mode

To view a specific file information, use ll | grep filename command.

ll | grep iamfile.txt
Enter fullscreen mode Exit fullscreen mode

Move and Copy file

To move a certain file, use mv command followed with target file and target directory. In this example, the file called file.txt moved to the mydirectory directory.

mv file.txt mydirectory
Enter fullscreen mode Exit fullscreen mode

To copy a certain file, use cp command followed with target file and target directory. In this example, the file called file_2.txt copied to the mydirectory directory.

cp file_2.txt mydirectory
Enter fullscreen mode Exit fullscreen mode

Remove file and directory

To remove certain file, use rm command followed with file name. In this example, the file called file_2.txt will be removed.

rm file_2.txt
Enter fullscreen mode Exit fullscreen mode

To remove a directory, use rmdir command followed with directory name.

rmdir directory
Enter fullscreen mode Exit fullscreen mode

To remove a directory and its content. Use rm -rf.

rm -rf directory_name
Enter fullscreen mode Exit fullscreen mode

Access Control in file

This access control also available for directory

There are three types of user in file's access control:

  • Owner user: u
  • Group: g
  • Other user: o

There are three types of file control:

  • read: read or open the file (r)
  • write: write inside the file (w)
  • execute: execute a file (x)

Access control mechanism can be done using chmod command followed with specific parameters to define the access control type. There are two ways to define the access control type.

1. Using characters

In this example, the file's access control defined using characters.

chmod go-w cool.txt
Enter fullscreen mode Exit fullscreen mode

Based on the command above, the cool.txt file can be written, read and executed by user. The group and other user cannot write or edit the file, but available for read operation.

The complete formula of this command can be seen below:

Operation Meaning
+ add access type
- remove access type
= available access type
chmod [user's type (u, g, o, a)] [-, +, =] [access type (r, w, x)]
Enter fullscreen mode Exit fullscreen mode

a means the all user type.

Example, user only available for reading the file.

chmod u=r hey.txt
Enter fullscreen mode Exit fullscreen mode

Another example, user only capable to read file, group available to write and other can read and write except execute the file:

chmod u=r,g+w,o-x cool.txt
Enter fullscreen mode Exit fullscreen mode

2. Using numbers.

The number that used in file's access control is using binary number to define the access type of certain file. This is the sample usage.

chmod 744 cool.txt
Enter fullscreen mode Exit fullscreen mode

Based on the command above, the user is available for all file's operation (read, write and execute), the group and the other user is only available to read the file.

The number 0 represents access is not available, when the number 1 represents access is available.

number read write execute
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1

File ownership

To change the file or directory ownership, use chown command followed with user's name.

chown cool.txt other_user
Enter fullscreen mode Exit fullscreen mode

To change the file or directory ownership for group, use chgrp followed with group's name.

chgrp cool.txt mygroup
Enter fullscreen mode Exit fullscreen mode

Tips

When you want to learn more about specific commands, just add --help after specific command.

ls --help
Enter fullscreen mode Exit fullscreen mode

I hope this article is helpful for learning about linux command especially for file operations. If you have any thoughts or feedbacks, you can write in the discussion section below.

Top comments (0)